Enjoy Conditional Programming in C using If…else and switch statement.

Table of Contents
< All Topics
Print

Enjoy Conditional Programming in C using If…else and switch statement.

1.Write a program to find that given number is odd even (using if… else statement)

The steps to create a program to find whether a given number is odd or even using if…else statement:

  1. Start by including the necessary header file for input/output operations in C, which isstdio.h.
  2. Define the main function.
  3. Declare a variable to store the input number, for example,int num;.
  4. Ask the user to input a number usingprintfandscanffunctions.
  5. Use anifstatement to check if the number is even. This can be done by checking if the remainder of the number divided by 2 is equal to 0, i.e.,if (numb % 2 == 0). If this condition is true, useprintfto display that the number is even.
  6. If the condition in step 5 is false, use anelsestatement to display that the number is odd.
  7. End the main function and the program.

Type the following code in your IDE (preferably Dev C++)

// ODD EEVEN using if...else statement
#include<stdio.h>

int main()
{
 int num;
 printf("Enter a positive integer: ");
 scanf("%d", &num);
 if(num%2 == 0)
 {
  printf("%d is EVEN.", num);
 }
 else
 {
  printf("%d is ODD.", num);
 }
 return 0;
}

2. Write a program to find that given number is odd even (using switch statement)

The steps to create a program to find whether a given number is odd or even using switch statement:

  1. Start by including the necessary header file for input/output operations in C, which isstdio.h.
  2. Define the main function.
  3. Declare a variable to store the input number and remainder, for example,int num, rem;.
  4. Ask the user to input a number usingprintfandscanffunctions.
  5. computenum % 2and store the result on variablerem.
  6. Use a switch statement to get the value of remainder. The possible remainder values are either 1 (i.e num is odd) or 0 (i.e num is even). Here we have used1undercase value:(useprintf()statement to print ODD) and zero (0) underdefault:(useprintf()statement to print EVEN) part of theswitch statement.
  7. End the main function and the program.

Type the following code in your IDE (preferablyDev C++)

// ODD EEVEN using switch statement
#include<stdio.h>

int main()
{
 int num, rem;
 printf("Enter positive integer: ");
 scanf("%d", &num);
 rem = num%2;
 switch(rem)
 {
  case 1: printf("%d is ODD.", num);
    break;
  default: printf("%d is EVEN.", num);
       break;
 }
 return 0;
}

3. Write a program to find that given number is odd even (using conditional operator. i.e. ?:;)

The steps to create a program to find whether a given number is odd or even using conditional operator-based statement:

  1. Start by including the necessary header file for input/output operations in C, which isstdio.h.
  2. Define the main function.
  3. Declare a variable to store the input number and result, for example,int num, res;
  4. Ask the user to input a number usingprintfandscanffunctions.
  5. Use an?:;statement to check remainder as(num%2)and if remainder is 0 then print even otherwise print odd.
  6. End the main function and the program.
// ODD EEVEN using conditional operator
#include<stdio.h>

int main()
{
 int num, res;
 printf("Enter positive integer: ");
 scanf("%d", &num);
 res = (num%2)==0 ? printf("%d is EVEN.", num) :  printf("%d is ODD.", num);
 return 0;
}

4. Write a C program to check leap year.

The steps to create a program to find whether a given year is leap year or not using if…else statement:

  1. Start by including the necessary header file for input/output operations in C, which isstdio.h.
  2. Define the main function.
  3. Declare a variable to store the input year, for example,int year;
  4. Ask the user to input a year usingprintfandscanffunctions.
  5. A leap year is a year which is either fully divisible by 400 or the year is fully divisible by 4 but not by 100. So, we jointly compute these two conditions with logical OR operation.
  6. if the result of any part of if condition is true then print the given year is leap otherwise print the given year is not a leap year.
  7. End the main function and the program.
#include <stdio.h>

int main() {
   int year;
   //year = 1904;
   printf("\n Enter the  year :");
   scanf("%d", &year);
   if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
      printf("A %d is a leap year", year);
   else
      printf("A %d is not a leap year", year);

   return 0;
}

5. Write a c program to create calculator using switch statement.

The steps to create a calculator is as under-

  1. Start by including the necessary header file for input/output operations in C, which isstdio.h.
  2. Define the main function.
  3. Declare int variables (say and b) to store the values.
  4. Also declare one more variable as char opr to read user choice of operation.
  5. Read the values for a and b.
  6. Use printf () statement to give options for the available calculator operations.
  7. Then read the choice of operation on opr using getch() function.
  8. Use switch(opr) statement to select appropriate options as ‘+’, ‘-‘, ‘/’, ‘*’ for addition, subtraction, division, and multiplication operations.
  9. Add default: statement printing wrong input message.
  10. close the switch statement.
  11. End the main function.
// program to create calculator using SWITCH statement
#include<stdio.h>
#include<conio.h>
void main()
{int a,b;
char opr;
printf("\n Enter two numbers \n");
scanf("%d",&a);
scanf("%d",&b);
printf("\n Press:\n + for addition\n - for substraction\n * for multiplication\n / for divide operation");
opr=getch();
//scanf("%c",&ch);
switch(opr)
{ case '+': 
printf("\n addition of %d and %d  = %d",a,b, a+b); break;
  case '-':
  printf("\n substraction of %d and %d  = %d",a,b,a-b);break;
  case '*':
  printf("\n multiplication of %d and %d  = %d",a,b, a*b);break;
  case '/':
  printf("\n divide of %d and %d  = %d",a,b, a/b);break;
 default:
 printf("\n wrong operator");
}// end switch

}// end main

2 thoughts on “Enjoy Conditional Programming in C using If…else and switch statement.”

Leave a comment