You are on page 1of 22

Code List with code:

1. Write a C program to print your name, date of birth, and mobile number.
Expected Output:
Name: Alexandra Abramov
DOB: July 14, 1975
Mobile: 99-9999999999

Code:
#include <stdio.h>
int main()
{
printf("Name : Alexandra Abramov\n");
printf("DOB : July 14, 1975\n");
printf("Mobile : 99-9999999999\n");
return(0);
}

2. Write a C program to print the following characters in reverse.


Test Characters: 'X', 'M', 'L'
Expected Output:
The reverse of XML is LMX

Code:
#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);
}

Page | 1
3. Write a C program that accepts two integers from the user and calculates the
sum of the two integers.
Test Data: Input the first integer: 25
Input the second integer: 38
Expected Output:
Sum of the above two integers = 63

Code:
#include <stdio.h>
int main()
{
int x, y, sum;
printf("\nInput the first integer: ");
scanf("%d", &x);
printf("\nInput the second integer: ");
scanf("%d", &y);
sum = x + y;
printf("\nSum of the above two integers = %d\n", sum);
return 0;
}

4. Write a C program that accepts two integers from the user and calculates the
product of the two integers.
Test Data: Input the first integer: 25
Input the second integer: 15
Expected Output:
Product of the above two integers = 375

Code:
#include <stdio.h>
int main()
{
int x, y, result;
printf("\nInput the first integer: ");
scanf("%d", &x);
printf("\nInput the second integer: ");
scanf("%d", &y);
result = x * y;
printf("Product of the above two integers = %d\n", result);
}

Page | 2
5. Write a C program to compute the perimeter and area of a rectangle with a
height of 7 inches and width of 5 inches.
Expected Output:
Perimeter of the rectangle = 24 inches
Area of the rectangle = 35 square inches

Code:
#include <stdio.h>
int width;
int height;
int area;
int perimeter;

int main() {
height = 7;
width = 5;

perimeter = 2*(height + width);


printf("Perimeter of the rectangle = %d inches\n", perimeter);

area = height * width;


printf("Area of the rectangle = %d square inches\n", area);

6. Write a C program to compute the perimeter and area of a circle with radius 5.
Expected Output:
The perimeter of the circle is 31.4
The area of the circle is 78.5

Code:

#include <stdio.h>
int main(){
int r = 8;
float area = (3.14)*r*r;
float perimeter =2* (3.14)*r;
printf("The area of the circle is %f",area);
printf("The perimeter of the circle is %f",perimeter);
}
Page | 3
7. Write a C program that reads three floating-point values and checks if it is
possible to make a triangle with them. Determine the perimeter of the triangle if
the given values are valid.
Test Data:
Input the first number: 25
Input the second number: 15
Input the third number: 35
Expected Output:
Perimeter = 75.0

Code:
#include <stdio.h>
int main() {
float x, y, z, P, A;

printf("\nInput the first number: ");


scanf("%f", &x);

printf("\nInput the second number: ");


scanf("%f", &y);

printf("\nInput the third number: ");


scanf("%f", &z);

if(x < (y+z) && y < (x+z) && z < (y+x))


{
P = x+y+z;
printf("\nPerimeter = %.1f\n", P);

}
else
{
printf("Not possible to create a triangle..!");
}
}

Page | 4
8. Write a C program to convert specified days into years, weeks and days.
Note: Ignore leap year.
Test Data:
Number of days: 1329
Expected Output:
Years: 3
Weeks: 33
Days: 3

Code:

#include <stdio.h>

int main()

{
int days, years, weeks;
days = 1329;

// Converts days to years, weeks and days


years = days/365;
weeks = (days % 365)/7;
days = days- ((years*365) + (weeks*7));

printf("Years: %d\n", years);


printf("Weeks: %d\n", weeks);
printf("Days: %d \n", days);
return 0;
}

Page | 5
9. Write a C program to convert a given integer (in days) to years, months and
days, assuming that all months have 30 days and all years have 365 days.
Test Data:
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s)

Code:

#include <stdio.h>

int main() {
int n, y, m, d;

printf("Input no. of days: ");

scanf("%d", &n);

y = (int) n/365;

n = n-(365*y);

m = (int)n/30;

d = (int)n-(m*30);

printf(" %d Year(s) \n %d Month(s) \n %d Day(s)", y, m, d);


return 0;
}

Page | 6
10. Write a C program that accepts two item's weight and number of purchases
(floating point values) and calculates their average value.
Test Data:
Weight - Item1: 15
No. of item1: 5
Weight - Item2: 25
No. of item2: 4
Expected Output:
Average Value = 19.444444

Code:

#include <stdio.h>
int main()

double wi1, ci1, wi2, ci2, result;


printf("Weight - Item1: ");
scanf("%lf", &wi1);
printf("No. of item1: ");
scanf("%lf", &ci1);

printf("Weight - Item2: ");


scanf("%lf", &wi2);
printf("No. of item2: ");
scanf("%lf", &ci2);

result = ((wi1 * ci1) + (wi2 * ci2)) / (ci1 + ci2);


printf("Average Value = %f\n", result);
return 0;
}

Page | 7
11. Write a C program that accepts three integers and finds the maximum of three.
Test Data:
Input the first integer: 25
Input the second integer: 35
Input the third integer: 15
Expected Output:
Maximum value of three integers: 35

Code:

#include <stdio.h>

int main()

int x, y, z, result, max;


printf("\nInput the first integer: ");
scanf("%d", &x);
printf("\nInput the second integer: ");
scanf("%d", &y);
printf("\nInput the third integer: ");
scanf("%d", &z);

if(x>y && x>z){


max=x;
}
else if(y>z && y>x){
max=y;
}
else{
max=z;
}
printf("\nMaximum value of three integers: %d", max);

return 0;
}
Page | 8
12. Write a C program to calculate a bike’s average consumption from the given
total distance (integer value) travelled (in km) and spent fuel (in litters, float
number – 2 decimal points).
Test Data:
Input total distance in km: 350
Input total fuel spent in liters: 5
Expected Output:
Average consumption (km/lt) 70.000

Code:

#include <stdio.h>

int main()

int x;
float y;

printf("Input total distance in km: ");


scanf("%d",&x);

printf("Input total fuel spent in liters: ");


scanf("%f", &y);

printf("Average consumption (km/lt) %.3f ",x/y);

return 0;
}

Page | 9
13. Write a C program to convert a given integer (in seconds) to hours, minutes
and seconds.
Test Data:
Input seconds: 25300
Expected Output:
There are:
H:M:S - 7:1:40

Code:

#include <stdio.h>

int main() {

int sec, h, m, s;

printf("Input seconds: ");


scanf("%d", &sec);

h = (sec/3600);

m = (sec -(3600*h))/60;

s = (sec -(3600*h)-(m*60));

printf("H:M:S - %d:%d:%d\n",h,m,s);

return 0;
}

Page | 10
14. Write a C program that prints all even numbers between 1 and 50 (inclusive).
Test Data:
Even numbers between 1 to 50 (inclusive):
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

Code:
#include <stdio.h>
int main() {
int i;
printf("Even numbers between 1 to 50 (inclusive):\n");
for (i = 1; i <= 50; i++)
{
if(i%2 == 0)
{
printf("%d ", i);
}
}
}

15. Write a C program that prints all odd numbers between 1 and 50 (inclusive).
Test Data:
Odd numbers between 1 to 50 (inclusive):
Expected Output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

Code:
#include <stdio.h>
int main() {
int i;
printf("Even numbers between 1 to 50 (inclusive):\n");
for (i = 1; i <= 50; i++)
{
if(i%2 != 0)
{
printf("%d ", i);
}
}
}

Page | 11
16. Write a C program that reads 5 numbers and sums all odd values between
them.
Test Data:
Input the first number: 11
Input the second number: 17
Input the third number: 13
Input the fourth number: 12
Input the fifth number: 5
Expected Output:
Sum of all odd values: 46

Code:

#include <stdio.h>
int main() {
int j, numbers[5],total=0;

printf("\nInput the first number: ");


scanf("%d", &numbers[0]);

printf("\nInput the second number: ");


scanf("%d", &numbers[1]);

printf("\nInput the third number: ");


scanf("%d", &numbers[2]);

printf("\nInput the fourth number: ");


scanf("%d", &numbers[3]);

printf("\nInput the fifth number: ");


scanf("%d", &numbers[4]);

for(j = 0; j < 5; j++) {


if((numbers[j]%2) != 0)
{
total += numbers[j];
}
}
printf("\nSum of all odd values: %d", total);

Page | 12
17. Write a C program that reads 5 numbers and sums all even values between them.
Test Data:
Input the first number: 11
Input the second number: 16
Input the third number: 13
Input the fourth number: 12
Input the fifth number: 2
Expected Output:
Sum of all even values: 30

Code:

#include <stdio.h>
int main() {
int j, numbers[5],total=0;

printf("\nInput the first number: ");


scanf("%d", &numbers[0]);

printf("\nInput the second number: ");


scanf("%d", &numbers[1]);

printf("\nInput the third number: ");


scanf("%d", &numbers[2]);

printf("\nInput the fourth number: ");


scanf("%d", &numbers[3]);

printf("\nInput the fifth number: ");


scanf("%d", &numbers[4]);

for(j = 0; j < 5; j++) {


if((numbers[j]%2) == 0)
{
total += numbers[j];
}
}
printf("\nSum of all even values: %d", total);

Page | 13
18. Write a C program that reads 5 numbers and counts the number of positive
numbers and negative numbers.
Test Data:
Input the first number: 5
Input the second number: -4
Input the third number: 10
Input the fourth number: 15
Input the fifth number: -1
Expected Output:
Number of positive numbers: 3
Number of negative numbers: 2
Code:

#include <stdio.h>
int main() {
float numbers[5];
int j, pctr=0, nctr=0;

printf("\nInput the first number: ");


scanf("%f", &numbers[0]);
printf("\nInput the second number: ");
scanf("%f", &numbers[1]);
printf("\nInput the third number: ");
scanf("%f", &numbers[2]);
printf("\nInput the fourth number: ");
scanf("%f", &numbers[3]);
printf("\nInput the fifth number: ");
scanf("%f", &numbers[4]);

for(j = 0; j < 5; j++) {


if(numbers[j] > 0)
{

pctr++;
}
else if(numbers[j] < 0)
{
nctr++;
}
}
printf("\nNumber of positive numbers: %d", pctr);
printf("\nNumber of negative numbers: %d", nctr);
}

Page | 14
19. Write a C program that reads 5 numbers, counts the number of positive numbers,
and prints out the average of all positive values.
Test Data:
Input the first number: 5
Input the second number: 8
Input the third number: 10
Input the fourth number: -5
Input the fifth number: 25
Expected Output:
Number of positive numbers: 4
Average value of the said positive numbers: 12.00
Code:
#include <stdio.h>

int main() {

float numbers[5],total=0, avg;


int j, pctr=0;
printf("\nInput the first number: ");
scanf("%f", &numbers[0]);
printf("\nInput the second number: ");
scanf("%f", &numbers[1]);
printf("\nInput the third number: ");
scanf("%f", &numbers[2]);
printf("\nInput the fourth number: ");
scanf("%f", &numbers[3]);
printf("\nInput the fifth number: ");
scanf("%f", &numbers[4]);

for(j = 0; j < 5; j++) {

if(numbers[j] > 0)

{
pctr++;
total += numbers[j];
}
}

avg = total/pctr;
printf("\nNumber of positive numbers: %d", pctr);
printf("\nAverage value of the said positive numbers: %.2f", avg);
}

Page | 15
20. Write a C program to check whether a given integer is positive even, negative
even, positive odd or negative odd. Print even if the number is 0.
Test Data:
Input an integer: 13
Expected Output:
Positive Odd

Code:
#include <stdio.h>
int main() {

int x;
printf("Input an integer: ");
scanf("%d", &x);

if(x == 0){
printf("Positive\n");
}
else if(x < 0 && (x%2) != 0)
{
printf("Negative Odd\n");
}
else if(x < 0 && (x%2) == 0)
{
printf("Negative Even\n");
}
else if(x > 0 && (x%2) != 0)
{
printf("Positive Odd\n");
}
else if(x > 0 && (x%2) == 0)
{
printf("Positive Even\n");
}

return 0;
}

Page | 16
21. Write a C program to find and print the square of all the even values from 1
to a specified value.
Test Data:
List of square of each one of the even values from 1 to a 4:
Expected Output:
2^2 = 4
4^2 = 16

Code:

#include <stdio.h>
int main() {
int x, i;

printf("Input an integer: ");


scanf("%d", &x);

printf("List of square of each one of the even values from 1 to a %d :\n",x);

for(i = 2; i <= x; i++) {


if((i%2) == 0) {
printf("%d^2 = %d\n", i, i*i);
}
}

return 0;
}

Page | 17
22. Write a C program to read an amount (integer value) and break the amount into
the smallest possible number of bank notes.
Test Data:
Input the amount: 375
Expected Output:
There are:
3 Note(s) of 100.00
1 Note(s) of 50.00
1 Note(s) of 20.00
0 Note(s) of 10.00
1 Note(s) of 5.00
0 Note(s) of 2.00
0 Note(s) of 1.00

Code:
#include <stdio.h>
int main() {
int amt, total;

printf("Input the amount: ");


scanf("%d",&amt);

total = (int)amt/100;
printf("There are: ");
printf("\n%d Note(s) of 100.00\n", total);

amt = amt-(total*100);
total = (int)amt/50;
printf("%d Note(s) of 50.00\n", total);

amt = amt-(total*50);
total = (int)amt/20;
printf("%d Note(s) of 20.00\n", total);

amt = amt-(total*20);
total = (int)amt/10;
printf("%d Note(s) of 10.00\n", total);

amt = amt-(total*10);
total = (int)amt/5;
printf("%d Note(s) of 5.00\n", total);

Page | 18
amt = amt-(total*5);
total = (int)amt/2;
printf("%d Note(s) of 2.00\n", total);

amt = amt-(total*2);
total = (int)amt/1;
printf("%d Note(s) of 1.00\n", total);

return 0;
}

23. Write a C program that reads two integers and checks whether they are
multiplied or not.
Test Data:
Input the first number: 5
Input the second number: 15
Expected Output:
Multiplied!
Code:
#include <stdio.h>

int main() {
int x, y;
printf("\nInput the first number: ");
scanf("%d", &x);
printf("\nInput the second number: ");
scanf("%d", &y);

if(x > y)
{
int temp;
temp = x;
x = y;
y = temp;
}

if((y % x)== 0)
{
printf("\nMultiplied!\n");

Page | 19
}
else
{
printf("\nNot Multiplied!\n");
}

return 0;
}

24. Write a C program that reads an integer between 1 and 12 and prints the
month of the year in English.
Test Data:
Input a number between 1 to 12 to get the month name: 8
Expected Output:
August

Code:
#include <stdio.h>

int main() {
int mno;

printf("\nInput a number between 1 to 12 to get the month name: ");


scanf("%d", &mno);

switch(mno) {
case 1 : printf("January\n"); break;
case 2 : printf("February\n"); break;
case 3 : printf("March\n"); break;
case 4 : printf("April\n"); break;
case 5 : printf("May\n"); break;
case 6 : printf("June\n"); break;
case 7 : printf("July\n"); break;
case 8 : printf("August\n"); break;
case 9 : printf("September\n"); break;
case 10 : printf("October\n"); break;
case 11 : printf("November\n"); break;
case 12 : printf("December\n"); break;
default : printf("Input a number between 1 to 12.");
}
return 0;
}

Page | 20
25. Write a C program to check if two numbers in a pair are in ascending order or
descending order.
Test Data:
Input a pair of numbers (for example 10,2 : 2,10):
Input first number of the pair: 10
Expected Output:
Input second number of the pair: 2
The pair is in descending order
Code:
#include <stdio.h>
int main()
{
int x, y, i, total = 0;

printf("\nInput a pair of numbers (for example 10,2 : 2,10):");


printf("\nInput first number of the pair: ");
scanf("%d", &x);

printf("\nInput second number of the pair: ");


scanf("%d", &y);

if (x>y)
{
printf("The pair is in descending order!");
}
else
{
printf("The pair is in ascending order!");
}
}

Page | 21
26. Write a C program to read a password until it is valid. For wrong password
print "Incorrect password" and for correct password print, "Correct password" and
quit the program. The correct password is 1234.
Test Data:
Input the password: 1234
Expected Output:
Correct password

Code:
#include <stdio.h>
int main() {
int pass, x=10;

while (x!=0)
{
printf("\nInput the password: ");
scanf("%d",&pass);

if (pass==1234)
{
printf("Correct password");
x=0;
}
else
{
printf("Wrong password, try another");
}
printf("\n");
}
return 0;
}

Page | 22

You might also like