Sie sind auf Seite 1von 24

Basic Structure

of C-Program
Structure of c-programs
Documentation 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
Link 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>
Definition 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
Global 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
Variable declaration
• Syntax:-
data-type variable-name;
Ex:-
int a,x;
float b,c;
char name;
Main() function
• C-program should have at least one
main() function
• Form:-
main()
{
local variables declaration
computation part
logic part
}
Reading data from input
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
Print 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);
Arithmetic operators

C o p e ra tio n Arithm e tic Alg e b ra ic C e xp re ssio n


op e ra tor e xp re ssio n
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
Write a c-program to find
sum of two numbers
/* 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/p
sum=a+b; //computation part
printf("sum of two numbers=%d",sum); //ouput
getch();
}
Write a c-program to find
Area of a Circle
/* 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();
}
Write a c-program to evaluate
following expressions
• Area=∏r2 +2∏rh
• Torque= (2m1m2/(m1+m2))*g
• Side=sqrt(a2+b2+2abcos(x))
• Energy=
mass [(acceleration*ht)+(vel)2/2]
Write a c-program to convert
number of days to a measure of
time given in years, weeks and
days

Example:-

375 days is equal to


1 year
1 week
3 days
# 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();
}
Write a c-program to compute
the surface area and volume of
the cube
• surfArea=6.0*side*side
• Volume=side3
#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();
}
Write a C-Program To
Accept Student Roll No,
Marks in 3 Subjects and
Calculate Total, Average
and Print it.
# 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();
}
Write a C-Program to
accept a three digit
number and print the
sum of individual digits.
# 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( );
}

Das könnte Ihnen auch gefallen