Control flow-based C Programs

Table of Contents
< All Topics
Print

Control flow-based C Programs

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 (number % 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. compute num % 2 and store the result on variable rem.
  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 used 1 under case value: (use printf() statement to print ODD) and zero (0) under default: (use printf() statement to print EVEN) part of the switch statement.
  7. End the main function and the program.

Type the following code in your IDE (preferably Dev 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 odd or even using Bitwise operator.

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 using bitwise and (i.e. &) by 1 is equal to 1, i.e.,if (number & 1 == 1). If this condition is true, useprintfto display that the number is odd.
  6. If the condition in step 5 is false, use anelsestatement to display that the number is even.
  7. End the main function and the program.
// ODD EEVEN using bitwise operator
#include<stdio.h>

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

5. 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;
}

Leave a comment