C - Day 2
C - Day 2
RECAP
Topics
• Conditional Statements
• if
• switch
• goto
Conditional Statements
Types of Conditional Statements
• if statement
• switch statement
• goto statement
• Conditional operator
if Statement
• if statement can be used as-
• Simple if statement
• if else statement
• Nested if else statement
• else if ladder
Simple if Statement
syntax:
if(test expression)
test False
{ expression
statement-1;
True
………………..
Statement Block
……………….. Statement Block
statement-n;
} Statement-x
statement-x;
if else statement
syntax:
if(test expression) test
{ expression
False True
statement block-1
}
else Statement block-2 Statement block-1
{
statement block-2;
}
statement-x; Statement-x
Nested if else statement
syntax:
if(test expression-1)
{
if(test expression-2)
{ test
statement block-1 expression-1
}
else True
{ False
statement block-2
}
} test test
else expressi expressi
{ on-3
if(test expression-3)
on-2
{ False
statement block-3 False True
True
}
else Statement
{
Statement block-4 Statement
statement block-4
} block-2 Statement block-3
} block-1
Statement-x;
Statement-x
else if ladder
syntax:
if(test expression-1)
{ test
Statement block-1 expression-
} False 1
True
else if(test expression-2)
test
{ statement
expressio
Statement block-2 n-2 block-1
} False True
else
{ statement statement
statement block-3 block-3 block-2
}
Statement-x;
statement
block-1
Program to check whether a number is
even or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Eneter a Number:\n”);
scanf(“%d”,&n); Output:
if(n%2==0) Enter a Number:
printf(“Number is Even”); 12
Number is Even
else
printf(“Number is odd”);
getch();
}
Program to check whether a year is a leap
year or not
#include<stdio.h>
#include<conio.h>
void main() Output:
{ Enter a Year:
int y; 2000
Year is a leap year
printf(“Eneter a Year:\n”);
scanf(“%d”,&y);
if((y%4==0 && y%100!=0)||(y%400==0))
printf(“Year is a leap year”);
else
printf(“Year is not a leap year”);
getch();
}
Program to convert an uppercase letter
into lowercase and vice-versa
#include<stdio.h> else if(ch>=’a’ && ch<=’z’)
#include<conio.h> {
void main() ch=ch-32;
{ printf(“%c”,ch);
char ch; }
printf(“Eneter a character:\n”); else
scanf(“%c”,&ch); printf(“Invalid Character”);
if(ch>=’A’ && ch<=’Z’ ) getch();
{ }
ch=ch+32;
printf(“%c”,ch); Output:
} Enter a character:
A
a
Program to find the largest number among
three numbers
#include<stdio.h> else
#include<conio.h> printf(“%d is largest”,c);
void main() getch();
{ }
int a,b,c;
printf(“Eneter three numbers:\n”);
scanf(“%d%d%d”,&a,&b,&c); Output:
if(a>b && a>c) Enter three numbers:
printf(“%d is largest”,a); 35
47
else if(b>a && b>c)
20
printf(“%d is largest”,b); 47 is largest
Program to check whether a number is
positive, negative or zero
#include<stdio.h> else
#include<conio.h> printf(“Number is Zero”);
void main() getch();
{ }
int n;
printf(“Eneter a Number:\n”);
Output:
scanf(“%d”,&n); Enter a Number:
if(n>0) 25
printf(“Number is Positive”); Number is Positive
else if(n<0)
printf(“Number is Positive”);
switch statement
syntax:
switch(expression)
{
case value-1: statement-block-1;
break;
case value-2: statement-block-2;
break;
case value-3: statement-block-3;
break;
default: default-block;
break;
}
statement-x;
Advantages and Limitations of Switch
• Advantages
• Easy to debug, read and understand
• Faster than else if ladder
• Limitations
• Cannot be used with real expression
• Logical operators are not allowed in cases
Write a program to simulate
calculator using switch statement.
#include<stdio.h> case ‘*’: c=a*b;
#include<conio.h> printf(“Multiplication=%f”,c);
void main() break;
{ case ‘/’: c=a/b;
float a,b,c; printf(“Division=%f”,c);
char op; break;
printf(“Enter two numbers:\n”); default: printf(“Invalid Operator”);
scanf(“%f%f”,&a,&b); break;
fflush(stdin); }
printf(“Enter Operator:\n”); getch();
scanf(“%c”,&op); }
switch(op)
{ Output:
case ‘+’: c=a+b; Enter two numbers:
printf(“Addition=%f”,c); 6
break; 5
case ‘-’: c=a-b; Enter Operator:
printf(“Subtraction=%f”,c); *
break; Multiplication=30
Write a menu driven program to
calculate the area of different
geometrical figures such as
rectangle, square, circle and triangle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area;
int n;
printf(“Enter:\n1:For rectangle\n2:For square\n3:For circle\n4:For triangle\n”);
scanf(“%d”,&n);
switch(n)
{
case 1: printf(“Enter length and breadth of rectangle:\n”);
scanf(“%f%f”,&a,&b);
area=a*b;
printf(“Area of Rectanle:%f”,area);
break;
case 2: printf(“Enter the side of square:\n”);
scanf(“%f”,&a);
area=a*a;
printf(“Area of Square:%f”,area);
break;
case 3: printf(“Enter the radius of circle:\n”);
scanf(“%f”,&a);
area= 3.14*a*a;
printf(“Area of Circle:%f”,area);
break;
void main()
{
int x=5;
if(x)
printf(“It is True”);
else
printf(“It is False”);
}
Output: It is True
Quiz
• What is the output of this C code?
void main()
{
int x=10,y=100%90;
if(x!=y);
printf(“x=%d,y=%d”,x,y);
}
Output: x=10, y=10
Programs for Hands-on
Program
• A company insures its drivers in the following
cases:
– If the driver is married
– If the driver is unmarried, male and above 30
years age
– If the driver is unmarried and above 25 years age
– In all other cases the driver is not insured.
Write a C program without using logical operators to
determine whether the driver is insured or not.
Program
• What is the advantage of a switch statement
over an if else statement? Write a program in
C using switch statement to find the value of Y
for a given N between 1 and 4.
Program Loops and Iterations
6/24/2024
Loop
• Are used to repeat the execution of
statements
• There are two types of loop
• Entry Controlled loop
• for
• while
• Exit Controlled loop
• do while
6/24/2024
for Loop
Syntax: Example:
6/24/2024
while Loop
Syntax: Example:
while(condition) #include<stdio.h>
{ #include<conio.h>
void main()
Body of loop {
} int i=1;
while(i<=10)
{
printf(“%d”,i);
i++
}
getch();
}
6/24/2024
do while Loop
Syntax: Example:
do #include<stdio.h>
{ #include<conio.h>
void main()
Body of loop
{
} int i=1;
while(condition); do
{
printf(“%d”,i);
i++
}
while(i<=10);
getch();
}
6/24/2024
Entry Controlled Loop
Entry
Control
Condition
False
True
Body of Loop
Statement-x
6/24/2024
Exit Controlled Loop
Entry
Body of Loop
True Control
Condition
False
Statement-x
6/24/2024
Programs Using Loops
6/24/2024
Program to find the factorial of a Number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i; Output:
long int f=1; Enter a number:
5
printf(“Eneter a number:\n”); Factorial of 5=120
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf(“Factorial of %d=%ld”,n,f);
getch();
}
6/24/2024
Program to check whether an integer number
is Prime number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
printf(“Eneter a number:\n”); Output:
scanf(“%d”,&n); Enter a number:
for(i=1;i<=n;i++) 7
{ Number is Prime Number
if(n%i==0)
c++;
}
if(c==2)
printf(“Number is Prime Number”);
else
printf(“Number is not Prime Numbe”);
getch();
}
6/24/2024
Write a program to print all Prime number
between 1 and r.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c,r;
printf(“Enter the value of r:\n”);
scanf(“%d”,&r);
for(n=2;n<r;n++)
{
c=0;
for(i=1;i<=n;i++) Output:
{ Enter the value of r:
if(n%i==0) 15
c++; 2 3 5 7 11 13
}
if(c==2)
printf(“%d\t”,n);
}
getch();
}
6/24/2024
Program to find the sum of digits of an integer
number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0; Output:
printf(“Eneter a number:\n”); Enter a number:
254
scanf(“%d”,&n); The Sum of digits=11
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
printf(“The Sum of digits=%d”,s);
getch();
}
6/24/2024
Program to find the reverse of an integer
number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0; Output:
printf(“Eneter a number:\n”); Enter a number:
254
scanf(“%d”,&n); The Reverse of Number=452
while(n>0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
printf(“The Reverse of Number=%d”,s);
getch();
}
6/24/2024
Program to check whether an integer number
is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0,t;
printf(“Eneter a number:\n”); Output:
scanf(“%d”,&n); Enter a number:
t=n; 252
while(n>0) Number is Palindrome Number
{
d=n%10;
s=s*10+d;
n=n/10;
}
if(s==t)
printf(“Number is Palindrome Number”);
else
printf(“Number is not Palindrome Number”);
getch();
}
6/24/2024
break and continue Statements
6/24/2024
break Statement
• It is used to terminate the execution of loop
• It transfer the control to the first statement
after loop
6/24/2024
break Statement Syntax and Example
Syntax: Example:
for(initialization; condition; updation) #include<stdio.h>
{ #include<conio.h>
statement-1; void main()
statement-2; {
if(condition) int i;
{ for(i=1;i<=10;i++)
break; {
} if(i==5)
statement-3; break;
} printf(“%d”,i);
statement-x; }
getch();
}
6/24/2024
continue Statement
• It is used to transfer the control to the
updation in for loop and to the condition in
while and do while loop
6/24/2024
continue Statement Syntax and
Example
Syntax: Example:
for(initialization; condition; updation) #include<stdio.h>
{ #include<conio.h>
statement-1; void main()
statement-2; {
if(condition) int i;
{ for(i=1;i<=10;i++)
continue; {
} if(i==5)
statement-3; continue;
} printf(“%d”,i);
statement-x; }
getch();
}
6/24/2024
Nested Loops
6/24/2024
Nested Loops
• Loop inside loop
Syntax:
for(initialization; condition; updation)
{
for(initialization; condition; updation)
{
statement-1;
}
statement-2;
}
6/24/2024
Example of Nested Loops
#include<stdio.h>
#include<conio.h>
void main()
{ Output:
int i, j; 1
for(i=1;i<=5;i++) 2 2
{ 3 3 3
for(j=1;j<=i; j++) 4 4 4 4
5 5 5 5 5
{
printf(“%d”,i);
}
printf(“\n”);
}
getch();
}
6/24/2024
Multi#include<stdio.h>
Loop Variables
#include<conio.h>
void main()
{
Output:
int i, j; 1 5
for(i=1,j=5; i<=5; i++,j--) 2 4
3 3
{ 4 2
printf(“%d\t%d\n”,i,j); 5 1
}
getch();
}
6/24/2024
QUIZ
6/24/2024
Quiz
• What is the output of this C code?
void main()
{
char a=1;
while(a<=255)
{
printf(“\t%d”,a);
a=a+1;
}
}
Output: 1 2 3 4 ………. 127 -128 -127…….. 0 1 ……..
6/24/2024
Quiz
• What is the output of this C code?
void main()
{
int i,j;
i=j=2,3;
while(--i && j++)
printf(“%d\t%d”,i,j);
}
Output: 1 3
6/24/2024
Quiz
• What is the output of this C code?
int main()
{
int i=1;
for(i=0;i=-1;i=1)
{
printf("%d ",i);
if(i!=1) break;
}
}
Output: -1
6/24/2024
Quiz
• What is the output/error of this C code?
void main()
{
for( ; ; )
{
printf("%d ",10);
}
}
Output: Infinite Loop
6/24/2024
Programs for Hands-on
Program
• Write a code to calculate and print the next
two numbers in each of the following series:
1. 0, 5, 10, 15, 20, 25, ?, ?.
2. 0, 2, 4, 6, 8, 10, ?, ?.
3. 1, 2, 4, 8, 16, 32, ?.
• Differentiate between while do and do while
loops.
6/24/2024
Program
• Write a program in C to read five-digit
number; if it is even then add the digits,
otherwise multiply them. Then print the
result.
6/24/2024
Program
• Given a number, write a program in C using
while loop to reverse the digits of the number.
For example, the number12345 should be
outputted as 54321.
6/24/2024
Program
• Write a program to find and print all the prime numbers
between 0 and entered number n.
• Write a C program to print the nth Fibonacci number.
• Write a program to determine whether a given integer is a
perfect number or not. A number is said to be a perfect
number if the sum of factors is equal to the number itself.
For example the factors of 6 are 1, 2, and 3 whose sum is
1+2+3=6.
• Write a C program to find the sum of following series up to
50 terms.
S=-13 + 33 – 53 + 73 + .............. 50 terms.
• Write a program in C language to generate the given series
up to terms less than 200.
1 – 4 + 9 – 16 + 25........
6/24/2024
Program
• Write a C program to generate the following
pattern.
* 1 1 *
** 23 12 ***
*** 456 123 *****
**** 7 8 9 10 1234 *******
54321 1
5432 121
543 12321
54 1234321
5
6/24/2024