What will be the Output of the Following C Code
What will be the Output of the Followingย  Code

In this blog, โ€œWhat will be the output of the following codeโ€, weโ€™ll be taking you through some questions which will prepare you for your coding rounds and eventually improve your programming skills. These will help you get clarity on how the questions can be asked in your next coding round.ย 

Output-based questions can be one of the best ways to test your programming skills. It helps with attention to detail and improves your problem-solving abilities. Multiple coding tests prepare you to crack the coding round of big companies like IBM and Accenture and help you to build critical thinking. Eventually, you solve these questions in a very short period and you solve complex questions easily.ย 

Top 10 C Programming Questions and Answers related with

โ€œWhat will be the output of the following codeโ€:

  1. What will be the Output of the Following C Code?

void main() {

ย ย char ch = 'a';

ย ย if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

ย ย ย ย printf("Vowel");

ย ย } else {

ย ย ย ย printf("Consonant");

ย ย }

}

Output: Vowel

Explanation:

The given code here checks if the character ch is equal to either โ€˜aโ€™, โ€˜eโ€™, โ€˜iโ€™, โ€˜oโ€™, or โ€˜uโ€™. Since โ€˜aโ€™ is assigned to ch, the if condition becomes true. Therefore, the code prints โ€œVowelโ€.

ย 

  1. What will be the Output of the Following C Code?

char ch;

printf("Enter a character: ");

scanf("%c", &ch);


if (ch >= 'a' && ch <= 'z') {

ย ย printf("Lowercase alphabet");

} else if (ch >= 'A' && ch <= 'Z') {

ย ย printf("Uppercase alphabet");

} else if (ch >= '0' && ch <= '9') {

ย ย printf("Digit");

} else {

ย ย printf("Special character");

}

ย 

Output: The output here will depend on the user input.ย 

*Note:

If you enter a lowercase letter (a-z): โ€œLowercase alphabetโ€

And If you enter an uppercase letter (A-Z): โ€œUppercase alphabetโ€

If you enter a digit (0-9): โ€œDigitโ€

If you enter any other character: โ€œSpecial characterโ€ will be the output.ย 

ย 

Explanation:

The given code here uses scanf to read a character from the user and store it in ch.

Then, it proceeds to use nested if statements to check the range of ch:

if (ch >= โ€˜aโ€™ && ch <= โ€˜zโ€™): This checks if ch is a lowercase alphabet.

else if (ch >= โ€˜Aโ€™ && ch <= โ€˜Zโ€™): This checks if ch is an uppercase alphabet.

else if (ch >= โ€˜0โ€™ && ch <= โ€˜9โ€™): This checks if ch is a digit.

else: This prints โ€œSpecial characterโ€ if none of the above conditions are true.

ย 

  1. What will be the output of the following code in C++?

int main() {

ย ย int choice;

ย ย float radius, length, breadth;




ย ย std::cout << "Enter 1 for circle, 2 for rectangle: ";

ย ย std::cin >> choice;




ย ย if (choice == 1) {

ย ย ย ย std::cout << "Enter radius: ";

ย ย ย ย std::cin >> radius;

ย ย ย ย std::cout << "Area of circle: " << 3.14159 * radius * radius << std::endl;

ย ย } else if (choice == 2) {

ย ย ย ย std::cout << "Enter length: ";

ย ย ย ย std::cin >> length;

ย ย ย ย std::cout << "Enter breadth: ";

ย ย ย ย std::cin >> breadth;

ย ย ย ย std::cout << "Area of rectangle: " << length * breadth << std::endl;

ย ย } else {

ย ย ย ย std::cout << "Invalid choice." << std::endl;

ย ย }

ย ย return 0;

}

ย 

Output: The code here depends on the user input.ย 

GUIDE:

To proceed first the code prompts the user to enter a choice:

If the user enters 1: The program asks for the radius of the circle. Then, it eventually calculates the area of the circle using the formula ฯ€rยฒ and gives the result .ย 

If the user enters 2: The program asks for the length and breadth of the rectangle.

Then, it calculates the area of the rectangle using the formula lb and prints the result.

If the user enters anything else:

It prints an error message โ€œInvalid choice.โ€

Explanation:

The above-given code uses conditional statements (if and else if) to check the userโ€™s choice and perform calculations accordingly. It utilizes the input with std::cin and prints output with std::cout.

The code assumes the value of ฯ€ as 3.14159.

  1. What will be the output of the following code in C++?

int main() {

ย ย int score;




ย ย std::cout << "Enter your score: ";

ย ย std::cin >> score;




ย ย if (score >= 90) {

ย ย ย ย std::cout << "Grade: A" << std::endl;

ย ย } else if (score >= 80) {

ย ย ย ย std::cout << "Grade: B" << std::endl;

ย ย } else if (score >= 70) {

ย ย ย ย std::cout << "Grade: C" << std::endl;

ย ย } else if (score >= 60) {

ย ย ย ย std::cout << "Grade: D" << std::endl;

ย ย } else {

ย ย ย ย std::cout << "Grade: F" << std::endl;

ย ย }




ย ย return 0;

}

ย 

Output: The output depends on the user input.ย 

GUIDE:

If the user score is between 90 and 100: the program will print โ€œGrade: Aโ€

If the user score is between 80 and 89: the program will print โ€œGrade: Bโ€

If the user score is between 70 and 79: the program will print โ€œGrade: Cโ€

If the user score is between 60 and 69: the program will print โ€œGrade: Dโ€

If the user score is less than 60: the program will printย  โ€œGrade: Fโ€


Explanation:

The above code uses nested if statements to check the userโ€™s score and prints the associated grade. The program starts with the highest grade A and gradually moves down based on the grade to F. The code assumes that there are no scores above 100 or below 0.

Also Read:

  1. What will be the output of the following code in JAVA?

public class CheckPrime {

ย ย public static void main(String[] args) {

ย ย ย ย int number = 13;

ย ย ย ย boolean isPrime = true;




ย ย ย ย for (int i = 2; i < number / 2; i++) {

ย ย ย ย ย ย if (number % i == 0) {

ย ย ย ย ย ย ย ย isPrime = false;

ย ย ย ย ย ย ย ย break;

ย ย ย ย ย ย }

ย ย ย ย }




ย ย ย ย if (isPrime) {

ย ย ย ย ย ย System.out.println(number + " is prime.");

ย ย ย ย } else {

ย ย ย ย ย ย System.out.println(number + " is not prime.");

ย ย ย ย }

ย ย }

}

ย 

Output: 13 is prime.

Explanation:

The above-given code checks if a number is prime or not using a loop and a boolean flag. The loop iterates from 2 to half the number (excluding 1 and itself).

In the loop, if the number is divisible by any iterating value given the isPrime flag is set to false and eventually breaks the loop. After the loop, if isPrime is still true, the number is prime; otherwise, it prints not prime. The code prints the corresponding message based on the flagโ€™s value.

Also Read:

  1. What will be the output of the following code in JAVA?ย 

public class CheckNumber {

ย ย public static void main(String[] args) {

ย ย ย ย int number = 10;




ย ย ย ย if (number > 0) {

ย ย ย ย ย ย System.out.println(number + " is positive.");

ย ย ย ย } else if (number < 0) {

ย ย ย ย ย ย System.out.println(number + " is negative.");

ย ย ย ย } else {

ย ย ย ย ย ย System.out.println(number + " is zero.");

ย ย ย ย }

ย ย }

}

ย 

Output: 10 is positive.

Explanation:

The above-given code checks the sign of a number using nested if statements. If the given number is greater than 0, itโ€™s positive. If the number is less than 0, itโ€™s negative. If the number is neither greater than 0 nor less than 0, it must be 0.

The code prints the corresponding message based on the evaluation of the conditions.

ย 

  1. What will be the output of the following code in Python?ย 

price = float(input("Enter the price: "))

discount = float(input("Enter the discount percentage: "))




if discount > 0 and discount < 100:

ย ย ย ย discounted_price = price - (price * discount / 100)

ย ย ย ย print(f"Discounted price: ${discounted_price:.2f}")

else:

ย ย ย ย print("Invalid discount percentage.")

ย 

Output: It depends on the user input.ย 

GUIDE:

If you enter a valid discount percentage (between 0 and 100): The code calculates the discounted price using the formula price โ€“ (price * discount / 100).

And then proceeds to print the discounted price with two decimal places. If the user enters an invalid discount percentage (less than 0 or greater than 100):The code prints an error message โ€œInvalid discount percentage.โ€


Explanation:

The above-given code calculates the discounted price of a product based on the userโ€™s input. It first reads the price and discount percentage as floating-point values using float(input()). Then the code checks if the discount percentage is within the valid range using if and and operators.

If it is valid, the code calculates the discounted price and gives the output.ย  If it is invalid, the code prints an error message.

ย 

  1. What will be the output of the following code in Python?

char = input("Enter a character: ")


if char in "aeiou":

ย ย ย ย print(f"'{char}' is a vowel.")

else:

ย ย ย ย print(f"'{char}' is a consonant.")

ย 

Output: The output will depend on the user input.ย 

GUIDE:

If the user enters a vowel (a, e, i, o, u):

The code prints the character with a message saying โ€ is a vowel.โ€

ย 

If the user enters anything other than a vowel: The code prints the character with the message โ€ is a consonant.โ€

ย 

Explanation:

The above-given code checks if a user-entered character is a vowel or a consonant. It reads a character using input() and assigns it to char. Then, it proceeds to check if the character is present in the string โ€œaeiouโ€ using the in operator. If it is present, the code prints a message with the character and โ€œvowelโ€.

If it is not present, the code prints a message with the character and โ€œconsonantโ€.

ย 

  1. What will be the Output of the Following C Code?

#include <stdio.h>




int main() {

ย ย int number;




ย ย printf("Enter a number: ");

ย ย scanf("%d", &number);




ย ย if (number % 2 == 0) {

ย ย ย ย printf("%d is even.\n", number);

ย ย } else {

ย ย ย ย printf("%d is odd.\n", number);

ย ย }




ย ย return 0;

}

ย 

Output: The output depends on the user input.ย 

ย 

If the user enters an even number: The code prints the number with a message โ€ is even.โ€ If the user enters an odd number:

The code prints the number with the message โ€ is odd.โ€

ย 

Explanation:

This code checks if a user-entered number is even or odd and then prints the result.ย 

ย 

  1. What will be the output of the following code in C?ย 

#include <stdio.h>

int main() {

ย ย int number;

ย ย printf("Enter a number: ");

ย ย scanf("%d", &number);


ย ย if (number > 0) {

ย ย ย ย printf("%d is positive.\n", number);

ย ย } else if (number < 0) {

ย ย ย ย printf("%d is negative.\n", number);

ย ย } else {

ย ย ย ย printf("%d is zero.\n", number);

ย ย }

ย ย return 0;

}

ย 

Output: Depends on the user input.

The code reads the input and by using an if statement it checks if the number is greater than 0. For positive numbers, the code prints a message stating that the number is positive. If the number is not positive, the code uses an else if statement to check if it is less than 0. If it is, the code prints a message stating that the number is negative. If the number is not positive or negative itโ€™ll eventually print 0.ย 

Follow onย Fbย for more updates

Conclusion:

We hope that you enjoyed these โ€œWhat will be the output of the following codeโ€ questions and they helped you gain more clarity on how questions are asked in coding rounds and tests and how you can solve them. Let us know what you think about these questions in the comment section and follow our socials to get more coding questions like these.ย 


What will be the Output of the Following Code (Q & A) Pdfย 

Download for FREE