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

5db95f3c4c7d75a5c62b6bf6d89328c8125f2ab2