Basic Structure of C-Program

24
Basic Structure Basic Structure of C-Program of C-Program

Transcript of Basic Structure of C-Program

Page 1: Basic Structure of C-Program

Basic Structure Basic Structure of C-Programof C-Program

Page 2: Basic Structure of C-Program

Structure of c-programsStructure of c-programs

Page 3: Basic Structure of C-Program

Documentation SectionDocumentation Section• The Documentation Section consists of set of

comment lines giving the Name of the program The author Other Details which the programmer would like to

use later Syntax /* Multiple Comment lines *\ // Single Comment line Example /* c –program to find gcd and lcm of two numbers

using Euclid's Algorithm *\ // Welcome to C

Page 4: Basic Structure of C-Program

Link SectionLink Section• The link section provides instruction to the

complier to link functions from system library• Syntax #include<header file name .h>Header files stdio– Standard Input /Output conio– Console input/Output, math- contains mathematical functions like

(cos(), sin(), sqrt(), abs())• Example #include<stdio.h> #include<conio.h>

Page 5: Basic Structure of C-Program

Definition SectionDefinition Section• This section is used to define symbolic

constants• Syntax:- #define symbolic-name constant-value• Example:- #define pi 3.142 #define maxmarks 100 #define minmarks 35

Page 6: Basic Structure of C-Program

Global declaration SectionGlobal declaration Section• In C There are two types of declarations 1.Local variable declaration 2. Global variable Declaration• Global variables are declared out side the

main function• Local variables are declared inside the

main function

Page 7: Basic Structure of C-Program

Variable declaration Variable declaration • Syntax:- data-type variable-name;Ex:- int a,x; float b,c; char name;

Page 8: Basic Structure of C-Program

Main() functionMain() function• C-program should have at least one main()

function• Form:- main() { local variables declaration computation part logic part}

Page 9: Basic Structure of C-Program

Reading data from input Reading data from input device (keyboard)device (keyboard)

• scanf() function is used to read the data from standard input device

• Syntax:- scanf(“format specifier”,&var1,&var2-----,&varn)

• Example:- scanf(“%d”,&a) for int data-type scanf(“%f”,&b) for float data-type

Page 10: Basic Structure of C-Program

Print data or variablePrint data or variable• printf() is used to print the variable or data• Syntax:- printf(“ message -format specifier”,var1,var2-----,varn)

• Example:- printf(“Welcome to c”); int a=10; printf(“value of a=%d”,a);

Page 11: Basic Structure of C-Program

Arithmetic operatorsArithmetic operators

C operation

Arithmetic operator

Algebraic expression

C expression

Addition + f + 7 f + 7

Subtraction - p – c p - c

Multiplication * bm b * m

Division / x/ y x / y

Modulus % r mod s r % s

Page 12: Basic Structure of C-Program

Write a c-program to find Write a c-program to find sum of two numberssum of two numbers

Page 13: Basic Structure of C-Program

/* c-program to find sum of two numbers *\#include<stdio.h> //link section#include<conio.h>main(){int a,b,sum; // local variable declaration clrscr();printf("enter two numbers \n");scanf("%d%d",&a,&b); // reading input from std i/psum=a+b; //computation partprintf("sum of two numbers=%d",sum); //ouputgetch();}

Page 14: Basic Structure of C-Program

Write a c-program to find Write a c-program to find Area of a CircleArea of a Circle

Page 15: Basic Structure of C-Program

/* c-program to find Area of a circle *\#include<stdio.h>#include<conio.h>#define pi 3.142 // definition section main(){float r,area;clrscr();printf("enter the radius value\n");scanf("%f",&r);area=pi*r*r;printf("area of a circle=%f",area);getch();}

Page 16: Basic Structure of C-Program

Write a c-program to evaluate Write a c-program to evaluate following expressionsfollowing expressions

• Area=∏r2 +2∏rh• Torque= (2m1m2/(m1+m2))*g• Side=sqrt(a2+b2+2abcos(x)) • Energy= mass [(acceleration*ht)+(vel)2/2]

Page 17: Basic Structure of C-Program

Write a c-program to convert Write a c-program to convert number of days to a measure of number of days to a measure of time given in years, weeks and time given in years, weeks and days days

Example:-Example:-

375 days is equal to375 days is equal to 1 year 1 year 1 week 1 week 3 days 3 days

Page 18: Basic Structure of C-Program

# include <stdio.h>#include<conio.h>#define DAYSINWEEK 7 main(){ int ndays, year, week, days; clrscr(); printf("Enter the number of days\n"); scanf("%d",&ndays); year = ndays/365; week = (ndays % 365)/DAYSINWEEK; days = (ndays%365) % DAYSINWEEK; printf ("%d is equivalent to %d years, %d weeks

and %d days\n", ndays, year, week, days); getch(); }

Page 19: Basic Structure of C-Program

Write a c-program to compute Write a c-program to compute the surface area and volume of the surface area and volume of

the cubethe cube• surfArea=6.0*side*side• Volume=side3

Page 20: Basic Structure of C-Program

#include <stdio.h> #include<conio.h> #include <math.h> main() { float side, surfArea, volume; clrscr(); printf("Enter the length of a side\n"); scanf("%f", &side); surfArea = 6.0 * side * side; volume = pow (side, 3); printf("Surface area =%f and Volume=%f", surfArea,

volume); getch(); }

Page 21: Basic Structure of C-Program

Write a C-Program To Write a C-Program To Accept Student Roll No, Accept Student Roll No, Marks in 3 Subjects and Marks in 3 Subjects and Calculate Total, Average Calculate Total, Average

and Print it.and Print it.

Page 22: Basic Structure of C-Program

# include <stdio.h># include <conio.h>main(){int r,b,c,d, tot, avg;clrscr();printf (“ENTER STUDENT RNO ; “);scanf (“%d”,&r);printf(“ENTER FIRST SUBJECT MARKS ;”);scanf(“%d”,&b);printf(“ENTER SECOND SUBJECT MARKS;”);scanf(“%d”,&c);printf(“ENTER THIRD SUBJECT MARKS ;”);scanf(“%d”,&d);tot=b+c+d;avg=tot/3;printf(“\t STUDENT RNO ; %d “,r);printf(“\t FIRST SUBJECT MARKS ;%d “,b);printf(“\t SECOND SUBJECT MARKS ;%d “,C);printf(“\t THIRD SUBJECT MARKS ;%d “,d);printf(“\t AVERAGE MARKS ; %d”, avg);getch();}

Page 23: Basic Structure of C-Program

Write a C-Program to Write a C-Program to accept a three digit accept a three digit

number and print the number and print the sum of individual digits.sum of individual digits.

Page 24: Basic Structure of C-Program

# include <stdio.h># include <conio.h>main( ){int a,b,c,n, sum;clrscr( );printf (“ Enter a Three Digit Number:“);scanf (“%d”,&n);a=n/100;b=( (n%100)/10);c=n%10;sum=a+b+c;printf (“ Sum of Individual Digits of Given Numbers is %d”, Sum);getch( );}