Cp-Unit-Ii 2024
Cp-Unit-Ii 2024
IF STATEMENT:
This is a conditional statement used in C to check condition or to control the flow of
execution of statements. This is also called as 'decision making statement or control
statement.' The execution of a whole program is done in one direction only.
Syntax:
if(condition)
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program control
flow goes inside the braces and executes the block of statements associated with it. If it
returns false, then program skips the braces. If there are more than 1 (one) statements in if
statement then use { } braces else it is not necessary to use.
Program :
/* Program to demonstrate if statement.
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
a=5;
clrscr();
if(a>4)
printf("\nValue of A is greater than 4 !");
if(a==4)
printf("\n\n Value of A is 4 !");
getch();
}
Output :
IF-ELSE STATEMENT:
This is also one of the most useful conditional statement used in C to check
conditions.
Syntax:
if(condition)
{
true statements;
}
else
{
false statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow
goes inside the braces and executes the block of statements associated with it. If it returns
false, then it executes the else part of a program.
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("\n Enter Number :");
scanf("%d",&no);
if(no%2==0)
printf("\n\n Number is even !");
else
printf("\n\n Number is odd !");
getch();
}
Output :
if(condition)
{
if(condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow
goes inside the braces and again checks the next condition. If it is true then it executes the
block of statements associated with it else executes else part.
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("\n Enter Number :");
scanf("%d",&no);
if(no>0)
{
printf("\n\n Number is greater than 0 !");
}
else
{
if(no==0)
{
printf("\n\n It is 0 !");
}
else
{
printf("Number is less than 0 !");
}
}
getch();
}
Output :
IF ELSE-IF STATEMENTS:
If some situations if may be desired to nest multiple if-else statements. In this situation
one of several different course of action will be selected.
Syntax
if ( <exp1> )
Statement-1;
else if ( <exp2> )
Statement-2;
else if ( <exp3> )
Statement-3;
else
Statement-4;
When a logical expression is encountered whose value is true the corresponding
statements will be executed and the remainder of the nested else if statement will be
bypassed. Thus control will be transferred out of the entire nest once a true condition is
encountered.
The final else clause will be apply if none of the exp is true.
Write a C program to generate electricity bill.
Conditions:
#include<stdio.h>
int main()
{
float bill, units;
}
else if(units<=250 && units>150)
{
bill=50*3.50+100*4+(units-150)*5.20;
printf("Electricity Bill=%0.2f Rupees",bill);
else if(units>250)
{
bill=50*3.50+100*4+100*5.20+(units-250)*6.50;
printf("Electricity Bill=%0.2f Rupees",bill);
}
else
{
printf("Please enter valid consumed units...");
}
return 0;
}
Write a C program to find the max and min of four numbers using
if-else.
#include<stdio.h>
#include<conio.h>
int main() {
int a,b,c,d;
printf("Enter the Four Numbers :");
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a>b && a>c && a>d)
{
printf("%d is max \n",a);
}
else if(b>c && b>d)
{
printf("%d is max \n",b);
}
else if(c>d)
{
printf("%d is max \n",c);
}
else
{
printf("%d is big \n",d);
}
switch(expression)
{
case expr1:
statements;
break;
case expr2:
statements;
break;
''''''''''''''''
''''''''''''''''
case exprn:
statements;
break;
default:
statements;
}
In above syntax, switch, case, break are keywords.
expr1, expr2 are known as 'case labels.'
Statements inside case expression need not to be closed in braces.
Break statement causes an exit from switch statement.
Default case is optional case. When neither any match found, it executes.
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("\n Enter any number from 1 to 3 :");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n\n It is 1 !");
break;
case 2:
printf("\n\n It is 2 !");
break;
case 3:
printf("\n\n It is 3 !");
break;
default:
printf("\n\n Invalid number !");
}
getch();
}
Output 1 :
#include <stdio.h>
#include<conio.h>
int main(){
char ch;
int a, b, result;
clrscr();
printf("Enter an Operator (+, -, *, /, %): ");
scanf("%c", &ch);
printf("Enter two operands: \n");
scanf("%d %d", &a, &b);
switch(ch){
case '+':
result = a + b;
printf("%d + %d = %d", a, b, result);
break;
case '-':
result = a - b;
printf("%d - %d = %d", a, b, result);
break;
case '*':
result = a * b;
printf("%d * %d = %d", a, b, result);
break;
case '/':
result = a / b;
printf("%d / %d = %d", a, b, result);
break;
case '%':
result = a % b;
printf("%d mod %d = %d", a, b, result);
break;
default:
printf("choose proper menu");
break;
}
return 0;
}
WHILE LOOP:
This is an entry controlled looping statement. It is used to repeat a block of statements until
condition becomes true.
Syntax:
while(condition)
{
statements;
increment/decrement;
}
In above syntax, the condition is checked first. If it is true, then the program control flow
goes inside the loop and executes the block of statements associated with it. At the end of
loop increment or decrement is done to change in variable value. This process continues until
test condition satisfies.
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
a=1;
while(a<=5)
{
printf("\n TechnoExam");
a+=1 // i.e. a = a + 1
}
getch();
}
Output :
TechnoExam
TechnoExam
TechnoExam
TechnoExam
FOR LOOP :
This is an entry controlled looping statement.
In this loop structure, more than one variable can be initilized. One of the most important
feature of this loop is that the three actions can be taken at a time like variable initilisation,
condition checking and increment/decrement. The for loop can be more concise and flexible
than that of while and do-while loops.
Syntax:
In above syntax, the given three expressions are seperated by ';' (Semicolon)
Features :
o More concise
o Easy to use
o Highly flexible
o More than one variable can be initilized.
o More than one increments can be applied.
o More than two conditions can be used.
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
for(i=0; i<5; i++)
{
printf("\n\t TechnoExam"); // 5 times
}
getch();
}
Output :
TechnoExam
TechnoExam
TechnoExam
TechnoExam
TechnoExam
DO-WHILE LOOP :
This is an exit controlled looping statement.
Sometimes, there is need to execute a block of statements first then to check condition. At
that time such type of a loop is used. In this, block of statements are executed first and then
condition is checked.
Syntax:
do
{
statements;
(increment/decrement);
}while(condition);
In above syntax, the first the block of statements are executed. At the end of loop, while
statement is executed. If the resultant condition is true then program control goes to evaluate
the body of a loop once again. This process continues till condition becomes true. When it
becomes false, then the loop terminates.
Note: The while statement should be terminated with ; (semicolon).
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
a=1;
do
{
printf("\n\t TechnoExam"); // 5 times
a+=1; // i.e. a = a + 1
}while(a<=5);
a=6;
do
{
printf("\n\n\t Technowell"); // 1 time
a+=1; // i.e. a = a + 1
}while(a<=5);
getch();
}
Output :
TechnoExam
TechnoExam
TechnoExam
TechnoExam
TechnoExam
Technowell
Break Statement :
Sometimes, it is necessary to exit immediately from a loop as soon as the condition is
satisfied.
When break statement is used inside a loop, then it can cause to terminate from a loop. The
statements after break statement are skipped.
Syntax :
break;
Figure :
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1; ; i++)
{
if(i>5)
break;
printf("%d",i); // 5 times only
}
getch();
}
Output :
Continue Statement :
Sometimes, it is required to skip a part of a body of loop under specific conditions. So, C
supports 'continue' statement to overcome this anomaly.
The working structure of 'continue' is similar as that of that break statement but difference is
that it cannot terminate the loop. It causes the loop to be continued with next iteration after
skipping statements in between. Continue statement simply skipps statements and continues
next iteration.
Syntax :
continue;
Figure :
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i++)
{
if(i==6)
continue;
printf("\n\t %d",i); // 6 is omitted
}
getch();
}
Output :
Goto Statement :
It is a well known as 'jumping statement.' It is primarily used to transfer the control of
execution to any place in a program. It is useful to provide branching within a loop.
When the loops are deeply nested at that if an error occurs then it is difficult to get exited
from such loops. Simple break statement cannot work here properly. In this situations, goto
statement is used.
Syntax :
goto [expr];
Figure :
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1, j;
clrscr();
while(i<=3)
{
for(j=1; j<=3; j++)
{
printf(" * ");
if(j==2)
goto stop;
}
i = i + 1;
}
stop:
printf("\n\n Exited !");
getch();
}
Output :
Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
167, 173, 179, 181, 191, 193, 197, 199 etc
#include<stdio.h>
int main()
{
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
A number is called palindrome number if it is remain same when its digits are reversed. For
example 121 is palindrome number. When we will reverse its digit it will remain same
number i.e. 121
Palindrome numbers examples: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99,
101, 111, 121, 131, 141, 151, 161, 171, 181, 191 etc
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
Sample output:
Enter a number: 131
131 is a palindrome
Fn = Fn-2 + Fn-1
0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ...
5 is Fibonacci number since sum of its two previous number i.e. 2 and 3 is 5
8 is Fibonacci number since sum of its two previous number i.e. 3 and 5 is 8 and so on.
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.
for(k=2;k<r;k++)
{
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
#include<stdio.h>
int main(){
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num)
{
f=f*i;
i++;
}
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}
Sample output:
Enter the range: 10
FLOYD'S TRIANGLE
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55