0% found this document useful (0 votes)
20 views19 pages

Cp-Unit-Ii 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views19 pages

Cp-Unit-Ii 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DECISION MAKING STATEMENTS / CONDITIONAL STATEMENTS:

C program executes program sequentially. Sometimes, a program requires checking


of certain conditions in program execution. C provides various key condition statements to
check condition and execute statements according conditional criteria.These statements are
called as 'Decision Making Statements' or 'Conditional Statements.'
Followings are the different conditional statements used in C.
 If Statement
 If-Else Statement
 Nested If-Else Statement
 If else if statement
 Switch Case

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 :

/* Program to demonstrate if-else statement.

#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 :

C program to check even or odd


#include<stdio.h>
int main()
{
int number;
printf("Enter any integer: ");
scanf("%d",&number);
if(number % 2 ==0)
printf("%d is even number.",number);
else
printf("%d is odd number.",number);
return 0;

NESTED IF-ELSE STATEMENT:


It is a conditional statement which is used when we want to check more than 1
conditions at a time in a same program. The conditions are executed from top to bottom
checking each condition whether it meets the conditional criteria or not. If it found the
condition is true then it executes the block of associated statements of true part else it goes to
next condition to execute.
Syntax:

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 :

/* Program to demonstrate nested if-else statement.

#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:

For first 50 units – Rs. 3.50/unit


For 51 - 150 units – Rs. 4.00/unit
For 151 -250 units – Rs. 5.20/unit
For units above 250 – Rs. 6.50/unit

#include<stdio.h>
int main()
{
float bill, units;

printf("Enter the units consumed=");


scanf("%f",&units);

if(units<=50 && units>=0)


{
bill=units*3.50;
printf("Electricity Bill=%0.2f Rupees",bill);
}
else if(units<=150 && units>50)
{
bill=50*3.50+(units-50)*4;
printf("Electricity Bill=%0.2f Rupees",bill);

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

if(a<b && a<c && a<d)


{
printf("%d is min",a);
}
else if(b<c && b<d)
{
printf("%d is min",b);
}
else if(c<d)
{
printf("%d is min",c);
}
else
{
printf("%d is min",d);
}
return 0;
}

SWITCH CASE STATEMENT:


This is a multiple or multiway brancing decision making statement.
When we use nested if-else statement to check more than 1 conditions then the complexity of
a program increases in case of a lot of conditions. Thus, the program is difficult to read and
maintain. So to overcome this problem, C provides 'switch case'.
Switch case checks the value of a expression against a case values, if condition matches the
case values then the control is transferred to that point.
Syntax:

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 :

/* Program to demonstrate switch case statement.

#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 :

Write a C program to simulate a calculator using switch case.

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

* RULES FOR DECLARING SWITCH CASE :


 The case label should be integer or character constant.
 Each compound statement of a switch case should contain break statement to exit from
case.
 Case labels must end with (:) colon.
* ADVANTAGES OF SWITCH CASE :
 Easy to use.
 Easy to find out errors.
 Debugging is made easy in switch case.
 Complexity of a program is minimized.

LOOPING STATEMENTS / ITERATIVE STATEMENTS:


'A loop' is a part of code of a program which is executed repeatedly.
A loop is used using condition. The repetition is done until condition becomes condition true.
A loop declaration and execution can be done in following ways.
o Check condition to start a loop
o Initialize loop with declaring a variable.
o Executing statements inside loop.
o Increment or decrement of value of a variable.

* TYPES OF LOOPING STATEMENTS:


Basically, the types of looping statements depends on the condition checking mode.
Condition checking can be made in two ways as : Before loop and after loop. So, there are
2(two) types of looping statements.
 Entry controlled loop
 Exit controlled loop
1. Entry controlled loop :
In such type of loop, the test condition is checked first before the loop is executed.
Some common examples of this looping statements are :
while loop
for loop
2. Exit controlled loop :
In such type of loop, the loop is executed first. Then condition is checked after block of
statements are executed. The loop executed at least one time compulsorily.
Some common example of this looping statement is :
do-while loop

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 :

/* Program to demonstrate while loop.

#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:

for(initialisation; test-condition; incre/decre)


{
statements;
}

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 :

/* Program to demonstrate for loop.

#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 :

/* Program to demonstrate do while loop.

#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 :

/* Program to demonstrate break statement.

#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 :

/* Program to demonstrate continue statement.

#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 :

/* Program to demonstrate goto statement.

#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 :

WRITE A C PROGRAM TO CHECK GIVEN NUMBER IS PRIME NUMBER OR


NOT.
Definition of prime number:A natural number greater than one has not any other divisors
except 1 and itself. In other word we can say which has only two divisors 1 and number itself.
For example: 5Their divisors are 1 and 5.Note: 2 is only even prime number.
Logic for prime number in c
We will take a loop and divide number from 2 to number/2. If the number is not
divisible by any of the numbers then we will print it as prime number.

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;
}

Definition of Palindrome number or What is palindrome number?

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

What is Fibonacci series?


Logic of Fibonacci series
Definition of Fibonacci numbers:
We assume first two Fibonacci are 0 and 1
A series of numbers in which each sequent number is sum of its two previous numbers is
known as Fibonacci series and each numbers are called Fibonacci numbers. So Fibonacci
numbers is

Algorithm for Fibonacci series

Fn = Fn-2 + Fn-1

Example of Fibonacci series:

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

C program to find the factorial of a given number

#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("Factorial of %d is: %d",num,f);


return 0;
}
Sample output:
Enter a number: 5
Factorial of 5 is: 120

Definition of floyd's triangle:


Floyd's triangle is a right angled-triangle using the natural numbers. Examples of floyd's
triangle:
Example 1:
1
23
456
7 8 9 10
#include<stdio.h>
int main(){
int i,j,r,k=1;
printf("Enter the range: ");
scanf("%d",&r);

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

You might also like