1. Write a C program to compute the sum of the two given integer values.
If the two values
are the same, then return triple their sum.
#include<stdio.h>
int main()
{
int X,Y,sum;
scanf("%d%d",&X,&Y);
sum = X+Y;
if(X==Y){
sum=sum*3;
}
printf("%d\n",sum);
return 0;
}
Input and Output:
Expected Output:
39
12
12 12
72
2. Write a C program to print a big 'C'.
#include<stdio.h>
int main()
{
printf(" ######\n");
printf(" ## ##\n");
printf("#\n");
printf("#\n");
printf("#\n");
printf("#\n");
printf("#\n");
printf(" ## ##\n");
printf(" ######\n");
// int i,j,n;
// scanf("%d",&n);
//
// for(i=1;i<=n-2;i++){
// for(j=1;j<=n;j++){
// if(i==1 || i==(n-2)){
// if(j<=(n/3))
// printf(" ");
// else{
// if(j<=(n-n/3+1))
// printf("#");
// }
// }
// else if(i==2 || i==(n-3)){
// if(j<=(n/6))
// printf(" ");
// else{
// if(j<=(n/3) || j>(n-(n/3)+1))
// printf("#");
// else
// printf(" ");
// }
// }
// else{
// if(j==1){
// printf("#");
// }
// }
// }
// printf("\n");
// }
return 0;
}
Input and Output:
Expected Output:
######
## ##
#
#
#
#
#
## ##
######
3. Write a C program to find the third angle of a triangle if two angles are given.
#include<stdio.h>
int main()
{
int X,Y,angle;
scanf("%d%d",&X,&Y);
angle = (180 - (X+Y));
printf("Third angle of the triangle: %d\n",angle);
return 0;
}
Input and Output:
Expected Output:
Input two angles of triangle separated by comma: 50, 70
Third angle of the triangle: 60
4. Write a C program to compute the perimeter and area of a rectangle with a height and
width of inches unit.
#include<stdio.h>
int main()
{
int w,h,perimeter,area;
scanf("%d%d",&w,&h);
perimeter = 2*(w+h);
area = w * h;
printf("Perimeter : %d inches\n",perimeter);
printf("Area : %d square inches\n",area);
return 0;
}
Input and Output:
Expected Output:
20 30
Perimeter : 100 inches
Area : 600 square inches
5. Write a C program to compute the perimeter and area of a circle with a radius of 6 inches.
#include<stdio.h>
#include<math.h>
#define pi 3.14
int main()
{
float perimeter,area,radius;
scanf("%f",&radius);
perimeter = 2 * pi * radius;
// area = 3.1416 * radius *radius;
area = pi * pow(radius,2);
printf("Perimeter of the Circle = %f inches\n",perimeter);
printf("Area of the Circle = %f square inches\n",area);
return 0;
}
Input and Output:
Expected Output:
6
Perimeter of the Circle = 37.680000 inches
Area of the Circle = 113.040001 square inches
6. Write a C program to convert specified days into years, weeks and days. [Note: Ignore leap
year.].
#include<stdio.h>
int main()
{
// int year,week,day,num;
// printf("Number of days: ");
// scanf("%d",&num);
// year = num/365;
// week=(num%365)/7;
// day =(num%365)%7;
int year,week,day;
printf("Number of days: ");
scanf("%d",&day);
year = day/365;
day = day%365;
week=day/7;
day =day%7;
printf("Years: %d Weeks: %d Days: %d\n",year,week,day);
return 0;
}
Input and Output:
Test Data:
Number of days: 1329
Expected Output: Years: 3 Weeks: 33 Days: 3
7. Write a C program that accepts two float from the user and calculate the sum of the two numbers.
#include<stdio.h>
int main()
{
float n1,n2,sum;
scanf("%f%f",&n1,&n2);
sum = n1+n2;
printf("%f\n",sum);
// printf("%.2f\n",sum);
return 0;
}
Input and Output:
Expected Output:
2.5 4.3
6.8
8. Write a C program that prints all even numbers between 1 and 50 (inclusive).
#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);
// for(int i=2;i<=num;i=i+2){
// printf("%d ",i);
// }
for(int i=1;i<=num;i++){
if(i%2==0){
printf("%d ",i);
}
}
return 0;
}
Input and Output:
Expected Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
9. Write a program that converts Centigrade to Fahrenheit.
#include<stdio.h>
int main()
{
float centi,far;
scanf("%f",¢i);
far = (9*centi + 160)/5;
printf("%f",far);
return 0;
}
Input and Output:
Expected Output:
-40
-40
10. Write a C program that takes hours and minutes as input, and calculates the total number
of minutes.
#include<stdio.h>
int main()
{
int hour,minute;
printf("Input hours: ");
scanf("%d",&hour);
printf("\nInput minutes: ");
scanf("%d",&minute);
minute=(hour*60)+minute;
printf("Total: %d minutes\n",minute);
return 0;
}
Input and Output:
Expected Output:
Input hours: 5
Input minutes: 37
Total: 337 minutes.
11. Write a C program to check whether a given number is even or odd.
#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);
if(num%2==0){
printf("Even\n");
}
else{
printf("Odd\n");
}
return 0;
}
Input and Output:
Expected Output:
200
Even
1989
Odd
12. Write a C program to check whether a given number is positive or negative.
#include<stdio.h>
int main()
{
int num;
printf("Test Data: ");
scanf("%d",&num);
if(num>0){
printf("%d is a positive number\n",num);
}
if(num<0){
printf("%d is a negative number\n",num);
}
else{
printf("Zero");
}
return 0;
}
Input and Output:
Test Data: 15
Expected Output:
15 is a positive number
13. Write a C program that accepts two item’s weight (floating points' values) and number of
purchase (floating points' values) and calculate the average value of the items.
#include<stdio.h>
int main()
{
float item1,item2,no1,no2,avg;
printf("Weight - Item1: ");
scanf("%f",&item1);
printf(" No. of item1: ");
scanf("%f",&no1);
printf("\nWeight - Item2: ");
scanf("%f",&item2);
printf("No. of item2: ");
scanf("%f",&no2);
avg = ((item1*no1)+(item2*no2))/(no1+no2);
printf("Average Value = %f\n",avg);
return 0;
}
Input and Output:
Sample Output:
Weight - Item1: 15 No. of item1: 5
Weight - Item2: 25 No. of item2: 4
Expected Output: Average Value = 19.444444
14. Write a C program that accepts three integers and find the maximum of three.
#include<stdio.h>
int main(){
int n1,n2,n3,max;
scanf("%d%d%d",&n1,&n2,&n3);
if((n1>n2)&&(n1>n3)){
max=n1;
}
else if((n2>n1)&&(n2>n3)){
max=n2;
}
else {
max=n3;
}
printf("%d",max);
}
Input and Output:
Expected Output:
10 15 12
15
15. Write a C program that reads three floating values and check if it is possible to make a
triangle with them. Also calculate the perimeter of the triangle if the said values are valid.
#include<stdio.h>
int main()
{
float a,b,c;
printf("Input the first number: ");
scanf("%f",&a);
printf("Input the second number: ");
scanf("%f",&b);
printf("Input the third number: ");
scanf("%f",&c);
if(((a+b)>c)||((b+c)>a)||((c+a)>b)){
printf("Make triangle\n");
printf("Perimeter = %.1f",(a+b+c));
}
return 0;
}
Input and Output:
Input the first number: 25
Input the second number: 15
Input the third number: 35
Expected Output:
Make triangle Perimeter = 75.0
16. Write a C program that reads an integer between 1 and 12 and print the month of the year in English.
#include<stdio.h>
int main()
{
int monthName;
printf("Input a number between 1 to 12 to get the month name: ");
scanf("%d",&monthName);
// if(monthName==1){
// printf("January\n");
// }
// else if(monthName==2){
// printf("February\n");
// }
// else if(monthName==3){
// printf("March\n");
// }
// else if(monthName==4){
// printf("April\n");
// }
// else if(monthName==5){
// printf("May\n");
// }
// else if(monthName==6){
// printf("June\n");
// }
// else if(monthName==7){
// printf("July\n");
// }
// else if(monthName==8){
// printf("August\n");
// }
// else if(monthName==9){
// printf("September\n");
// }
// else if(monthName==10){
// printf("October\n");
// }
// else if(monthName==11){
// printf("November\n");
// }
// else if(monthName==12){
// printf("December\n");
// }
char Month[12][10] = { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
printf("%s\n",Month+(monthName-1));
return 0;
}
Input and Output:
Test Data:
Input a number between 1 to 12 to get the month name: 8
Expected Output: August
17. Write a C program to print the following word/characters in a reverse way.
#include<stdio.h>
#include<string.h>
//void reverse(){
// int ch;
// ch=getchar();
// if(ch!='\n'){
// reverse();
// printf("%c",ch);
// }
//}
int main()
{
char word[1000];
scanf("%s",&word);
int n=strlen(word);
// printf("%d",n);
printf("The reverse of %s is: ",word);
int i;
for(i=n-1;i>=0;i--){
printf("%c",word[i]);
}
// reverse();
return 0;
}
Input and Output:
Test Characters: 'milton'
Expected Output: The reverse of milton is notlim
18. Write a C program to find whether a given year is a leap year or not.
#include<stdio.h>
int main()
{
int year;
scanf("%d",&year);
if((year%400==0)||(year%4==0)&&(year%100!=0)){
printf("%d is a leap year",year);
}
else{
printf("%d is Not a leap year",year);
}
return 0;
}
Input and Output:
Test Data: 2016
Expected Output: 2016 is a leap year
19. Write a C program to accept a coordinate point in a XY coordinate system and determine
in which quadrant the coordinate point lies.
#include<stdio.h>
int main()
{
int X,Y;
scanf("%d%d",&X,&Y);
if((X>0)&&(Y>0)){
printf("The coordinate point (%d,%d) lies in the First quadrant.",X,Y);
}
else if((X<0)&&(Y>0)){
printf("The coordinate point (%d,%d) lies in the Second quadrant.",X,Y);
}
else if((X<0)&&(Y<0)){
printf("The coordinate point (%d,%d) lies in the Third quadrant.",X,Y);
}
else if((X>0)&&(Y<0)){
printf("The coordinate point (%d,%d) lies in the Fourth quadrant.",X,Y);
}
else if((X==0)&&(Y!=0)){
printf("The coordinate point (%d,%d) lies in the Y-Axis.",X,Y);
}
else if((X!=0)&&(Y==0)){
printf("The coordinate point (%d,%d) lies in the X-Axis.",X,Y);
}
else {
printf("The coordinate point (%d,%d) lies in the Origin.",X,Y);
}
return 0;
}
Input and Output:
Test Data: 7 9
Expected Output : The coordinate point (7,9) lies in the First quadrant.
20. Write a C program to check whether a character is an alphabet, digit or special character.
#include<stdio.h>
int main()
{
int ch;
printf("Test Data: ");
ch=getchar();
// printf("%d %c",ch,ch);
if(ch>=48 && ch<=57){
printf("This is a digit\n");
}else if((ch>=65 && ch<=90)||(ch>=97 && ch<=122)){
printf("This is a character\n");
}else{
printf("This is a special character\n");
}
return 0;
}
Input and Output:
Test Data: @
Expected Output : This is a special character.
21. Write a C program to find the sum of first 10 natural numbers.
#include<stdio.h>
int main()
{
int sum=0,n;
scanf("%d",&n);
// for(int i=1;i<=n;i++){
// sum=sum+i;
// }
sum= n*(n+1)/2;
printf("The sum is: %d",sum);
return 0;
}
Input and Output:
Expected Output:
The first 10 natural number is: 1 2 3 4 5 6 7 8 9 10
The Sum is : 55
22. Write a program in C to display n terms of natural number and their sum.
#include<stdio.h>
int main()
{
int sum=0;
printf("The first 7 natural number is : ");
for(int i=1;i<=7;i++){
scanf("%d",&i);
}
for(int i=1;i<=7;i++){
sum=sum+i;
}
printf("The Sum of Natural Number upto 7 terms : %d",sum);
return 0;
}
Input and Output:
Expected Output:
The first 7 natural number is : 1 2 3 4 5 6 7
The Sum of Natural Number upto 7 terms : 28
23. Write a program in C to display the pattern like right angle triangle using an asterisk.
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
Input and Output:
4
*
**
***
****
24. Write a program in C to display the pattern like right angle triangle with a number.
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int row=1;row<=n;row++){
for(int col=1;col<=row;col++){
printf("%d",col);
}
printf("\n");
}
return 0;
Input and Output:
4
1
12
123
1234
25. Write a program in C to make such a pattern like right angle triangle with a number
which will repeat a number in a row.
#include<stdio.h>
int main()
{
int n;
printf("Enter how many numbers do you like to want? ");
scanf("%d",&n);
for(int row = 1;row<=n;row++){
for(int col=1;col<=row;col++){
printf("%d",row);
}
printf("\n");
}
return 0;
}
Input and Output:
4
1
22
333
4444
26. Write a program in C to make such a pattern like right angle triangle with number increased by 1.
#include<stdio.h>
int main()
{
int n;
printf("Enter how many numbers do you like to want? ");
scanf("%d",&n);
int sum=1;
for(int row = 1;row<=n;row++){
for(int col=1;col<=row;col++){
printf("%d ",sum++);
}
printf("\n");
}
return 0;
}
Input and Output:
4
1
23
456
7 8 9 10
27. Write a C program to calculate the factorial of a given number.
#include<stdio.h>
int factorial(int n){
if(n==1)
return 1;
else
return n*factorial(n-1);
}
int main()
{
int n,fact=1;
printf("Input the number: ");
scanf("%d",&n);
// for(int i=n;i>=1;i--){
// fact=fact*i;
// }
fact=factorial(n);
printf("The Factorial of 5 is: %d",fact);
return 0;
}
Input and Output:
Test Data :
Input the number : 5
Expected Output :
The Factorial of 5 is: 120
28. Write a program in C for addition of two Matrices of same size.
#include<stdio.h>
int main()
{
int row,col,i,j;
printf("Enter number of rows and columns: ");
scanf("%d%d",&row,&col);
int A[row][col],B[row][col],sum[row][col];
printf("Enter the element of first matrix: \n");
for(i=0;i<row;i++){
for( j=0;j<col;j++){
scanf("%d",&A[i][j]);
}
}
printf("Enter the element of second matrix: \n");
for(i=0;i<row;i++){
for( j=0;j<col;j++){
scanf("%d",&B[i][j]);
}
}
for( i=0;i<row;i++){
for(j=0;j<col;j++){
sum[i][j] = A[i][j]+B[i][j];
}
}
printf("The First matrix is: \n");
for(i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("The second matrix is: \n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d ",B[i][j]);
}
printf("\n");
}
printf("The sum is: \n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d ",sum[i][j]);
}
printf("\n");
}
return 0;
}
Input and Output:
Enter number of rows and columns: 2 2
Enter the element of first matrix:
12
34
Enter the element of second matrix:
56
78
The First matrix is :
12
34
The Second matrix is :
56
78
The sum is :
68
10 12
29. Write a C program to display the pattern like pyramid using the alphabet.
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int sp=1;sp<=n-i;sp++){
printf(" ");
}
int k=1;
for(int j=1;j<=2*i-1;j++){
if(j<=i){
printf("%c",(j+64));
}else{
printf("%c",(i-k+64));
k++;
}
}
printf("\n");
}
return 0;
}
Input and Output:
4
A
ABA
ABCBA
ABCDCBA
30. Write a C program to find the transpose of a given matrix.
#include<stdio.h>
int main()
{
int n,m;
printf("Enter number of rows and columns: ");
scanf("%d%d",&n,&m);
int A[n][m];
printf("Enter elements of the matrix \n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&A[i][j]);
}
}
printf("The Transpose of the given matrix is \n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",A[j][i]);
}
printf("\n");
}
return 0;
}
Input and Output:
Enter number of rows and columns: 2 2
Enter elements of the matrix
12
34
The Transpose of the given matrix is
13
24