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)
{
statement-1;
………………..
………………..
statement-n;
}
statement-x;
test
expression
Statement Block
Statement-x
False
True
Statement Block
if else statement
syntax:
if(test expression)
{
statement block-1
}
else
{
statement block-2;
}
statement-x;
test
expression
Statement block-2 Statement block-1
Statement-x
False True
Nested if else statement
syntax:
if(test expression-1)
{
if(test expression-2)
{
statement block-1
}
else
{
statement block-2
}
}
else
{
if(test expression-3)
{
statement block-3
}
else
{
statement block-4
}
}
Statement-x;
test
expression-1
test
expressi
on-3
test
expressi
on-2
Statement
block-2 Statement
block-1
Statement-x
Statement
block-4 Statement
block-3
False
False
False
True
True
True
else if ladder
syntax:
if(test expression-1)
{
Statement block-1
}
else if(test expression-2)
{
Statement block-2
}
else
{
statement block-3
}
Statement-x;
test
expression-
1
statement
block-1
test
expressio
n-2
statement
block-2
statement
block-3
statement
block-1
False
False
True
True
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);
if(n%2==0)
printf(“Number is Even”);
else
printf(“Number is odd”);
getch();
}
Output:
Enter a Number:
12
Number is Even
Program to check whether a year is a leap
year or not
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
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();
}
Output:
Enter a Year:
2000
Year is a leap year
Program to convert an uppercase letter
into lowercase and vice-versa
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“Eneter a character:n”);
scanf(“%c”,&ch);
if(ch>=’A’ && ch<=’Z’ )
{
ch=ch+32;
printf(“%c”,ch);
}
else if(ch>=’a’ && ch<=’z’)
{
ch=ch-32;
printf(“%c”,ch);
}
else
printf(“Invalid Character”);
getch();
}
Output:
Enter a character:
A
a
Program to find the largest number among
three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(“Eneter three numbers:n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
printf(“%d is largest”,a);
else if(b>a && b>c)
printf(“%d is largest”,b);
else
printf(“%d is largest”,c);
getch();
}
Output:
Enter three numbers:
35
47
20
47 is largest
Program to check whether a number is
positive, negative or zero
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Eneter a Number:n”);
scanf(“%d”,&n);
if(n>0)
printf(“Number is Positive”);
else if(n<0)
printf(“Number is Positive”);
else
printf(“Number is Zero”);
getch();
}
Output:
Enter a Number:
25
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>
#include<conio.h>
void main()
{
float a,b,c;
char op;
printf(“Enter two numbers:n”);
scanf(“%f%f”,&a,&b);
fflush(stdin);
printf(“Enter Operator:n”);
scanf(“%c”,&op);
switch(op)
{
case ‘+’: c=a+b;
printf(“Addition=%f”,c);
break;
case ‘-’: c=a-b;
printf(“Subtraction=%f”,c);
break;
case ‘*’: c=a*b;
printf(“Multiplication=%f”,c);
break;
case ‘/’: c=a/b;
printf(“Division=%f”,c);
break;
default: printf(“Invalid Operator”);
break;
}
getch();
}
Output:
Enter two numbers:
6
5
Enter Operator:
*
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 rectanglen2:For squaren3:For circlen4:For trianglen”);
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;
case 4: printf(“Enter the three sides of triangle:n”);
scanf(“%f%f%f”,&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“Area of Triangle:%f”,area);
break;
default: printf(“Invalide Choice”);
break;
}
getch();
}
Output
Enter:
1:For rectangle
2:For square
3:For circle
4:For triangle
3
Enter the radius of circle:
5
Area of Circle:78.5
goto Statement
Syntax:
statement-1;
if(condition)
{
goto label;
}
statement-2;
statement-3;
label:
statement-4;
statement-5;
Syntax:
statement-1;
label:
statement-2;
statement-3;
if(condition)
{
goto label;
}
statement-4;
statement-5;
Program to print 1 to 10 numbers using
goto statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
start:
printf(“%d”,i);
if(i<10)
{
i++;
goto start;
}
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10
QUIZ
Quiz
void main()
{
int n, x = 1,y = 1;
if(n>0)
x = x+1;
y = y-1;
printf(“%d,%d”,x,y);
}
What will be the value of x and y if the value of n is (a) 1
and (b) 0
Ans: (a) 2 0
(b) 1 0
Quiz
What is the output of this C code?
void main()
{
int a = 100;
if(a>10)
printf(“Greater than 10”);
else if(a>20)
printf(“Greater than 20”);
else
printf(“Greater than 50”);
}
Output: Greater than 10
Quiz
• What is the output of this C code?
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:
for(initialization; condition; iteration)
{
Body of loop
}
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf(“%d”,i);
}
getch();
}
6/24/2024
while Loop
Syntax:
while(condition)
{
Body of loop
}
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
{
printf(“%d”,i);
i++
}
getch();
}
6/24/2024
do while Loop
Syntax:
do
{
Body of loop
}
while(condition);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
do
{
printf(“%d”,i);
i++
}
while(i<=10);
getch();
}
6/24/2024
Entry Controlled Loop
Control
Condition
Body of Loop
Statement-x
True
False
Entry
6/24/2024
Exit Controlled Loop
Body of Loop
Statement-x
True
False
Entry
Control
Condition
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;
long int f=1;
printf(“Eneter a number:n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf(“Factorial of %d=%ld”,n,f);
getch();
}
Output:
Enter a number:
5
Factorial of 5=120
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”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf(“Number is Prime Number”);
else
printf(“Number is not Prime Numbe”);
getch();
}
Output:
Enter a number:
7
Number is Prime Number
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++)
{
if(n%i==0)
c++;
}
if(c==2)
printf(“%dt”,n);
}
getch();
}
Output:
Enter the value of r:
15
2 3 5 7 11 13
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;
printf(“Eneter a number:n”);
scanf(“%d”,&n);
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
printf(“The Sum of digits=%d”,s);
getch();
}
Output:
Enter a number:
254
The Sum of digits=11
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;
printf(“Eneter a number:n”);
scanf(“%d”,&n);
while(n>0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
printf(“The Reverse of Number=%d”,s);
getch();
}
Output:
Enter a number:
254
The Reverse of Number=452
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”);
scanf(“%d”,&n);
t=n;
while(n>0)
{
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();
}
Output:
Enter a number:
252
Number is Palindrome Number
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:
for(initialization; condition; updation)
{
statement-1;
statement-2;
if(condition)
{
break;
}
statement-3;
}
statement-x;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
break;
printf(“%d”,i);
}
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:
for(initialization; condition; updation)
{
statement-1;
statement-2;
if(condition)
{
continue;
}
statement-3;
}
statement-x;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
continue;
printf(“%d”,i);
}
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()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i; j++)
{
printf(“%d”,i);
}
printf(“n”);
}
getch();
}
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6/24/2024
Multi Loop Variables
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
for(i=1,j=5; i<=5; i++,j--)
{
printf(“%dt%dn”,i,j);
}
getch();
}
Output:
1 5
2 4
3 3
4 2
5 1
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(“%dt%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.
6/24/2024
*
***
*****
*******
1
1 2
1 2 3
1 2 3 4
1
2 3
4 5 6
7 8 9 10
*
**
***
****
1
121
12321
1234321
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 2.pdf

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
    Types of ConditionalStatements • if statement • switch statement • goto statement • Conditional operator
  • 6.
    if Statement • ifstatement can be used as- • Simple if statement • if else statement • Nested if else statement • else if ladder
  • 7.
    Simple if Statement syntax: if(testexpression) { statement-1; ……………….. ……………….. statement-n; } statement-x; test expression Statement Block Statement-x False True Statement Block
  • 8.
    if else statement syntax: if(testexpression) { statement block-1 } else { statement block-2; } statement-x; test expression Statement block-2 Statement block-1 Statement-x False True
  • 9.
    Nested if elsestatement syntax: if(test expression-1) { if(test expression-2) { statement block-1 } else { statement block-2 } } else { if(test expression-3) { statement block-3 } else { statement block-4 } } Statement-x; test expression-1 test expressi on-3 test expressi on-2 Statement block-2 Statement block-1 Statement-x Statement block-4 Statement block-3 False False False True True True
  • 10.
    else if ladder syntax: if(testexpression-1) { Statement block-1 } else if(test expression-2) { Statement block-2 } else { statement block-3 } Statement-x; test expression- 1 statement block-1 test expressio n-2 statement block-2 statement block-3 statement block-1 False False True True
  • 11.
    Program to checkwhether a number is even or odd #include<stdio.h> #include<conio.h> void main() { int n; printf(“Eneter a Number:n”); scanf(“%d”,&n); if(n%2==0) printf(“Number is Even”); else printf(“Number is odd”); getch(); } Output: Enter a Number: 12 Number is Even
  • 12.
    Program to checkwhether a year is a leap year or not #include<stdio.h> #include<conio.h> void main() { int y; 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(); } Output: Enter a Year: 2000 Year is a leap year
  • 13.
    Program to convertan uppercase letter into lowercase and vice-versa #include<stdio.h> #include<conio.h> void main() { char ch; printf(“Eneter a character:n”); scanf(“%c”,&ch); if(ch>=’A’ && ch<=’Z’ ) { ch=ch+32; printf(“%c”,ch); } else if(ch>=’a’ && ch<=’z’) { ch=ch-32; printf(“%c”,ch); } else printf(“Invalid Character”); getch(); } Output: Enter a character: A a
  • 14.
    Program to findthe largest number among three numbers #include<stdio.h> #include<conio.h> void main() { int a,b,c; printf(“Eneter three numbers:n”); scanf(“%d%d%d”,&a,&b,&c); if(a>b && a>c) printf(“%d is largest”,a); else if(b>a && b>c) printf(“%d is largest”,b); else printf(“%d is largest”,c); getch(); } Output: Enter three numbers: 35 47 20 47 is largest
  • 15.
    Program to checkwhether a number is positive, negative or zero #include<stdio.h> #include<conio.h> void main() { int n; printf(“Eneter a Number:n”); scanf(“%d”,&n); if(n>0) printf(“Number is Positive”); else if(n<0) printf(“Number is Positive”); else printf(“Number is Zero”); getch(); } Output: Enter a Number: 25 Number is Positive
  • 16.
    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;
  • 17.
    Advantages and Limitationsof 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
  • 18.
    Write a programto simulate calculator using switch statement.
  • 19.
    #include<stdio.h> #include<conio.h> void main() { float a,b,c; charop; printf(“Enter two numbers:n”); scanf(“%f%f”,&a,&b); fflush(stdin); printf(“Enter Operator:n”); scanf(“%c”,&op); switch(op) { case ‘+’: c=a+b; printf(“Addition=%f”,c); break; case ‘-’: c=a-b; printf(“Subtraction=%f”,c); break; case ‘*’: c=a*b; printf(“Multiplication=%f”,c); break; case ‘/’: c=a/b; printf(“Division=%f”,c); break; default: printf(“Invalid Operator”); break; } getch(); } Output: Enter two numbers: 6 5 Enter Operator: * Multiplication=30
  • 20.
    Write a menudriven program to calculate the area of different geometrical figures such as rectangle, square, circle and triangle.
  • 21.
    #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,s,area; intn; printf(“Enter:n1:For rectanglen2:For squaren3:For circlen4:For trianglen”); 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;
  • 22.
    case 3: printf(“Enterthe radius of circle:n”); scanf(“%f”,&a); area= 3.14*a*a; printf(“Area of Circle:%f”,area); break; case 4: printf(“Enter the three sides of triangle:n”); scanf(“%f%f%f”,&a,&b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“Area of Triangle:%f”,area); break; default: printf(“Invalide Choice”); break; } getch(); }
  • 23.
    Output Enter: 1:For rectangle 2:For square 3:Forcircle 4:For triangle 3 Enter the radius of circle: 5 Area of Circle:78.5
  • 24.
  • 25.
    Program to print1 to 10 numbers using goto statement #include<stdio.h> #include<conio.h> void main() { int i=1; start: printf(“%d”,i); if(i<10) { i++; goto start; } getch(); } Output: 1 2 3 4 5 6 7 8 9 10
  • 26.
  • 27.
    Quiz void main() { int n,x = 1,y = 1; if(n>0) x = x+1; y = y-1; printf(“%d,%d”,x,y); } What will be the value of x and y if the value of n is (a) 1 and (b) 0 Ans: (a) 2 0 (b) 1 0
  • 28.
    Quiz What is theoutput of this C code? void main() { int a = 100; if(a>10) printf(“Greater than 10”); else if(a>20) printf(“Greater than 20”); else printf(“Greater than 50”); } Output: Greater than 10
  • 29.
    Quiz • What isthe output of this C code? void main() { int x=5; if(x) printf(“It is True”); else printf(“It is False”); } Output: It is True
  • 30.
    Quiz • What isthe 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
  • 31.
  • 32.
    Program • A companyinsures 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.
  • 33.
    Program • What isthe 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.
  • 34.
    Program Loops andIterations 6/24/2024
  • 35.
    Loop • Are usedto repeat the execution of statements • There are two types of loop • Entry Controlled loop • for • while • Exit Controlled loop • do while 6/24/2024
  • 36.
    for Loop Syntax: for(initialization; condition;iteration) { Body of loop } Example: #include<stdio.h> #include<conio.h> void main() { int i; for(i=1;i<=10;i++) { printf(“%d”,i); } getch(); } 6/24/2024
  • 37.
    while Loop Syntax: while(condition) { Body ofloop } Example: #include<stdio.h> #include<conio.h> void main() { int i=1; while(i<=10) { printf(“%d”,i); i++ } getch(); } 6/24/2024
  • 38.
    do while Loop Syntax: do { Bodyof loop } while(condition); Example: #include<stdio.h> #include<conio.h> void main() { int i=1; do { printf(“%d”,i); i++ } while(i<=10); getch(); } 6/24/2024
  • 39.
    Entry Controlled Loop Control Condition Bodyof Loop Statement-x True False Entry 6/24/2024
  • 40.
    Exit Controlled Loop Bodyof Loop Statement-x True False Entry Control Condition 6/24/2024
  • 41.
  • 42.
    Program to findthe factorial of a Number #include<stdio.h> #include<conio.h> void main() { int n,i; long int f=1; printf(“Eneter a number:n”); scanf(“%d”,&n); for(i=1;i<=n;i++) { f=f*i; } printf(“Factorial of %d=%ld”,n,f); getch(); } Output: Enter a number: 5 Factorial of 5=120 6/24/2024
  • 43.
    Program to checkwhether 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”); scanf(“%d”,&n); for(i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) printf(“Number is Prime Number”); else printf(“Number is not Prime Numbe”); getch(); } Output: Enter a number: 7 Number is Prime Number 6/24/2024
  • 44.
    Write a programto 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++) { if(n%i==0) c++; } if(c==2) printf(“%dt”,n); } getch(); } Output: Enter the value of r: 15 2 3 5 7 11 13 6/24/2024
  • 45.
    Program to findthe sum of digits of an integer number. #include<stdio.h> #include<conio.h> void main() { int n,d,s=0; printf(“Eneter a number:n”); scanf(“%d”,&n); while(n>0) { d=n%10; s=s+d; n=n/10; } printf(“The Sum of digits=%d”,s); getch(); } Output: Enter a number: 254 The Sum of digits=11 6/24/2024
  • 46.
    Program to findthe reverse of an integer number. #include<stdio.h> #include<conio.h> void main() { int n,d,s=0; printf(“Eneter a number:n”); scanf(“%d”,&n); while(n>0) { d=n%10; s=s*10+d; n=n/10; } printf(“The Reverse of Number=%d”,s); getch(); } Output: Enter a number: 254 The Reverse of Number=452 6/24/2024
  • 47.
    Program to checkwhether 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”); scanf(“%d”,&n); t=n; while(n>0) { 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(); } Output: Enter a number: 252 Number is Palindrome Number 6/24/2024
  • 48.
    break and continueStatements 6/24/2024
  • 49.
    break Statement • Itis used to terminate the execution of loop • It transfer the control to the first statement after loop 6/24/2024
  • 50.
    break Statement Syntaxand Example Syntax: for(initialization; condition; updation) { statement-1; statement-2; if(condition) { break; } statement-3; } statement-x; Example: #include<stdio.h> #include<conio.h> void main() { int i; for(i=1;i<=10;i++) { if(i==5) break; printf(“%d”,i); } getch(); } 6/24/2024
  • 51.
    continue Statement • Itis used to transfer the control to the updation in for loop and to the condition in while and do while loop 6/24/2024
  • 52.
    continue Statement Syntaxand Example Syntax: for(initialization; condition; updation) { statement-1; statement-2; if(condition) { continue; } statement-3; } statement-x; Example: #include<stdio.h> #include<conio.h> void main() { int i; for(i=1;i<=10;i++) { if(i==5) continue; printf(“%d”,i); } getch(); } 6/24/2024
  • 53.
  • 54.
    Nested Loops • Loopinside loop Syntax: for(initialization; condition; updation) { for(initialization; condition; updation) { statement-1; } statement-2; } 6/24/2024
  • 55.
    Example of NestedLoops #include<stdio.h> #include<conio.h> void main() { int i, j; for(i=1;i<=5;i++) { for(j=1;j<=i; j++) { printf(“%d”,i); } printf(“n”); } getch(); } Output: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6/24/2024
  • 56.
    Multi Loop Variables #include<stdio.h> #include<conio.h> voidmain() { int i, j; for(i=1,j=5; i<=5; i++,j--) { printf(“%dt%dn”,i,j); } getch(); } Output: 1 5 2 4 3 3 4 2 5 1 6/24/2024
  • 57.
  • 58.
    Quiz • What isthe 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
  • 59.
    Quiz • What isthe output of this C code? void main() { int i,j; i=j=2,3; while(--i && j++) printf(“%dt%d”,i,j); } Output: 1 3 6/24/2024
  • 60.
    Quiz • What isthe 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
  • 61.
    Quiz • What isthe output/error of this C code? void main() { for( ; ; ) { printf("%d ",10); } } Output: Infinite Loop 6/24/2024
  • 62.
  • 63.
    Program • Write acode 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
  • 64.
    Program • Write aprogram 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
  • 65.
    Program • Given anumber, 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
  • 66.
    Program • Write aprogram 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
  • 67.
    Program • Write aC program to generate the following pattern. 6/24/2024 * *** ***** ******* 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 6 7 8 9 10 * ** *** **** 1 121 12321 1234321 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5