0% found this document useful (0 votes)
20 views

Unit2 C Program

C Program notes for college students

Uploaded by

deepaamalan21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit2 C Program

C Program notes for college students

Uploaded by

deepaamalan21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIT – II Programming in C

Data Input Output Functions:


Input functions are used to feed the value into the program during the execution. In the same way output
functions are used to extract the output from the program.
Unformatted Input functions:
getchar():
This input statement is used to read a character by the keyboard during the program execution.
Syntax:
charvar=getchar();
Ex:
ch=getchar();
gets():
This input functions is used to read a string (collection of characters) by the keyboard during the
execution.
Syntax:
gets(string_var);
Ex:
gets(name);
Unformatted Output functions:
putchar():

This output function is used to display a single character on the output screen.

Syntax:
putchar(char_var);
Ex:
putchar(ch);
puts():
This output function is used to display a string on the output screen.
Syntax:
puts(str_var);
Ex:
puts(name);

MS Page 1
UNIT – II Programming in C

Formatted input output functions:


scanf():
This input function is used to read all kinds of data from the standard input device keyboard.
Syntax:
scanf(“control string”,&var1,&var2,…,&varn);
Ex:
int a,b;
float c;
scanf(“%d%d%f”,&a,&b,&c);
printf():
This formatted output function is used to display all kinds of output on the output screen.
Syntax:
printf(“control string”,var1,var2,…,varn);
Ex:
printf(“%d%d%f”,a,b,c);
Control String:
The following control strings can be used in formatted input output function for reading and writing
operations of different data elements.
 %d – Integer
 %c – Character
 %f – Float
 %s – String
 %lf – double
 %ld – long integer
 %Lf – long double
Simple Programs:
Addition of two numbers:
#include<stdio.h>
#include<conio.h>

void main()
{

MS Page 2
UNIT – II Programming in C

int a,b,c;
clrscr();
printf("Enter your a and b value :\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("Addition of a and b is : %d",c);
getch();
}
Output:
Enter your a and b value :
60
40
Addition of a and b is : 100
Area and perimeter of a circle:
#include<stdio.h>
#include<conio.h>

void main()
{
int r;
float area,per;
clrscr();
printf("Enter your radius value :");
scanf("%d",&r);
area=3.14*r*r;
per=2*3.14*r;
printf("\nArea of the circle is : %f",area);
printf("\nPerimeter of the circle is : %f",per);
getch();
}
Output:
Enter your radius value : 7

MS Page 3
UNIT – II Programming in C

Area of the circle is : 153.860001


Perimeter of the circle is : 43.959999
Area and perimeter of a rectangle:
#include<stdio.h>
#include<conio.h>

void main()
{
int l,b,area,per;
clrscr();
printf("\n Enter your l and b values :\n");
scanf("%d%d",&l,&b);
area=l*b;
per=2*(l+b);
printf("\nArea of Rectangle is : %d",area);
printf("\nPerimeter of Rectangle is : %d",per);
getch();
}
Output:
Enter your l and b values :
4
6
Area of Rectangle is : 24
Perimeter of Rectangle is : 20
Area and perimeter of square
#include<stdio.h>
#include<conio.h>

void main()
{
int a,area,per;
clrscr();

MS Page 4
UNIT – II Programming in C

printf("\n Enter your a value :");


scanf("%d",&a);
area=a*a;
per=4*a;
printf("\nArea of square is : %d",area);
printf("\nPerimeter of square is : %d",per);
getch();
}
Output:
Enter your a value : 8
Area of square is : 64
Perimeter of square is : 32
Roots of a quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int a,b,c;
float r1,r2;
clrscr();
printf("\nEnter your a,b and c values :\n");
scanf("%d%d%d",&a,&b,&c);
r1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);
r2=(-b-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\nRoots for given equation is:”);
printf("\nRoot1 : %f",r1);
printf("\nRoot2 : %f",r2);
getch();
}
Output:

MS Page 5
UNIT – II Programming in C

Enter your a,b and c values :


1
1
-6
Roots for given equation is:
Root1 : 2.000000
Root2 : -3.000000
Statement:
Every line in a C program is called statement. All the statements should be ended with a semicolon
except conditional and looping statements.
Flow of Control or Control Structors:
We know that C program is a set of statements which are normally executed sequentially. We have
a number of situations where we have to change this sequential execution. So change this sequential
execution C provides conditional (selection), looping and jumping statements.
Conditional Statements or Selection Structures:
Conditional statements are used to execute or omit set of statements based on the conditions. C
offers the following conditional statements
1. if or simple if.
2. if else
3. else if
4. switch
if or simple if:
It checks the condition; if the condition is true then it executes the true block. If the condition is
false then it skips the true block.
Syntax:

if(condition)
{
true block;
}

Statement X;

MS Page 6
UNIT – II Programming in C

Control flow diagram:

if
(Exp)

True Block

Example:
#include<stdio.h>
#include<conio.h>

void main()
{
int p1,p2,distance;
clrscr();
printf("Enter your two points :\n");
scanf("%d%d",&p1,&p2);
distance=p1-p2;

if(distance<0)
distance=-distance;

printf("Distance is between %d and %d is : %d",p1,p2,distance);


getch();
}
Output:
Enter your two points :
8
5

MS Page 7
UNIT – II Programming in C

Distance is between 8 and 5 is : 3


if else statement:
It checks the condition; if the condition is true then it executes the true block. If the condition is
false then it executes the false block. Every time any one of the block will be executed. Not both at the
same time.
Control flow diagram:

F if T
(Exp)

False Block True Block

Syntax:

if(condition)
{
True block;
}
else
{
False block;
}

Example1:
#include<stdio.h>
#include<conio.h>

void main()
{
int mark;

MS Page 8
UNIT – II Programming in C

clrscr();
printf("Enter your mark :");
scanf("%d",&mark);
if(mark>=40)
printf("\n PASS ");
else
printf("\n FAIL");

getch();
}
Output:
Enter your mark :
80
PASS
Example2:
#include<stdio.h>
#include<conio.h>

void main()
{
int age;
clrscr();
printf("Enter your age :");
scanf("%d",&age);

if(age>=18)
printf("\nYou are eligible to vote");
else
printf("\nYour are not eligible to vote");

getch();
}

MS Page 9
UNIT – II Programming in C

Output:
Enter your age :28
You are eligible to vote
else if statement:
It is very similar to if else, but it checks another one if condition in the else part.
Control flow diagram:

if F
Exp-1

T
if F
Block - 1 Exp-2

T
if F
Block - 2 Exp-3

Block - 3 Else block

Syntax:
if(condition1)
{
Block1;
}
else if(condition 2)

{
Block2;
}
else if(condition 3)
{

MS Page 10
UNIT – II Programming in C

Block3;
}
…………

else if(condition n)
{
Block n;
}
else
{
Default Statement;

Example1:
To find the given number is positive or negative.
#include<stdio.h>
#include<conio.h>

void main()
{
int num;
clrscr();
printf("Enter your number : ");
scanf("%d",&num);

if(num>0)
printf("\n Positive Number");
else if(num<0)
printf("\n Negative Number");
else
printf("\n Zero");

MS Page 11
UNIT – II Programming in C

getch();
}

Output:
Enter your number :
-78
Negative Number
Example2:

#include<stdio.h>
#include<conio.h>

void main()
{
int avg;
clrscr();
printf("\nEnter your avg value : ");
scanf("%d",&avg);

if(avg>=75)
printf("\nDistinction");
else if(avg>=60)
printf("\nFirst class");
else if(avg>=50)
printf("\nSecond class");
else if(avg>=40)

printf("\nThird class");
else
printf("\n Fail");
getch(); }

MS Page 12
UNIT – II Programming in C

Output:
Enter your avg value : 64
First class

switch statement:

We can use switch case instead of multiple else-if ladder. It is used to select appropriate case among
multiple alternatives.

Control flow diagram:

switch
Exp

case val-1
Block - 1

case val-2
Block - 2

.
.

case val-n
Block - n

default
Default block

Syntax:

switch (expression)
{
case val1:
block 1;

break;
case val2:

MS Page 13
UNIT – II Programming in C

block 2;
break;
…………

case valn:
block n;
break;
default :
default block;
}
Example:

#include<stdio.h>

#include<conio.h>

void main()
{
int a,b,c=0;
char op;

printf("\nEnter your arithmetic operator :");


scanf("%c",&op);
printf("\nEnter your a and b value :\n");
scanf("%d%d",&a,&b);

switch(op)
{

case '+':
c=a+b;
break;

MS Page 14
UNIT – II Programming in C

case '-':
c=a-b;
break;

case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
case '%':

c=a%b;
break;
default:
printf("\n Invalid Arithmetic operator");
}
printf("\nYour result is : %d",c);

getch();
}

Output:
Enter your arithmetic operator : %
Enter your a and b value :
10

3
Your result is : 1

Example2:
#include<stdio.h>
#include<conio.h>

MS Page 15
UNIT – II Programming in C

void main()
{
int day;

clrscr();
printf("\nEnter your day value :");
scanf("%d",&day);

switch(day)
{
case 1:

printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:

printf("Tues day");
break;
case 4:
printf("Wednesday");
break;
case 5:

printf("Thurs day");
break;
case 6:
printf("Friday");
break;

MS Page 16
UNIT – II Programming in C

case 7:
printf("Satur day");
break;

default:
printf("\nInvalid day value ");
}
getch();
}

Output:
Enter your day value : 8

Invalid day value


Looping Statements:

Looping statements are used to execute set of lines repeatedly. C provides the following looping
statements

1. While
2. do-while
3. for
while loop:

It is an entry-controlled loop. It first checks the condition, if the condition is true then the loop body
will be executed. Same process will be continued until the specified condition becomes false.

Syntax:
while(condition)
{
Body of the loop;
}
Next statements;

MS Page 17
UNIT – II Programming in C

Control flow diagram:

while False
Exp

True

Loop body

Next statements

Example:

Print the series : ->1->2->3->….->n


#include<stdio.h>
#include<conio.h>

void main()
{
int i=1,n;

clrscr();
printf("Enter your n value :");
scanf("%d",&n);

while(i<=n)
{

printf("->%d",i);
i=i+1;
}
getch(); }

MS Page 18
UNIT – II Programming in C

Output:
Enter your n value : 15
->1->2->3->4->5->6->7->8->9->10->11->12->13->14->15

Example2:
Sum of n numbers : 1+2+3+…+n
#include<stdio.h>
#include<conio.h>

void main()
{

int i=1,s=0,n;
clrscr();
printf("Enter your n value :");
scanf("%d",&n);

while(i<=n)

{
s=s+i;
i++;
}
printf("\n Sum of %d number is : %d",n,s);
getch();

Output:
Enter your n value : 8
Sum of 8 number is : 36

MS Page 19
UNIT – II Programming in C

Do While loop:

It is very similar to while loop, but it is an exit control loop. It first executes the body of the loop
and then tests the condition. If the condition is true then go for iteration otherwise it will terminate the loop
execution.

Control flow diagram:

Loop body

True while
Exp

False

Next statements

Syntax:

do

{
Body of the loop;
}while(condition);
Next statements;

Example1:
#include<stdio.h>
#include<conio.h>
void main()

{
int i=1,n;

MS Page 20
UNIT – II Programming in C

clrscr();
printf("Enter your n value :");
scanf("%d",&n);

printf("0");
do
{
printf(" +%d",i);
i++;
}while(i<=n);
getch();

Output:
Enter your n value : 10
0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10

Example2:
To find the factorial value for given n.
#include<stdio.h>
#include<conio.h>

void main()
{
int p=1,i=1,n;

clrscr();
printf("Enter your n value :");
scanf("%d",&n);
do
{
p=p*i;

MS Page 21
UNIT – II Programming in C

i=i+1;
}while(i<=n);
printf("\nFactorial of %d is : %d",n,p);

getch();
}

Output:
Enter your n value : 6
Factorial of 6 is : 720
Note: Main difference between while and do while is, at very first time if the condition is false then while
loop will not be executed, but do while will execute at least once.

For loop:

For loop is a useful looping statement, if you know how many times you need to execute the body of
the loop.

Control flow diagram:

Syntax:
for(initialization;condition;Increment/decrement)
{
Body of the loop;

MS Page 22
UNIT – II Programming in C

Example:
#include<stdio.h>
#include<conio.h>

void main()
{
int s=0,n,i;
clrscr();
printf("Enter your n value : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
s=s+i;

printf("\nSum value is : %d",s);


getch();
}
Output:
Enter your n value : 10
Sum value is : 55
Nested Control Structure:

If a control structure is presented inside of another one same control structure is called nested control
structure. For example if an if statement is present inside of an if is called nested if statement. In the same
way if a loop is presented inside of another loop is called nested loop.

Example:
#include<stdio.h>
#include<conio.h>

void main()

{
int a,b,c;

MS Page 23
UNIT – II Programming in C

clrscr();
printf("Enter your a,b and c values :\n");
scanf("%d%d%d",&a,&b,&c);

if(a>b)
{
if(a>c)
printf("\nA is greater");
else
printf("\nC is greater");

}
else
{
if(b>c)
printf("\nB is greater");
else

printf("\nC is greater");
}
getch();
}

Output:
Enter your a,b and c values :

50
130
78
B is greater

MS Page 24
UNIT – II Programming in C

Jumping statements:
Jumping statements are used to transfer the control from one place to another place in a program
either conditionally or unconditionally. C provides the following jumping statements.
1. goto
2. break
3. continue
goto:
C supports the goto statement to branch unconditionally from one point to another point in a
program. The goto requires a label to identify the place where the branch is to be made. Label is a valid
variable name.
Control flow diagram:

Forward Jump Backward Jump

Example:
To find the factorial value for given n.
#include<stdio.h>

#include<conio.h>

void main()
{
int n,i=1,p=1;
clrscr();

printf("Enter your n value :");


scanf("%d",&n);
L:

MS Page 25
UNIT – II Programming in C

p=p*i;
i++;
if(i<=n)

goto L;

printf("\nThe factorial of %d is : %d",n,p);


getch();
}

Output:
Enter your n value : 6
The factorial of 6 is : 720
break:
When the break statement is encountered inside a loop, the loop is immediately terminated and the
program continues with the statement immediately following the loop. When the loops are nested, the break
can only exit from the loop containing it. That is, the break will exit only a single loop.
Example:
To find the given number is prime or not.
#include<stdio.h>

#include<conio.h>

void main()
{
int i,n,a=0;
clrscr();
printf("Enter your n value :");

scanf("%d",&n);

for(i=2;i<n;i++)
if(n%i==0)

MS Page 26
UNIT – II Programming in C

{
a=1;
break;

if(a==0)
printf("\nThe given number %d is prime",n);
else
printf("\nThe given number %d is not prime",n);

getch();
}

Output:
Enter your n value : 21
The given number 21 is not prime
continue:
Sometimes we need to skip a part of the body of the loop. In this situation we can use continue statement.
When a continue statement is encountered inside a loop control automatically transferred to the beginning of
the loop for the next iteration.
Example:
Print the series 1 2 4 5 7 8 10…..n
#include<stdio.h>
#include<conio.h>

void main()
{
int i,n;
clrscr();
printf("Enter your n value : ");
scanf("%d",&n)

MS Page 27
UNIT – II Programming in C

for(i=1;i<=n;i++)
{
if(i%3==0)
continue;
printf(" %d",i);
}
getch();
}
Output:
Enter your n value : 30
1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29

MS Page 28

You might also like