Program 1: WAP to print Positive or Negative Number using if…else.
#include<stdio.h>
int main(){
int a;
printf("Enter Number:");
scanf("%d", &a);
scanf("%d", &a);
if(a >= 0)
{
printf("Positive Number");
}
else
{
printf("Negative Number");
}
return 0;
}
{
printf("Positive Number");
}
else
{
printf("Negative Number");
}
return 0;
}
Output:
Enter Number:10
Positive Number
Program 2: WAP to print Odd or Even Number using if…else.
#include<stdio.h>
void main()
{
int a;
clrscr();
printf("Enter Number:");
scanf("%d", &a);
if(a%2 == 0)
{
printf("Even Number");
}
else
{
printf("Odd Number");
}
getch();
}
Output:
Enter Number:11
Odd Number
Program 3: WAP to find largest number from given 2 numbers using if else.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter value of a :");
scanf("%d", &a);
printf("Enter value of b :");
scanf("%d", &b);
if(a > b)
{
printf("%d is largest", a);
}
else
{
printf("%d is largest", b);
}
getch();
}
Output:
Enter value of a :10
Enter value of b :20
20 is largest
Program 4: WAP to print minimum from given three numbers. (Using Nested If Else Structure).
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter value of a :");
scanf("%d", &a);
printf("Enter value of b :");
scanf("%d", &b);
printf("Enter value of c :");
scanf("%d", &c);
if(a<b)
{
if(a<c)
{
printf("%d is min", a);
}
else
{
printf("%d is min", c);
}
}
else
{
if(b<c)
{
printf("%d is min", b);
}
else
{
printf("%d is min", c);
}
}
return 0;
}
Output:
Enter value of a :10
Enter value of b :5
Enter value of c :20
5 is min
Program 5: WAP to print maximum from given three numbers. (Using Nested If Else Structure).
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter value of a :");
scanf("%d", &a);
printf("Enter value of b :");
scanf("%d", &b);
printf("Enter value of c :");
scanf("%d", &c);
if(a>b)
{
if(a>c)
{
printf("%d is max", a);
}
else
{
printf("%d is max", c);
}
}
else
{
if(b>c)
{
printf("%d is max", b);
}
else
{
printf("%d is max", c);
}
}
return 0;
}
Output:
Enter value of a :10
Enter value of b :5
Enter value of c :20
20 is max
Program 6: WAP to check that a number is Zero, Positive or Negative Number. (Using Else If Ladder)
#include<stdio.h>
int main()
{
int a;
printf("Enter Number:");
scanf("%d", &a);
if(a>0)
{
printf("\nPositive Number");
}
else if(a<0)
{
printf("\nNegative Number");
}
else
{
printf("\nZero");
}
return 0;
}
Output:
Enter a Character: -5
Negative Number
Program 7: WAP to find maximum number from 3 numbers. (Using Else If Ladder)
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter Number a:");
scanf("%d", &a);
printf("Enter Number b:");
scanf("%d", &b);
printf("Enter Number c:");
scanf("%d", &c);
if(a>b && a>c)
{
printf("\na is maximum");
}
else if(b>a && b>c)
{
printf("\nb is maximum");
}
else
{
printf("\nc is maximum");
}
return 0;
}
Output:
Output:
Enter Number a: 8
Enter Number b: 19
Enter Number c: 13
b is maximum
Program 8: Write a C program to check whether the entered character is capital, small letter, digit or any special character. (Use ASCII Value)
#include<stdio.h>
int main()
{
char ch;
printf("Enter a Character: ");
scanf("%c", &ch);
if(ch>=65 && ch<=90)
{
printf("\n Upper case letter”);
}
else if(ch>=97 && ch<=122)
{
printf("\n Lower case letter");
}
else if(ch>=48 && ch<=57)
{
printf("\n Digit");
}
else
{
printf("\n Special Symbol");
}
return 0;
}
Output:
Enter a Character: z
Lower case letter
Program 9: Write a C program to check whether the entered character is capital, small letter, digit or any special character. (Do not use ASCII value)
#include<stdio.h>
int main()
{
char ch;
printf("Enter a Character: ");
scanf("%c", &ch);
if(ch>='A' && ch<='Z')
{
printf("\n Upper case letter”);
}
else if(ch>='a' && ch<='z')
{
printf("\n Lower case letter");
}
else if(ch>='0' && ch<='9')
{
printf("\n Digit");
}
else
{
printf("\n Special Symbol");
}
return 0;
}
Output:
Enter a Character: P
Upper case letter
Program 10: Write a program to read marks from keyboard and your program should display equivalent grade according to following table (using if else ladder)
Marks Grade
100 - 80 = Distinction
80 - 60 = First Class
60 - 40 = Second Class
< 40 = Fail
#include<stdio.h>
int main()
{
int m, p, c, total;
float per;
printf("Enter marks of Mathematics: ");
scanf("%d", &m);
printf("Enter marks of Physics: ");
scanf("%d", &p);
printf("Enter marks of Chemistry: ");
scanf("%d", &c);
total = m + p + c;
per = (float) (total*100) / 300;
if( m<0 || m>100 || p<0 || p>100 || c<0 || c>100)
{
printf("\nEnter correct values of subject marks");
}
else
{
printf("\nTotal = %d\nPercentage = %f \n", total, per);
if(per>=80)
{
printf("You got Distinction");
}
else if(per>=60)
{
printf("You got 1st class");
}
else if(per>=40)
{
printf("You got 2nd class");
}
else
{
printf("You are Fail");
}
}
return 0;
}
Output:
Enter marks of Mathematics: 80
Enter marks of Physics: 70
Enter marks of Chemistry: 90
Total = 240
Percentage = 80.000000
You got Distinction
Program 11: WAP to print traffic signal colors using switch case.
#include<stdio.h>
int main()
{
char signal;
printf("Enter a character(r|y|g): ");
scanf("%c", &signal);
switch(signal)
{
case ’r’:
printf(“Signal Color = Red");
break;
case ’y’:
printf(“Signal Color = Yellow");
break;
case ’g’:
printf(“Signal Color = Green");
break;
default:
printf("Wrong input");
break;
}
return 0;
}
Output:
Enter a character(r|y|g): g
Signal Color = Green
Program 12: Write a program to read marks from keyboard and your program should display equivalent grade according to following table (using if else ladder)
Marks Grade
100 - 80 = Distinction
80 - 60 = First Class
60 - 40 = Second Class
< 40 = Fail
#include<stdio.h>
int main()
{
int m, p, c, total;
float per;
printf("Enter marks of Mathematics: ");
scanf("%d", &m);
printf("Enter marks of Physics: ");
scanf("%d", &p);
printf("Enter marks of Chemistry: ");
scanf("%d", &c);
total = m + p + c;
per = (float) (total*100) / 300;
if( m<0 || m>100 || p<0 || p>100 || c<0 || c>100)
{
printf("\nEnter correct values of subject marks");
}
else
{
printf("\nTotal = %d\n" , total);
printf("Percentage = %f\n", per);
a = per/10;
switch(a)
{
case 10:
case 9:
case 8:
printf("grade = Distinction");
break;
case 7:
case 6:
printf("grade = 1st class");
break;
case 5:
case 4:
printf("grade = 2nd class");
break;
default:
printf("Student is failed");
}
}
return 0;
}
Output:
Enter marks of Mathematics: 80
Enter marks of Physics: 70
Enter marks of Chemistry: 90
Total = 240
Percentage = 80.000000
grade = Distinction
Program 13: WAP to print 1 to n. (Using while loop).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter Number :");
scanf("%d", &n);
while (i<=n)
{
printf("%d\t", i);
i = i + 1;
}
return 0;
}
Output:
Enter Number : 5
1 2 3 4 5
Program 14: WAP to print 1 to n. (Using for loop).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter Number :");
scanf("%d", &n);
for ( i = 1 ; i<=n ; i++)
{
printf("%d\t", i);
}
return 0;
}
Output:
Enter Number : 5
1 2 3 4 5
Program 15: WAP to print 1 to n. (Using do while loop).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter Number :");
scanf("%d", &n);
do
{
printf("%d\t", i);
i = i + 1;
} while(i<=n);
return 0;
}
Output:
Enter Number : 5
1 2 3 4 5
Program 16: WAP to print number between range. (Using while loop).
#include<stdio.h>
int main()
{
int i, n1, n2;
printf("Enter Starting Number : ");
scanf("%d", &n1);
printf("Enter Ending Number : ");
scanf("%d", &n2);
i=n1;
while (i<=n2)
{
printf("%d\t", i);
i=i+1;
}
return 0;
}
Output:
Enter Starting Number : 10
Enter Ending Number : 15
10 11 12 13 14 15
Program 17: WAP to print sum of 1 to n(Using while loop).
#include<stdio.h>
int main()
{
int i=1, n, sum=0;
printf("Enter Number :");
scanf("%d", &n);
while (i<=n)
{
sum = sum + i;
i = i + 1;
}
printf("sum = %d", sum);
return 0;
}
Output:
Enter Number : 5
Sum = 15
Program 18: WAP to print alphabets a to z (Using while loop).
#include<stdio.h>
int main()
{
char ch='a';
while (ch<='z')
{
printf("%c\t", ch);
ch++;
}
return 0;
}
Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z
Program 19: WAP to print reverse a number. (Using do while loop).
#include<stdio.h>
int main()
{
int a=0, n;
printf("Enter a number: ");
scanf("%d", &n);
do
{
a=a*10+n%10;
n=n/10;
} while (n!=0);
printf("%d", a);
return 0;
}
Output:
Enter a number: 123
321
Program 20: Demonstrate use of forward jump using goto statement.
#include<stdio.h>
int main()
{
printf("hello ");
goto label;
printf("How are");
label:
printf("you");
return 0;
}
Output:
Hello you
Program 21: Demonstrate use of backward jump using goto statement.
#include<stdio.h>
int main()
{
printf("hello");
label:
printf("\nHow are");
goto label;
printf("\nyou");
return 0;
}
Output:
Hello
How are
How are
How are
.
.
Program 22: WAP to print square root of a number if number is positive, if number entered by user is negative then ask user to enter number.
#include<stdio.h>
#include<math.h>
int main()
{
float x, y;
read:
printf("enter a number : ");
scanf("%f", &x);
if(x<0)
{
goto read;
}
y=sqrt(x);
printf("square root of %f = %f", x, y);
return 0;
}
Output:
enter a number : 25
square root of 25.000000 = 5.000000
Program 23: WAP to print Odd numbers between 1 to n. (using while loop).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter a number: ");
scanf("%d", &n);
while(i<=n)
{
if(i%2==1)
{
printf("%d\t", i);
}
i++;
}
return 0;
}
Output:
Enter a number : 10
1 3 5 7 9
Program 24: WAP to print Odd numbers between 1 to n. (using goto statement).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter a number: ");
scanf("%d", &n);
odd:
if(i%2==1)
{
printf("%d\t", i);
}
i=i+1;
if(i<=n)
{
goto odd;
}
return 0;
}
Output:
Enter a number : 10
1 3 5 7 9
Program 25: WAP to print even numbers between 1 to n. (using goto statement).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter a number: ");
scanf("%d", &n);
even:
if(i%2==0)
{
printf("%d\t", i);
}
i=i+1;
if(i<=n)
{
goto even;
}
return 0;
}
Output:
Enter a number : 10
2 4 6 8 10
Program 26: WAP to print sum of Odd numbers between 1 to n(while loop).
#include<stdio.h>
int main()
{
int i=1, n, sum=0;
printf("Enter a Number: ");
scanf("%d", &n);
while (i<=n)
{
if(i%2 != 0)
{
sum=sum+i;
}
i++;
}
printf("sum = %d", sum);
return 0;
}
Output:
Enter a Number: 10
sum = 25
Program 27: Write a C program to find average of maximum of n positive numbers entered by user. But, if the input is negative, display the average(excluding the average of negative input) and end the program.
#include<stdio.h>
int main()
{
int i, n;
float num, average, sum=0;
printf(“\nMaximum no. of inputs: ");
scanf("%d", &n);
for(i=1 ; i<=n ; ++i)
{
printf("\nEnter n%d: ",i);
scanf("%f", &num);
if(num<0.0)
{
break;
}
sum=sum+num;
}
average = sum/(i-1);
printf("\nAverage=%.2f",average);
return 0;
}
Output:
Maximum no. of inputs: 10
Enter n1:10
Enter n2:10
Enter n3:10
Enter n4:-10
Average=10.00
Program 28: Write a C program to find sum of 5 numbers entered by user. But, if the input is negative, neglect it, display the sum of positive value (excluding the negative input) and end the program.
#include<stdio.h>
int main()
{
int i;
float num, sum=0;
for( i=1 ; i<=5 ; i++)
{
printf("\nEnter n%d: ",i);
scanf("%f", &num);
if(num<0.0)
{
continue;
}
sum=sum+num;
}
printf("\nsum=%.2f",sum);
return 0;
}
Output:
Enter n1: 10
Enter n2: -5
Enter n3: 10
Enter n4: -20
Enter n5: 10
sum=30.00
Program 29: WAP to find factors of a number(while loop).
#include<stdio.h>
int main()
{
int i=1, n;
printf("Enter n to find factors=");
scanf("%d", &n);
while (i<=n)
{
if(n%i == 0)
{
printf("%d\t", i);
}
i++;
}
return 0;
}
Output:
Enter n to find factors=12
1,2,3,4,6,12,
Program 30: WAP to check given number is prime or not(while loop).
#include<stdio.h>
int main()
{
int i=2, n, flag=0;
printf("Enter a number : ");
scanf("%d", &n);
if(n==0 || n==1)
{
flag = 1;
}
while (i<n)
{
if(n%i==0)
{
flag = 1;
break;
}
i++;
}
if(flag==0)
{
printf("%d is a prime number", n);
}
else
{
printf("%d is not a prime number", n);
}
return 0;
}
Output:
Enter a number:7
7 is a prime number
Program 31: WAP to check given number is perfect or not(while loop).
#include<stdio.h>
int main()
{
int i=0,n,sum=0;
printf("Enter a number : ");
scanf("%d", &n);
while (i<n)
{
if(n%i==0)
{
printf("%d+ ", i);
sum = sum + i;
}
i++;
}
printf("\b=%d", sum);
if(sum==n)
{
printf("\n%d is a perfect number", n);
}
else
{
printf("\n%d is not a perfect number", n);
}
return 0;
}
Output:
Enter a number : 6
1+2+3=6
6 is a perfect number
Program 32: WAP to print sum of digits of a given number (while loop).
#include<stdio.h>
int main()
{
int a, n, sum=0;
printf("Enter a number : ");
scanf("%d", &n);
while (n!=0)
{
a=n%10;
printf("%d+", a);
sum=sum+a;
n=n/10;
}
printf("\b=%d", sum);
return 0;
}
Output:
Enter a number : 1234
4+3+2+1=10
Program 33: Write a program to check whether a number is Armstrong numbers or not (for 3 digit number).
#include<stdio.h>
int main()
{
int n, sum=0, original, r;
printf("Enter a number(3 digits): ");
scanf("%d", &n);
original=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum == original)
{
printf("%d=Armstrong number", original);
}
else
{
printf("%d=not Armstrong number", original);
}
return 0;
}
Output:
Enter a number : 370
370 = Armstrong number
0, 1, 153, 370, 371, 407
Program 34: WAP to print reverse a number(while loop).
#include<stdio.h>
int main()
{
int a=0,n;
printf("Enter a number : ");
scanf("%d", &n);
while (n!=0)
{
a = a*10 + n%10;
n = n/10;
}
printf("%d", a);
return 0;
}
Output:
Enter a number : 1234
4321
Program 35: WAP to check that a number is Palindrome or not (while loop).
#include<stdio.h>
int main()
{
int a=0,original,n;
printf("Enter a number : ");
scanf("%d", &n);
original=n;
while (n!=0)
{
a=a*10 + n%10;
n=n/10;
}
if( a == original)
{
printf("%d is palindrome", original);
}
else
{
printf("%d is not palindrome", original);
}
return 0;
}
Output:
Enter a number : 1331
1331 is palindrome
Program 36: Write a C program to find factorial of a given number.
#include<stdio.h>
int main()
{
int n, i;
long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
fact = fact* i;
}
printf("Factorial = %ld", n, fact);
return 0;
}
Output:
Enter an integer: 5
Factorial = 120