Differentiate between if and if-else selection structures.

The if and if-else selection structures are both used to make decisions in programming. In an if statement, a condition is evaluated, and if the condition is true, the subsequent block of code is executed. If the condition is false, no code is executed unless an alternative is explicitly provided. The if-else structure, however, provides … Read more

Write a program that takes input from the user for marks of five subjects, calculates the percentage, and prints the grade based on the following criteria: 90% and above: A+ 80%-89%: A 70%-79%: B 60%-69%: C Below 60%: Fail

#include <stdio.h> int main() { float marks[5], total = 0, percentage; char grade; // Taking input for marks printf(“Enter marks for five subjects:\n”); for (int i = 0; i < 5; i++) { printf(“Subject %d: “, i + 1); scanf(“%f”, &marks[i]); total += marks[i]; // Adding marks to total } // Calculating percentage percentage = … Read more

Explain different types of format specifiers used in C with examples.

In C, format specifiers are used with functions like printf() and scanf() to control the input and output format of variables. Below are some commonly used format specifiers: %d or %i: Used for printing or reading integers. int num = 10; printf(“%d”, num); // Output: 10 %f: Used for printing or reading floating-point numbers (e.g., … Read more

Write a program to check whether a given number is even or odd using the modulus operator.

#include <stdio.h> int main() { int num; // Take input from the user printf(“Enter a number: “); scanf(“%d”, &num); // Check if the number is even or odd using modulus operator if (num % 2 == 0) { printf(“%d is even.\n”, num); } else { printf(“%d is odd.\n”, num); } return 0; } Explanation: The … Read more

Why do we use the return 0 statement in the main function of a C program?

∅In C, the main function is the entry point of the program. The return 0; statement at the end of the main function indicates successful program execution. In C, the main function returns an integer value, and by convention, returning 0 signifies that the program executed successfully without errors. Non-zero return values can be used … Read more

What is the difference between an integer and a floating-point number in C?

In C, integers and floating-point numbers are both used to store numerical data, but they differ in the type of numbers they can represent. An integer is a data type used to store whole numbers, both positive and negative, without any fractional part (e.g., -1, 0, 100). The int data type in C is typically … Read more

Write a program that reads the length of one side of a cube and prints its volume.

#include <stdio.h> int main() { float side, volume; // Reading the length of one side of the cube printf(“Enter the length of one side of the cube: “); scanf(“%f”, &side); // Calculating volume volume = side * side * side; // Printing the volume printf(“Volume of the cube: %.2f\n”, volume); return 0; } Related Questions: … Read more