You are on page 1of 15

Programs for Exams

1. WAP to find the sum of all the elements.

Ans: -
#include<stdio.h>
int main()
{
int arr[100], size, i, sum = 0;
printf("Enter array size\n");
scanf("%d",&size);
printf("Enter array elements\n");
for(i = 0; i < size; i++)
scanf("%d",&arr[i]);
for(i = 0; i < size; i++)
sum = sum + arr[i];
printf("Sum of the array = %d\n",sum);
return 0;
}
2. WAP to store in an Array & Find the total no. duplicates in it.
Ans: -
#include <stdio.h>
int main()
{
int arr[10], i, j, Size, Count = 0;
printf ("\n Please Enter Number of elements in an array:");
scanf ("%d", &Size);
printf ("\n Please Enter %d elements of an Array:", Size);
for (i = 0; i < Size; i++)
{
Scanf ("%d", &arr[i]);
}
for (i = 0; i < Size; i++)
{
for (j = i + 1; j < Size; j++)
{
If (arr[i] == arr[j])
{
count++;
break;
}
}
}
printf("\n Total Number of Duplicate Elements in this Array = %d ", Count);
return 0;
}
3. WAP to print a right triangle.
Ans: -
#include<stdio.h>
int main()
{
int i, j, rows, count = 1;
printf("Enter the number of rows\n");
scanf("%d", &rows);
for (i = 0; i < rows; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", count);
count++;
}
printf("\n");
}
return(0);
}
4. WAP to print a pattern.
Ans: -
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
5. WAP to calculate the area of a Rectangle & Circle using (switch case)
Ans: -
#include <stdio.h>
int input();
void output(float);
int main()
{
float result;
int choice, num, num1,num2;
printf("Press 1 to calculate area of circle\n");
printf("Press 2 to calculate area of rectangle\n");
printf("Enter your choice:\n");
choice = input();
switch (choice) {
case 1: {
printf("Enter radius:\n");
num = input();
result = 3.14 * num * num;
printf("Area of circle=");
output(result);
break;
}
case 2: {
printf("Enter side of rectangle:\n");
scanf("%d%d",&num1,&num2);
result = num1 * num2;
printf("Area of rectangle=");
output(result);
break;
}
default:
printf("wrong Input\n");
}
return 0;
}
int input()
{
int number;
scanf ("%d", &number);
return (number);
}
void output (float number)
{
printf ("%f", number);
}
6. WAP to check & print Armstrong nos. 1 to n
Ans: -
#include <stdio.h>
int checkArmstrong( int num)
{
int a, rem, sum;
a = num;
sum = 0;
while (a != 0)
{
rem = a % 10;
sum = sum + (rem * rem * rem);
a /= 10;
}
if (sum == num)
return 1;
else
return 0;
}
int main()
{
int i, n;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("All Armstrong numbers from 1 to %d:\n", n);
for (i = 1; i <= n; i++)
{
if (checkArmstrong(i))
printf("%d,", i);
}
return 0;
}
7. WAP to check if the input is a no. or character
Ans: -
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an character.", c);
else
printf("%c is not an character, it's a number", c);
return 0;
}
8. WAP to print the Fibonacci series from 1 to n
Ans: -
#include <stdio.h>
int main()
{
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
9. WAP to print you own name.

Ans: -
#include<stdio.h>
int main()
{
printf("Enter your name:");
scanf("%c");
}
10. WAP to accept 2 integers & print their sum.
Ans: -
#include<stdio.h>
int main()
{
int A, B, sum = 0;
printf("Enter two numbers A and B : \n");
scanf("%d%d", &A, &B);
sum = A + B;
printf("Sum of A and B is: %d", sum);
return 0;
}
11. WAP to convert the value given in Celsius degree into Fahrenheit through user input.
Ans: -
#include<stdio.h>
int main()
{
float farenheit,celsius;
printf("celsius:");
scanf("%f",&celsius);
farenheit=((celsius*9)/5)+32;
printf("temperature in farenheit is:%f",farenheit);
return 0;
}
12. WAP to convert the value given in Fahrenheit degree into Celsius through user input.

Ans: -
#include<stdio.h>
int main()
{
float celsius,farenheit;
printf("farenheit:");
scanf("%f",&farenheit);
celsius=((farenheit-32)*5)/9;
printf("temperature in celsius is:%f",farenheit);
return 0;
}
13. WAP to take user input basic salary & print the gross salary.
Ans: -
#include<stdio.h>
int main()
{
float basic_salary,DA,HRA,PF,gross_salary;
printf("basic salary:");
scanf("%f",&basic_salary);
DA=(50/100.00)*basic_salary;
HRA=(10/100.00)*basic_salary;
PF=(15/100.00)*basic_salary;
gross_salary=DA+HRA+PF+basic_salary;
printf("the gross salary:%f",gross_salary);
return 0;
}
14. WAP to take a user input character & print its ASCII value.

Ans: -
#include<stdio.h>
int main()
{
char ch;
printf("enter a character:");
scanf("%c", &ch);
printf("ASCII value of character is %d", ch);
return 0;
}
15. WAP to print all natural numbers from 1 to n with user input.
Ans: -
#include<stdio.h>
Int main()
{
Int x, y, z;
Printf(“Enter two numbers to add\n”);
Scanf(“%d%d”, &x, &y);
z = x + y;
printf(“Sum of the numbers = %d\n”, z);
return 0;
}
16. WAP to check whether a number is a 'Perfect-Number' or not with user input.

Ans: -
#include <stdio.h>
int main()
{
int n,i,s,j;
printf("Enter the Number :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=0;
for (j=1;j<i;j++)
{
if(i%j==0)
{
s=s+j;
}
}
if(s==i)
printf("Perfect number:%d\n",i);
}
return 0;
}
17. WAP to take input user input & print sum of all prime nos.
Ans: -
#include <stdio.h>
int main()
{
int counter, N, i, s, R = 0;
printf("Enter a Number\n");
scanf("%d", &N);

for(counter = 2; counter <= N; counter++)


{
s = 1;
for(i = 2; i <=(counter/2); ++i)
{
if(counter%i==0)
{
s = 0;
break;
}
}

if(s==1)
R += counter;
}
printf("Sum of Prime Numbers between 1 to %d : %d", N,R);
return 0;
}
18. WAP to take user input to print natural nos. from n to 1.
Ans: -
#include<stdio.h>
int main()
{
int n,i;
printf("Enter the number:");
scanf("%d",&n);

for(i=n;i>=1;i--)
{
printf("Natural nubers:%d\n",i);
}
return 0;
}
19. WAP to take user input & print all even numbers from 1 to n.
Ans: -
#include<stdio.h>
int main()
{
int n,i;
printf("Enter the Number :");
scanf("%d",&n);

for(i=2;i<=n;i=i+2)
{
printf("Enter the number : %d\n",i);
}
return 0;
}
20. WAP to take user input of 3 numbers & check the smallest number among those 3 nos.

Ans: -

#include <stdio.h>
int main()
{
int a , b , c;
printf("the values are:");
scanf("%d %d %d", &a, &b, &c);
if(a<b && a<c)
printf("a is the smallest");
else if(b<a && b<c)
printf("b is the smallest");
else if(c<a && c<b)
printf("c is the smallest");
else
printf("return");
return 0;
}
21. WAP to take user input & check whether the number is +, - or 0.
Ans: -
#include <stdio.h>
int main()
{
int A;
printf("Enter the number A: ");
scanf("%d", &A);
if (A > 0)
printf("%d is positive.", A);
else if (A < 0)
printf("%d is negative.", A);
else if (A == 0)
printf("%d is zero.", A);
return 0;
}
22. WAP to check of the candidate is eligible to marry or not.
Ans: -
#include<stdio.h>
int main()

int a;

printf("Enter your age:");

scanf("%d",&a);

if(a<=18)

printf(" Not elligibe to marry :%d", a);

else if(a>18)

printf("Elligibe to marry :%d", a);


}

else

printf("Elligible to marry :%d", a);

return 0;

23. Write a C program to check whether an input character is vowel or consonant.

Ans: -
#include <stdio.h>
int main()

char ch;

printf("Enter an Alphabet\n");

scanf("%c",&ch);

if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')

printf("%c is vowel",ch);

else

printf("%c is consonant",ch);

return 0;

24. WAP to check if a no. is Armstrong number or not.

Ans: -
#include <stdio.h>
int checkArmstrong( int num)
{
int a, rem, sum;
a = num;
sum = 0;
while (a != 0)
{
rem = a % 10;
sum = sum + (rem * rem * rem);
a /= 10;
}
if (sum == num)
return 1;
else
return 0;
}
int main()
{
int i, n;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("All Armstrong numbers from 1 to %d:\n", n);
for (i = 1; i <= n; i++)
{
if (checkArmstrong(i))
printf("%d,", i);
}
return 0;
}
25. WAP to check whether a year is leap year or not.
Ans: -
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
26. WAP to calculate the Factorial of a no.
Ans: -
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
27. WAP to add 2 Arrays.
Ans: -
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[5],b[5],c[5];
printf("\nReading the 1st array\n");
for (i=0;i<5;i++)
{
printf("Enter the value");
scanf("%d",&a[i]);
}
printf("\nReading the 2nd array\n");
for (i=0;i<5;i++)
{
printf("Enter the value");
scanf("%d",&b[i]);
}
printf("\nThe output of addition of 2 array is\n");
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
printf("\nthe sum of %d & %d is %d",a[i],b[i],c[i]);
}
getch();
}
28. WAP to accept user input float values & print their sum.
Ans: -
#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
29. WAP to take 2 users input then give the output after dividing the inputs.
Ans: -
#include <stdio.h>
int main()
{
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
product = a * b;
printf("Product = %.2lf", product);
return 0;
}
30. WAP to accept user input & swap the values with using 3rd variable.
Ans: -
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\n After swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
31. WAP to accept user input & swap the values without using 3rd variable.
Ans: -
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
32. WAP to print a Right-Angle Triangle by printing numbers from 1 to 10.
Ans: -
#include<stdio.h>
int main()
{
int i, j, rows, count = 1;
printf("Enter the number of rows\n");
scanf("%d", &rows);
for (i = 0; i < rows; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", count);
count++;
}
printf("\n");
}
return(0);
}
33. WAP to take user input Decimal and convert it into Binary.

Ans: -
#include<stdio.h>
int main()
{
int a[10],n,i;
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
34. WAP to take input and check if a String is Palindrome or Not.
Ans: -
#include <stdio.h>
int main()
{
int n, reversed = 0, remainder, a;
printf("Enter an integer: ");
scanf("%d", &n);
a = n;
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (a == reversed)
printf("%d is a palindrome.", a);
else
printf("%d is not a palindrome.", a);
return 0;
}
35. WAP to print the sum of all the elements in an array.
Ans: -
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
int length = sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < length; i++) {
sum = sum + arr[i];
}
printf("Sum of all the elements of an array: %d", sum);
return 0;
}
36. WAP to copy the elements of an Array to another Array.
Ans: -
#include <stdio.h>
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int length = sizeof(arr1)/sizeof(arr1[0]);
int arr2[length];
for (int i = 0; i < length; i++)
{
arr2[i] = arr1[i];
}
printf("Elements of original array: \n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
printf("Elements of new array: \n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
37. WAP to print the reverse of characters to display character from A-Z with using loop.
Ans: -
#include <stdio.h>
int main()
{
char char1 = 'X';
char char2 = 'M';
char char3 = 'L';
printf("The reverse of %c%c%c is %c%c%c\n",
char1, char2, char3,
char3, char2, char1);
return(0);
}
38. WAP to print the reverse of characters.
Ans: -
#include <stdio.h>
int main()
{
char char1 = 'X';
char char2 = 'M';
char char3 = 'L';
printf("The reverse of %c%c%c is %c%c%c\n",
char1, char2, char3,
char3, char2, char1);
return(0);
}
39. WAP to calculate Average using Array.
Ans: -
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}

You might also like