Showing posts with label Branching. Show all posts
Showing posts with label Branching. Show all posts

Sunday, August 24, 2025

Viva questions based on C Decision Making statements with Answers

Viva Questions on C Decision Making Statements with Answers

(if, if-else, nested if, switch, conditional operator)

1. What is a decision-making statement in C?

Answer:

A decision-making statement allows the program to take different actions based on conditions. Examples are if, if-else, nested if, switch, and the conditional (ternary) operator ?:.

2. What is the difference between if and if-else statements?

Answer:

if executes a block of code only if the condition is true.
if-else provides an alternative block of code to execute when the condition is false.

3. Can we use multiple if-else statements in C?

Answer:

Yes, we can use multiple if-else if constructs, also known as ladder if-else, to check multiple conditions sequentially.

4. What is a nested if statement?

Answer:

A nested if means writing an if statement inside another if or else block. It is useful when multiple levels of decision-making are required.

5. How does a switch statement work in C?

Answer:

A switch evaluates an expression and compares it with multiple case labels. When a match is found, the corresponding block is executed until a break statement is encountered.

6. What happens if we don’t use break in a switch case?

Answer:

Without break, the program executes the matched case and then continues executing subsequent cases (fall-through) until it encounters a break or the end of the switch block.

7. Can a switch statement work with floating-point values?

Answer:

No, in C, a switch statement works only with integer and character values, not with float or double.

8. What is the difference between if-else and switch?

Answer:

if-else can handle complex conditions (logical expressions, ranges, multiple conditions).
switch is limited to checking equality of a single expression against constant values but is often faster and more readable.

9. What is the conditional (ternary) operator in C?

Answer:

The conditional operator ?: is a shorthand for if-else.

Example:

int max = (a > b) ? a : b;

This assigns a to max if a > b, otherwise assigns b.

10. Is it possible to use a switch inside another switch?

Answer:

Yes, we can have nested switch statements, but it is not commonly used because it reduces readability.

11. What is the difference between = and == in decision-making?

Answer:

= is the assignment operator (assigns value).
== is the equality operator (compares two values).
Using = instead of == inside conditions is a common error.

12. Can if statements be written without curly braces {}?

Answer:

Yes, if there is only one statement inside the if or else block, curly braces are optional. But for multiple statements, curly braces are required.


 < Back to Question Bank >

 

Sunday, January 23, 2022

Check whether a character is an alphabet, digit or special character

This program is based on C Programming Decision making concept (use of if..else statement).

Problem definition: Write a C program to check whether a given character is an alphabet, digit or special character.

#include <stdio.h>
int main()  
{  
    char chr;  

    printf("Enter a character: ");  
    scanf("%c", &chr);  
  
    /* logic to check whether it is an alphabet */  
    if((chr >= 'a' && chr<='z') || (chr>='A' && chr<='Z'))  
    {  
        printf("It is an alphabet.\n");
    }  
    else if(chr>='0' && chr<='9') /* to check digit */  
    {  
        printf("It is a digit.\n");  
    }  
    else /* else special character */
    {  
        printf("It is a special character.\n");  
    }  
}

Sample output 1

Enter a character: A
It is an alphabet.

Sample output 2

Enter a character: 5
It is a digit.

Sample output 3
Enter a character: @
It is a special character.




Friday, March 6, 2020

Sum of series 1 + 1/2 + 1/3 + 1/4 + ....N

C Program to print sum of series 1 + 1/2 + 1/3 + 1/4 + ....N for the given value of N.

For example, if N=5, output will be sum of 1 + 1/2 + 1/3 + 1/4 + 1/5, i.e. 2.28 

#include<stdio.h>
int main()
{
  int i, N;
  float sum=0;
  printf("Enter N:");
  scanf("%d", &N);
  if(N<1){
  printf("Enter value of N > 0.");
  exit(1);
  }
  for(i=1; i<=N; i++)
  {
   if(i==1)
   {
  sum=1;
  printf("Series = 1");
   }
   else
   {
     sum = sum + (float)1/i;
     printf(" + 1/%d", i);
   }
  }
  printf("\nSum of the series = %0.2f", sum);
  return 0; 
}

Output of program

Enter N:5
Series = 1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of the series = 2.28



Read more C Programming Series Programs here...

< Back to Home Page >

Saturday, May 13, 2017

C program without Semicolon




C program without Semicolon.

#include<stdio.h>
void main()
{
    if(printf("Hello World"))
    {
   
    }
}

Output of program

Hello World

Note: Here printf( ) function is passed as a parameter to if statement; hence semi-colon is not needed at the end of printf() statement.


Saturday, May 6, 2017

Program to check leap year.

A leap year is a calendar year containing one additional day added to keep the calendar year synchronized with the astronomical or seasonal year.

Leap year has 366 days in a year(February month with 29 days).
Non-leap year has 365 days in a year.

//Write a C program to check leap year.
#include <stdio.h>

int main ()
{
int year;
printf("Enter Year to check:");
scanf("%d",&year);

if(year%400 == 0 || (year%100 != 0 && year%4 == 0)){
printf("%d is leap year.",year);
}
else{
printf("%d is not leap year.",year);
}
return 0;
}


Output 1:

Enter Year to check:2016
2016 is leap year.

Output 2:

Enter Year to check:2017
2016 is not leap year.

Tuesday, December 27, 2016

Accept name and display name with good morning message.

C program to accept name and display the name with good morning message.

#include<stdio.h>
int main()
{
  int time;
  char name[20];
 
  printf("Enter your name:");
  gets(name);
 
  printf("Enter time:");
  scanf("%d", &time);
 
  if(time < 12)
  {
     printf("Good Morning...%s", name);
  }
  return 0;
}

Output of the program:

Enter your name:Manish
Enter time:10
Good Morning...Manish




< HOME >

* * * * *

Friday, August 26, 2016

C Program to check given string is Palindrome or not.



// C Program to check given string is Palindrome or not.

#include <stdio.h>
#include <string.h>

int main()
{
   char a[10], b[10];

   printf("Enter your string:\n");
   gets(a);

   strcpy(b,a);
   strrev(b);

   if (strcmp(a,b) == 0)
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
   return 0;
}


Output of the Program:

Enter your string:
ABA
Entered string is a palindrome.


Wednesday, August 24, 2016

C program to display Good morning message based on given time.

Conversion of flowchart to C program: Display Good morning message based on given time.

#include<stdio.h>
int main()
{
     int time;
     printf("Enter your time:");
     scanf("%d",&time);
     if(time < 12)
     {
           printf("Good Morning...");
     }
     return 0;
}

Output of the Program:

Enter your time:10
Good Morning...

Friday, June 10, 2016

User input validation to check a number.


//User input validation to check a number.

#include<stdio.h>
int main()
{
char chr;
printf("Enter a number: ");
scanf("%c",&chr);

if(isdigit(chr))
printf("It is a number.");
else
printf("It is not a number.");
 return 0;
}

//Output 1
Enter a number: 5
It is a number.

//Output 2
Enter a number: a
It is not a number.

Wednesday, April 27, 2016

C program to use Arrow keys.

This program explains how to use Arrow keys in C program.


#include<stdio.h>
int main()
{
 int chr1, chr2;
 printf("Press any arrow key...\n");
 chr1 = getch();
 if (chr1 == 0xE0) //to check scroll key interrupt
 {
  chr2 = getch();  //to read arrow key
  switch(chr2)
  {
   case 72: printf("UP ARROW KEY PRESSED\n");
      break;
   case 80: printf("DOWN ARROW KEY PRESSED\n");
      break;
   case 75: printf("LEFT ARROW KEY PRESSED\n");
      break;
   case 77: printf("RIGHT ARROW KEY PRESSED\n");
      break;
   default: printf("OTHER KEY PRESSED: %d %d\n", chr1, chr2);
      break;
  };
 }
 return 0;
}

Output of program

Press any arrow key...
UP ARROW KEY PRESSED

Saturday, March 12, 2016

Program to check whether entered number is odd or even.

#include<stdio.h>
int main()
{
 int number;

 printf("Enter number:");
 scanf("%d",&number);

 if(number%2 == 0)
  printf("Entered number is even.");
 else
  printf("Entered number is odd.");
 return 0;
}
/* Sample Output
Enter number:5
Entered number is odd.
*/

Thursday, February 11, 2016

C Program to find largest number from given three numbers.

#include<stdio.h>
int main()
{
int num1, num2, num3;

  printf("Enter number1:");
  scanf("%d",&num1);

  printf("Enter number2:");
  scanf("%d",&num2);

  printf("Enter number3:");
  scanf("%d",&num3);

  if(num1 > num2 && num1 > num3)
  {
    printf("Number1 is largest number.");
  }
  if(num2 > num1 && num2 > num3)
  {
    printf("Number2 is largest number.");
  }
  if(num3 > num1 && num3 > num2)
  {
    printf("Number3 is largest number.");
  }
    return 0;
}
/*
Enter number1:5
Enter number2:4
Enter number3:3
Number1 is largest number.
*/
//Note: Add logic to update this program when two/three numbers are same.

C Program to find bigger number.

#include<stdio.h>
int main()
{
int num1, num2;

  printf("Enter number1:");
  scanf("%d",&num1);

  printf("Enter number2:");
  scanf("%d",&num2);
  if(num1 > num2)
  {
    printf("Number1 is big.");
  }
  if(num2 > num1)
  {
    printf("Number2 is big.");
  }
  if(num1 == num2)
  {
    printf("Number1 and Number2 are equal.");
  }
    return 0;
}
/*
Enter number1:5
Enter number2:7
Number2 is big.
*/

Wednesday, February 10, 2016

Example of nested if statement. Check: A is greater than B and C.

#include <stdio.h>
void main ()
{
int a, b, c;
  printf("Enter number A:");
  scanf("%d",&a);

  printf("Enter number B:");
  scanf("%d",&b);

  printf("Enter number C:");
  scanf("%d",&c);

  if( a > b )
  {
    if( a > c )
      printf("A is greater than B and C.");
  }
}
/* Output
Enter number A:5
Enter number B:4
Enter number C:2
A is greater than B and C.
*/

C Program to convert given number (between 1 to 5) into words using switch.

#include<stdio.h>
int main ()
{
int number;
printf("Enter your number:");
scanf("%d",&number);
switch(number)
{
case 1 :printf("One");
          break;
case 2 :printf("Two");
          break;
case 3 :printf("Three");
          break;
case 4 :printf("Four");
          break;
case 5 :printf("Five");
          break;
default :printf("Invalid number\n" );
}
 return 0;
}

Output of the program:

Enter your number:5
Five

* * * * *

Check: Given number is positive or not using if..else statement.

#include<stdio.h>
void main()
{
int number;

printf("Enter number:");
scanf("%d",&number);

if(number > 0)
printf("Number is positive.");
else
printf("Number is not positive.");
}
/*Output
Enter number:11
Number is positive.
*/

Check: Given number is positive or not using if statement.

#include<stdio.h> 
void main()
{
int number;

printf("Enter number:");
scanf("%d",&number);

if(number > 0)
printf("Number is positive.");

if(number <= 0)
printf("Number is not positive.");
}
/* Output
Enter number:5
Number is positive.
*/