Unit 2 - SEE - QP - Programs
Unit 2 - SEE - QP - Programs
1. Demonstrate the usage of do-while to list all the leap year from 1900 to 2100. 8M
#include <stdio.h>
int main()
{
int year=1900;
printf("Leap years from 1900 to 2100:\n");
do
{
if( (year % 400==0)||(year%4==0 && year%100!=0) )
{
printf("%d ",year);
++year;
}
else
{
++year;
continue;
}
}while(year!=2100);
return 0;
}
#include <stdio.h>
void main()
{
int i, j, p=0;
for(i=1; i<= 4; i++)
{
for(j=1; j<=i; j++)
{
printf("%d",p);
p=p+1;
}
printf("\n");
}
}
3. WAP to find the factorial of a number using for loop. 8M
#include<stdio.h>
int main()
{
int i,fact=1,n;
printf("Enter a number to find factorial: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d is: %d",n,fact);
return 0;
}
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(k=5;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
return 0;
}
5. Analyze the following C code and predict the output. Justify the output. 6M
Output: 987654321
In the given code, initialization of loop variable is done before the for loop, until condition
becomes false, loop is executed and prints the value of num. Once num value becomes zero,
control exits out of the for loop.
6. WAP to calculate the GCD of two numbers. 6M
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}
7. WAP to demonstrate the use of while loop to calculate the sum of nos from m to n. 6M
#include <stdio.h>
int main()
{
int i,m,n,sum = 0;
8. WAP that accepts a number from 1 to 10 and prints whether the number is even or odd using a
switch case construct. 8M
#include <stdio.h>
int main()
{
int num;
printf("Enter any number (1 to 10) to check even or odd: ");
scanf("%d", &num);
switch(num % 2)
{
case 0:
printf("Number is Even");
break;
case 1:
printf("Number is Odd");
break;
}
return 0;
}
Output:
1+1=2
2+1=3
3+1=4
4+1=5
5+1=6
1+1=2
2+2=4
3+3=6
4+4=8
5+5=10
10. WAP using do-while loop to check number n is prime number or not. 8M
#include<stdio.h>
#include<math.h>
void main()
{
int n, i, flag=0;
printf("\n Enter a positive integer value: ");
scanf("%d",&n);
do
{
if((n!=2) && (n%i==0))
{
flag=1;
break;
}
i++;
}while(i<=sqrt(n));
if (flag==0)
printf("\n %d is a prime number.",n);
else
printf("\n %d is not a prime number.",n);
}
11. If the ages of Ram, Sham and Ajay are input through the keyboard, WAP to determine the
youngest of the three. 8M
#include<stdio.h>
void main()
{
int Ram,Shyam,Ajay;
printf("Enter the Ages of Ram,Shyam,Ajay");
scanf("%d%d%d",&Ram,&Shyam,&Ajay);
if(Ram<Shyam && Ram<Ajay)
{
printf("Ram is youngest");
}
if(Shyam<Ram && Shyam<Ajay)
{
printf("Shyam is youngest");
}
else if(Ajay<Ram && Ajay<Shyam)
{
printf("Ajay is youngest");
}
}
#include<stdio.h>
int main ()
{
int num1, num2;
float result;
char ch;
printf ("Choose operator to perform operation (+, *, *, /): ");
scanf ("%c", &ch);
printf ("Enter first number = ");
scanf ("%d", &num1);
printf ("Enter second number = ");
scanf ("%d", &num2);
result = 0;
switch (ch)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf ("Invalid operation\n");
}
printf ("Result: %d %c %d = %f\n", num1, ch, num2, result);
return 0;
}
14. WAP to check if triangle is equilateral, isosceles, or scalene of triangle taking 3 sides of a
triangle as input. 10M
#include<stdio.h>
int main(){
int side1, side2, side3;
printf("Enter sides of triangle:");
scanf("%d%d%d",&side1,&side2,&side3);
if(side1 == side2 && side2 == side3)
printf("The Given Triangle is equilateral");
else if(side1 == side2 || side2 == side3 || side3 == side1)
printf("The given Triangle is isosceles");
else
printf("The given Triangle is scalene");
return 0;
}
15. Write a C program to find factorial of a number and print the given pattern using suitable
programming constructs. 10M
1
23
456
78910
#include <stdio.h>
void main(){
int i, j, p=1;
for(i=1; i<= 4; i++){
for(j=1; j<=i; j++){
printf("%d",p);
p=p+1;
}
printf("\n");
}
}
16. The below program has to find the binary equivalent to the given decimal number. 5M
Complete the code:
#include<stdio.h>
int main( )
{
int decimal, binary=0, rem, i=0;
printf(“Enter a decimal integer\n”);
scanf(“%d”, &decimal);
while(______________)
{
rem= _____________;
binary+= __________;
decimal = __________;
____________;
}
printf(“The binary equivalent to decimal %d : %d”, decimal, binary);
return 0;
}
#include<stdio.h>
int main( )
{
int decimal, binary=0, rem, i=0;
printf(“Enter a decimal integer\n”);
scanf(“%d”, &decimal);
while(__decimal!=0__)
{
rem= __decimal%2__;
binary+= _rem+pow(10,i)_;
decimal = _decimal/2_;
__i++__;
}
printf(“The binary equivalent to decimal %d : %d”, decimal, binary);
return 0;
}
17. Write a program to print the sum of digits of a given integer. 5M
#include<stdio.h>
int main( )
{
int num, temp, sumofdigits=0;
printf(“Enter the number\n”);
scanf(“%d”, &num);
while(num!=0)
{
temp= num%10;
sumofdigits+=temp;
num = num/10;
}
printf(“The sum of digits= %d”, sumofdigits);
return 0;
}
18. Write a program to calculate all possible roots of quadratic equation. 10M
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,rpart,ipart,root1,root2;
printf("Enter three co-efficient\n");
scanf("%f%f%f",&a,&b,&c);
}
else if(a==0)
{
printf("Linear Equation\n");
root1=-c/b;
printf("Root=%f\n",root1);
}
else
{
d=(b*b)-(4*a*c);
if(d==0)
{
printf("The roots real and equal\n");
root1= -b/(2*a);
root2=root1;
printf("The roots are root1=%.3f and root2=%.3f\n",root1,root2);
}
else if(d>0)
{
printf("The roots are real and distinct\n");
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("The roots are root1=%.3f and root2=%.3f\n",root1,root2);
}
else
{
printf("The roots are imaginary\n");
rpart=-b/(2*a);
ipart=sqrt(fabs(d))/(2*a);
printf("The first root root1=%.3f+i%.3f\n",rpart,ipart);
printf("The second root root2=%.3f-i%.3f\n",rpart,ipart);
}
}
}
Output: 1 2 3 4
ii.)
#include<stdio.h>
int main( )
{
int x=1,y=2, z=3;
if( x<y)
printf("One is less than two\n");
else if(x==1)
printf("One is equal to 1\n");
else
printf("three = %d", z);
return 0;
}
20. Analyse the given program and write the expected output along with the justifications for the
same. 7M
#include <stdio.h>
int main( )
{
char i, j;
for(i=65;i<=70;i++)
{
printf ("\n");
for (j=65;j<=i;j++)
printf ("%c",j);
}
return 0;
}
Output:
A
AB
ABC
ABCD
ABCDE
ABCDEF
21. Code a C program to generate and display the following sequence: 1, 2, 4, 5, 7, 8, ..28, 29. 6M
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=30;i++)
{
while(i%3!=0)
{
printf("%d ",i);
i++;
}
}
return 0;
}
22. WAP to check whether a number is Armstrong or not. 8M
#include<stdio.h>
int main()
{
int num,sum=0,r,n;
printf("Enter the number:");
scanf("%d",&num);
n=num;
while(n>0)
{
r=n%10;
sum+=pow(r,3);
n=n/10;
}
if(sum==num)
printf("%d is an Armstrong number",num);
else
printf("%d is not an Armstrong number",num);
return 0;
}
#include <stdio.h>
int main() {
int i, space, k = 0;
for (i = 1; i <= 4; ++i, k = 0) {
for (space = 1; space <= 4 - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
24. Predict the output of the following code. 6M
Output:
********
*******
******
*****
****
***
**
*
#include <stdio.h>
void main()
{
int num=5, i = 1;
printf("Multiplication table of %d: \n", num);
do{
printf("\n %d x %d = %d", num, i, num * i);
i++;
}while (i <= 10);
26. A scenario is given to read the maximum of 10 numbers and a constraint is that if a negative
number enters the system, it should terminate and print the sum of taken inputs. Write a C
program to do the same. 8M
#include <stdio.h>
int main()
{
int i, n, sum = 0;
printf("Input the 10 numbers : \n");
for (i = 1; i <= 10; i++)
{
scanf("%d", &n);
if(n>0||n==0)
sum += n;
else
break;
}
printf("The sum of 10 no is : %d\n",sum);
return 0;
}
27. Given a binary number, write a program to print the decimal equivalent of it. 8M
#include<stdio.h>
int main( )
{
int decimal, binary=0, rem, i=0;
printf(“Enter a binary number\n”);
scanf(“%d”, &binary);
while(binary!=0)
{
rem= binary%10;
decimal+= rem*pow(2,i);
binary = binary/10;
i++;
}
printf(“The decimal equivalent = %d”, decimal);
return 0;
}
28. Write a program using appropriate programming constructs to print the following pattern:
1 or 1
22 01
333 101
4444 0101
55555 10101
For Pattern1:
#include<stdio.h>
void main()
{
int i,j,n;
for(i=0;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
}
For Pattern2:
#include<stdio.h>
void main()
{
int i,j,n=6;
for(i=0;i<=n;i++)
{
for(j=1;j<i;j++)
{
if((i+j)%2==0)
{
printf("\t 0");
}
else
{
printf("\t 1");
}
}
printf("\n");
}
}
(OR)
#include <stdio.h>
int main() {
int i, j, n, p, q;
return 0;
}
29. Given four subjects marks, calculate the total, aggregate, and display the grades obtained by
the student in a program. 8M
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,total=0;
float avg=0.0;
printf(“Enter the marks of 4 Subjects:”);
scanf(“%d%d%d%d”,&m1,&m2,&m3,&m4);
total=m1+m2+m3+m4;
avg=(float)total/4;
printf(“Total=%d”,total);
printf(“Aggregate=%.2f”,avg);
if(avg>=75)
printf(“Distinction”);
else if(avg>=60 && avg<75)
printf(“First class”);
else if(avg>=50 && avg<60)
printf(“Second class”);
else if(avg>=40 && avg<50)
printf(“Third class”);
else
printf(“FAIL”);
return 0;
}
Output:
*********
*******
*****
***
*
#include<stdio.h>
int main()
{
int n,sum,sum1=0,i,j;
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=i;j++)
{
sum=sum+j;
}
sum1=sum1+sum;
}
printf("\nThe Sum of Series up to Value [ %d ] = [ %d ]\n",n,sum1);
return 0;
}
33. Consider a number is input through keyboard. If number is between 0 to 10 then square the
number. If number is between 11 to 20 then cube the number and for all other conditions display
as invalid number. Write a C program for the same. 6M
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n<=10)
{
printf("%d",n*n);
}
else if(n>10&&n<=20)
{
printf("%d",n*n*n);
}
else if(n>20)
{
printf("Invalid Number");
}
return 0;
}
34. Write a program in C to read the numbers until -1 is encountered. Also count the negative,
positive and zeroes entered by the user. 6M
#include<stdio.h>
int main()
{
int num,neg=0,pos=0,zer=0;
printf("Enter -1 to exit");
printf("Enter any number:");
scanf("%d",&num);
while(num!=-1)
{
if(num>0)
pos++;
else if(num<0)
neg++;
else
zer++;
printf("Enter any number:");
scanf("%d",&num);
}
printf("Count of positive numbers entered =%d\n",pos);
printf("Count of negative numbers entered =%d\n",neg);
printf("Count of zeros entered =%d",zer);
return 0;
}
35. A company decides to give bonus to all its employees on New Year. A 10% bonus on salary is
given to the male workers and 20% bonus on salary to female workers. Write a C program to
enter the salary and gender of the employee. Calculate the bonus that has to be given to the
employee and display the revised salary (Salary + bonus) that the employee will get. 8M
#include<stdio.h>
int main()
{
int i,j;
float salary,bonus;
char gender;
printf("Enter M for Male and F for Female\n");
scanf("%c",&gender);
printf("Enter Salary\n");
scanf("%f",&salary);
if(gender=='M' || gender=='m')
{
if(salary>10000)
bonus=(float)(salary*0.05);//0.05--5%
else
bonus=(float)(salary*0.07);
}
if(gender=='F' || gender=='f')
{
if(salary>10000)
bonus=(float)(salary*0.1);//0.1--10%
else
bonus=(float)(salary*0.12);
}
salary=salary+bonus;
printf("Bonus=%.2f\nSalary=%.2f\n",bonus,salary);
}
36. WAP to print the following remarks based on input grades. Use switch statement. 6M
#include<stdio.h>
int main()
{
char grade;
printf("\nEnter the grade(A,B,C,F):");
scanf("%c",&grade);
if(grade==’A’)
printf("Excellent");
else if(grade==’B’)
printf("Well done");
else if(grade==’C’)
printf("You passed");
else if(grade==’F’)
printf("Better try again");
else
printf("Invalid grade");
return 0;
}
37. WAP using do-while loop to display the square and cube of first ‘n’ natural numbers.
#include <stdio.h>
int main()
{
int i, n;
printf("Enter the value of n : ");
scanf("%d", &n);
printf("Number Square Cube\n");
for (i = 1; i < n + 1; i++)
{
printf("%d \t %d \t %d\n", i, i * i, i * i * i);
}
return 0;
}
38. Analyse the following program and write the output of the following program. 8M
Output:
0
1
3
6
10
15
21
28
36
45
55
39. DAP to calculate the roots of a quadratic equation using switch statement. 5M
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float r1,r2,i;
float d;
printf("Enter values of a, b, c :");
scanf("%f%f%f", &a, &b, &c);
d = (b*b)-(4*a*c);
switch(d>0)
{
case 1:
r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);
printf("real roots:%.2f and %.2f",r1, r2);
break;
case 0:
switch(d<0)
{
case 1:
r1=r2=-b/(2*a);
i=sqrt(-d)/(2*a);
printf("complex roots: %.2f + i%.2f and %.2f - i%.2f",r1,i,r2,i);
break;
case 0:
r1=r2=-b/(2*a);
printf("Two equal and real roots: %.2f and %.2f",r1,r2);
break;
}
}
return 0;
}
40. DAP to display the numbers between 0 to 30 which are not divisible by 2.
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=30;i++)
{
while(i%2!=0)
{
printf("%d ",i);
i++;
}
}
return 0;
}
41. DAP to find the sum of series 1/1 + 22/2 + 33/3 + …. + nn/n. 5M
#include<stdio.h>
int main()
{
int n;
float sum=0.0;
printf("Enter the value of n , till which sum is required:-");
scanf("%d",&n);
for(int i=1;i<=n;i++)
sum=sum+(pow(i,i)/i);
printf("The sum of series 1/1 + 22/2 + 33/3 + …. + nn/n is %f",sum);
return 0;
}
42. DAP to calculate tax, given the following conditions: 5M
If income is less than 1,50,000 then no tax.
If taxable income is in the range 1,50,001-3,00,000 then charge 10% tax.
If taxable income is above 5,00,001 then charge 30% tax.
#include <stdio.h>
void main(){
float amount, tax=0;
printf("Enter the amount of income: ");
scanf("%f",&amount);
if(amount<=150000){
tax=0;
}
else if(amount>150000 && amount<=300000){
tax=amount*0.1;
}
else if(amount>300000 && amount<=500000){
tax=amount*0.2;
}
else{
tax=amount*0.3;
}
if(tax==0){
printf("\nNo tax.\n");
}else{
printf("\nThe tax = $%.2f\n",tax);
}
}
43. Design a C program to read the year as input and find whether it is a leap year or not. 4M
#include<stdio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(((year%4==0) && ((year%400==0) || (year%100!==0))
{
printf("%d is a leap year", &year);
}
else
{
printf("%d is not a leap year", &year);
}
}
44. Develop a C program to reverse an integer number “NUM” and check whether it is a
palindrome or not. 6M
#include <stdio.h>
void main()
{
int num, temp, rem, rev = 0;
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
46. Write a C program to read temperature in centigrade and display a suitable message according
to the temperature stated below. 7M
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 11-20 then Cold weather
Temp 21-30 then Normal in Temp
Temp 31-40 then Its Hot
Temp >40 then Its Very Hot. Write the expected output
Note: Above program should be written using else-if ladder statement.
#include <stdio.h>
void main()
{
int tmp;
printf("Input days temperature : ");
scanf("%d", &tmp);
if (tmp < 0)
printf("Freezing weather.\n");
else if (tmp < 10)
printf("Very cold weather.\n");
else if (tmp < 20)
printf("Cold weather.\n");
else if (tmp < 30)
printf("Normal in temp.\n");
else if (tmp < 40)
printf("Its Hot.\n");
else
printf("Its very hot.\n");
}
47. Develop a C code to display the multiplication table for a number provided by the user. (Output
to be of form 8x1=8) 7M
#include <stdio.h>
void main()
{
int num, i = 1;
printf(“Enter the number for table:\n”);
scanf(“%d”,&num);
printf("Multiplication table of %d: \n",
num);
do{
printf("\n %d x %d = %d", num, i, num* i);
i++;
}while (i <= 10);
}
48. Print the below pattern using a C code. 6M
#include<stdio.h>
int main()
{
int i,j,k;
k=1;
for(i=1;i<=4;i+=1)
{
for(j=5;j>=1;j--)
{
if(j>i)
printf(" ");
else
printf("%d ",k++);
}
printf("\n");
}
return 0;
}