💡Question 1
Write a C program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal.
Answer
#include <stdio.h>
int main() {
int result = 87 - 15;
printf("Subtracting 15 from 87 gives: %i", result);
return 0;
}
💡Question 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;
}
Here are the syntactic errors in the provided program:
- The Void in int main (Void) should be lowercase, i.e., void.
- The opening parenthesis after int main (Void) should be a curly brace {.
- The data type INT should be in lowercase, i.e., int.
- There is a missing semicolon at the end of the line sum = 25 + 37 – 19.
- The closing comment marker // after DISPLAY RESULTS is placed incorrectly. It should be before the printf line.
Corrected version of the program:
#include <stdio.h>
int main(void) {
// COMPUTE RESULT
int sum;
sum = 25 + 37 - 19;
// DISPLAY RESULTS
printf("The answer is %i\n", sum);
return 0;
}
💡Question 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;
}
Answer
answer
is initialized to 100.result
is assigned the value of answer - 10
, which is 100 - 10 = 90.- The
printf
statement prints "The result is %i\n", where %i
is a placeholder for an integer. It prints the value of result + 5
. result + 5
is 90 + 5 = 95.
💡Question 4Int
- Invalid. Variable names in C are case-sensitive, andint
is a reserved keyword in C for declaring integer variables. Using a variation such asInt
is not allowed.char
- Invalid.char
is a reserved keyword in C for character data type declaration. Using it as a variable name is not allowed.6_05
- Invalid. Variable names cannot start with a digit in C. Although underscores are allowed, a variable name cannot start with a digit.floating
- Invalid.floating
is a reserved keyword in C. Therefore, using it as a variable name is not allowed.A$
- Invalid. Variable names cannot contain special characters like$
in C.
0x10.5
- Invalid. Floating-point constants in hexadecimal notation cannot have fractional parts.0X0G1
- Invalid. Hexadecimal constants cannot contain the letter 'G'.98.6F
- Invalid. Suffix 'F' is not a valid suffix for a floating-point constant. It should be 'f' for float or 'L' for long double.98.7U
- Invalid. Suffix 'U' is not a valid suffix for a floating-point constant.17777s
- Invalid. Suffix 's' is not a valid suffix for an integer constant.0996
- Invalid. In C, octal constants cannot start with '0' followed by another digit other than '0'.1.2Fe-7
- Invalid. 'Fe' is not a valid part of a floating-point constant.15,000
- Invalid. Commas are not allowed in constants.
This program assigns the character 'd' to the variable c
, and then assigns the value of c
to the variable d
. Finally, it prints the value of d
. Since the value of c
is 'd', after assigning it to d
, the value of d
will also be 'd'.
So, the output of the program will be: d = d
Write C Programs for the requirements given below
💡Question 7
Convert given value in Meter to centimeter.
Answer
#include <stdio.h>
int main() {
float meters, centimeters;
printf("Enter the value in meters: ");
scanf("%f", &meters);
centimeters = meters * 100;
printf("%.2f meters is equal to %.2f centimeters.\n", meters, centimeters);
return 0;
}
💡Question 8
Calculate the volume of a cylinder. PI * r2 h
Answer
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, height, volume;
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
volume = PI * radius * radius * height;
printf("The volume of the cylinder is: %.2f cubic units.\n", volume);
return 0;
}
💡Question 9
Calculate average marks of 4 subjects which, entered separately.
Answer
#include <stdio.h>
int main() {
float marks[4];
float sum = 0, average;
int i;
for (i = 0; i < 4; i++) {
printf("Enter marks for subject %d: ", i + 1);
scanf("%f", &marks[i]);
sum += marks[i];
}
average = sum / 4;
printf("Average marks: %.2f\n", average);
return 0;
}
💡Question 10
Convert the given temperature in Celsius to Fahrenheit. T(°F) = T(°C) × 1.8 + 32
Answer
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter the temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = celsius * 1.8 + 32;
printf("%.2f Celsius is equal to %.2f Fahrenheit.\n", celsius, fahrenheit);
return 0;
}
💡Question 11
Find the value of y using y = 3.5x+5 at x = 5.23.
Answer
#include <stdio.h>
int main() {
float x = 5.23;
float y;
y = 3.5 * x + 5;
printf("The value of y at x = %.2f is: %.2f\n", x, y);
return 0;
}
💡Question 12
Find the cost of 5 items if the unit price is 10.50 Rupees.
Post a Comment