Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
106 views

MAT09-Example Exercises (Week 8)

This document contains 10 coding exercises in C programming language. Each exercise provides sample code to perform a specific task such as printing name, date of birth and mobile number, printing shapes like block F and big C, reversing characters, calculating perimeter and area of rectangle and circle, displaying multiple variables, converting days to years and weeks, calculating sum and product of two integers, and calculating average value of items. The given code samples demonstrate basic input/output functions, arithmetic operations, and control flow statements in C.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

MAT09-Example Exercises (Week 8)

This document contains 10 coding exercises in C programming language. Each exercise provides sample code to perform a specific task such as printing name, date of birth and mobile number, printing shapes like block F and big C, reversing characters, calculating perimeter and area of rectangle and circle, displaying multiple variables, converting days to years and weeks, calculating sum and product of two integers, and calculating average value of items. The given code samples demonstrate basic input/output functions, arithmetic operations, and control flow statements in C.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MAT09 - EXAMPLE EXERCISES (WEEK 8)

1. Write a C program to print your name,


date of birth and mobile number.  #include <stdio.h>

int main()
{
Expected Output: printf("Name : Alexandra Abramov\n");
Name : Alexandra Abramov
printf("DOB : July 14, 1975\n");
DOB : July 14, 1975
Mobile : 99-9999999999 printf("Mobile : 99-9999999999\n");
return 0;
}

2. Write a C program to print a block F


using hash (#), where the F has a height of six characters and width of five and four
characters. And also to print a big 'C'.
#include <stdio.h>
Expected Output: int main()
###### {
printf("######\n");
#
printf("#\n");
# printf("#\n");
##### printf("#####\n");
# printf("#\n");
# printf("#\n");
# printf("#\n");
######
printf(" ######\n");
## ## printf(" ## ##\n");
# printf(" #\n");
# printf(" #\n");
# printf(" #\n");
# printf(" #\n");
printf(" #\n");
#
printf(" ## ##\n");
## ## printf(" ######\n");
###### return 0;
}

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


Test Characters: 'X', 'M', 'L'
Expected Output:
The reverse of XML is LMX
#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;
}
4. 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 wthe rectangle = 35 square inches

#include <stdio.h>

int main() {
/* height and width of a rectangle in inches */
int width, height;
int area, perimeter;

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);

return 0;
}

5. Write a C program to compute the perimeter and area of a circle with a given radius.
Expected Output:
Perimeter of the Circle = 37.680000 inches
Area of the Circle = 113.040001 square inches

#include <stdio.h>
int main() {
int radius;
float area, perimeter;
radius = 6;

perimeter = 2*3.14*radius;
printf("Perimeter of the Circle = %f inches\n", perimeter);

area = 3.14*radius*radius;
printf("Area of the Circle = %f square inches\n", area);

return 0;
}

6. Write a C program to display multiple variables. 


Sample Variables :
a+ c, x + c,dx + x, ((int) dx) + ax, a + x, s + b, ax + b, s + c, ax + c, ax + ux
Expected Output :

a + c = 212
x + c = 89.134590
dx + x = 3.276183
((int) dx) + ax = 1234567891
a + x = 127.134590
s + b = 16388
ax + b = 1234580235
s + c = 4130
ax + c = 1234567977
ax + ux = 3776135780

#include <stdio.h>

int main()
{
int a = 125, b = 12345;
long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;

printf("a + c = %d\n", a + c);


printf("x + c = %f\n", x + c);
printf("dx + x = %f\n", dx + x);
printf("((int) dx) + ax = %ld\n", ((int) dx) + ax);
printf("a + x = %f\n", a + x);
printf("s + b = %d\n", s + b);
printf("ax + b = %ld\n", ax + b);
printf("s + c = %hd\n", s + c);
printf("ax + c = %ld\n", ax + c);
printf("ax + ux = %lu\n", ax + ux);

return 0;
}

7. Write a C program to convert specified days into years, weeks and days. 
(Note: Ignore leap year. You can also change the test data to check if the desired output
is attained)

Test Data:
Number of days : 1329
Expected Output :
Years: 3
Weeks: 33
Days: 3

#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;
}
8. Write a C program that accepts two integers from the user and calculate 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
#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;
}

9. Write a C program that accepts two integers from the user and calculate 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
#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);

return 0;
}

10. 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. 
Test Data :
#include <stdio.h>
Weight - Item1: 15 int main()
No. of item1: 5 {
Weight - Item2: 25 double wi1, ci1, wi2, ci2, result;
No. of item2: 4 printf("Weight - Item1: ");
Expected Output: scanf("%lf", &wi1);
Average Value = 19.444444 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;
}

You might also like