Tutorial_07
ICT/2022/139
R.S.R. Ranathunga
1. Write a program that subtracts the value 15 from 87 and displays the result, together with
an appropriate message, at the terminal.
#include <stdio.h>
int main ()
{
int a=87;
int b=15;
int result;
result=a-b;
printf("subtracts the value 15 from 87 and result is : %i\n", result);
return 0;
}
2. Identify the syntactic errors in the following program. Then type in and run the corrected
program to ensure you have correctly identified all the mistakes.
#include <stdio.h>
int main (Void)
(
INT sum;
/* COMPUTE RESULT
sum = 25 + 37 – 19
/* DISPLAY RESULTS //
printf ("The answer is %i\n" sum);
return 0;
}
corrected:
#include <stdio.h>
int main (void)
{
int sum;
/* COMPUTE RESULT*/
sum = 25 + 37 - 19;
/* DISPLAY RESULTS */
printf ("The answer is %i\n", sum);
return 0;
}
3. What output might you expect from the following program?
#include <stdio.h>
int main (void)
{
int answer, result;
answer = 100;
result = answer - 10;
printf ("The result is %i\n", result + 5);
return 0;
}
output:
The result is 95
4. Which of the following are invalid variable names? Why?
Int char 6_05 -invalid\int and char are keywords in C, Embedded spaces are not permitted.
Calloc Xx alpha_beta_routine - invalid\ Embedded spaces are not permitted.
floating _1312 z - invalid\ Embedded spaces are not permitted.
ReInitialize _ A$ - invalid\ Embedded spaces are not permitted and $ is not a valid character.
5. Which of the following are invalid constants? Why?
123.456 - valid\decimal constant
0x10.5 - invalid\Hexadecimal is not valid to floating point constant
0X0G1 - invalid\G is not valid letter for Hexadecimal constant
0001 - valid
0xFFFF - valid
123L - valid
0Xab05 - valid
0L - valid
-597.25 - valid
123.5e2 - valid
.0001 - valid
+12 - valid
98.6F - valid
98.7U - invalid\ "u" is not valid suffix letter
17777s - invalid\"s" is not valid for integer constant
0996 - invalid\"9" is not valid for octal constant
-12E-12 - valid
07777 - valid
1234uL - valid
1.2Fe-7 - valid
15,000 - invalid\ "," is not valid
1.234L - valid
197u - valid
100U - valid
0XABCDEFL - valid
0xabcu - valid
+123 - valid constant
6. What output would you expect from the following program?
#include <stdio.h>
int main (void)
{
char c, d;
c = 'd';
d = c;
printf ("d = %c\n", d);
return 0;
}
output:
d = d
Write C Programs for the requirements given below
7. Convert given value in Meter to centimeter.
#include <stdio.h>
int main ()
{
float meters, centimeters;
printf("Enter a value in meters : ");
scanf("%f", &meters);
centimeters= meters*100;
printf("The result is : %.2f\n", centimeters);
return 0;
}
8. Calculate the volume of a cylinder. PI * r2 h
#include <stdio.h>
int main ()
{
float PI = 3.14;
float radius, height, volume;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Enter height: ");
scanf("%f", &height);
volume= PI * radius * radius * height;
printf("The volume is: %.2f", volume);
return 0;
}
9. Calculate average marks of 4 subjects which, entered separately.
#include <stdio.h>
int main ()
{
int subject1, subject2, subject3, subject4;
float average;
printf("Enter first subject marks: ");
scanf("%i", &subject1);
printf("Enter second subject marks: ");
scanf("%i", &subject2);
printf("Enter third subject marks: ");
scanf("%i", &subject3);
printf("Enter fourth subject marks: ");
scanf("%i", &subject4);
average= (subject1+subject2+subject3+subject4)/4.0;
printf("Average is: %.2f", average);
return 0;
}
10. Convert the given temperature in Celsius to Fahrenheit.
T(°F) = T(°C) × 1.8 + 32
#include <stdio.h>
int main ()
{
float celsius, temperature;
printf("Enter the given temperature in Celsius: ");
scanf("%f", &celsius);
temperature= celsius*1.8+32;
printf("The Fahrenheit Value is: %.1f", temperature);
return 0;
}
11. Find the value of y using y = 3.5x+5 at x = 5.23.
#include <stdio.h>
int main ()
{
float x=5.23;
float y;
y=3.5*x+5;
printf("y value is : %.3f", y);
return 0;
}
12. Find the cost of 5 items if the unit price is 10.50 Rupees.
#include <stdio.h>
int main()
{
float unit_Price = 10.50;
float total_Cost= 0.0;
int i;
for (i = 1; i <= 5; ++i) {
total_Cost+= unit_Price;
}
printf("The total cost of 5 items is: %.2f\n", total_Cost);
return 0;
}
13. Enter the name, height, weight and gender of a person and calculate his/her BMI in Kg.
BMI = weight/ height2
#include <stdio.h>
int main()
{
char name[50], gender;
float height,weight, BMI;
printf("Enter name: ");
scanf("%s", &name);
printf("Enter gender: ");
scanf(" %c", &gender);
printf("Enter weight: ");
scanf("%f", &weight);
printf("Enter height: ");
scanf("%f", &height);
BMI= weight/(height*height);
printf("%s's BMI is: %f\n", name, BMI);
return 0;
}
14. Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a Length in inches, the output would be 42.926cm. (Hint: 1 inch = 2.54 centimeters.)
#include <stdio.h>
int main ()
{
float inches, centimeters;
printf("Enter inches value: ");
scanf("%f", &inches);
centimeters= inches*2.54;
printf("Centimeters value is: %.3f", centimeters);
return 0;
}
15. The figure gives a rough sketch of a running track. It includes a rectangular shape and
two semi-circles. The length of the rectangular part is 67m and breadth is 21m.Calculate
the distance of the running track.
#include <stdio.h>
int main ()
{
float PI=3.14, breadth=21.0, lenght=67.0, distance;
printf("Enter the length in meters: ");
scanf("%f", &lenght);
printf("Enter the breadth in meters: ");
scanf("%f", &breadth);
distance=2*PI*(breadth/2)+(2*lenght);
printf("The distance of the track(in meters) is: %.2f", distance);
return 0;
}
Comments
Post a Comment