• No results found

Basic Structure of C-Program

N/A
N/A
Protected

Academic year: 2021

Share "Basic Structure of C-Program"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

Basic Structure

Basic Structure

of C-Program

(2)

Structure of c-programs

(3)

Documentation Section

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 *\

(4)

Link Section

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

(5)

Definition Section

Definition Section

• This section is used to define symbolic

constants

#define symbolic-name constant-value

#define pi 3.142

#define maxmarks 100

#define minmarks 35

(6)

Global declaration Section

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

(7)

Variable declaration

Variable declaration

data-type variable-name;

int a,x;

float b,c;

char name;

(8)

Main() function

Main() function

• C-program should have at least one

main() function

main()

{

local variables declaration

computation part

(9)

Reading data from input

Reading data from input

device (keyboard)

device (keyboard)

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

standard input device

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

scanf(“%d”,&a) for int data-type

scanf(“%f”,&b) for float data-type

(10)

Print data or variable

Print data or variable

• printf() is used to print the variable or

data

• Syntax:-

printf(“ message -format specifier”,var1,var2---,varn)

printf(“Welcome to c”);

int a=10;

(11)

Arithmetic operators

Arithmetic operators

C o p e ra tio n

A rith m e tic

o p e ra to r

A lg e b ra ic

e xp re ssio n

C e xp re ssio n

A d d i t i o n

+

f + 7

f + 7

S u b t r a c t i o n

-

p – c

p - c

M u l t i p l i c a t i o n

*

b m

b * m

D i v i s i o n

/

x / y

x / y

M o d u l u s

%

r m o d s

r % s

(12)

Write a c-program to find

Write a c-program to find

sum of two numbers

(13)

/* 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(); }

(14)

Write a c-program to find

Write a c-program to find

Area of a Circle

(15)

/* 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();

(16)

Write a c-program to evaluate

Write a c-program to evaluate

following expressions

following expressions

• Area=∏r

2

+2∏rh

• Torque= (2m1m2/(m1+m2))*g

• Side=sqrt(a

2

+b

2

+2abcos(x))

• Energy=

mass [(acceleration*ht)+(vel)

2

/2]

(17)

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:-375 days is equal to

375 days is equal to

1 year

1 year

1 week

1 week

3 days

3 days

(18)

# 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);

(19)

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 cube

the cube

• surfArea=6.0*side*side

(20)

#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);

(21)

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.

(22)

# 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);

(23)

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.

(24)

# 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);

References

Related documents