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