Unit2 C Program
Unit2 C Program
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
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
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
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
if(condition)
{
true block;
}
Statement X;
MS Page 6
UNIT – II Programming in C
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;
MS Page 7
UNIT – II Programming in C
F if T
(Exp)
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
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.
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;
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
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
while False
Exp
True
Loop body
Next statements
Example:
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.
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.
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;
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:
Example:
To find the factorial value for given n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,p=1;
clrscr();
MS Page 25
UNIT – II Programming in C
p=p*i;
i++;
if(i<=n)
goto L;
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