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 is stdio.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 using printf and scanf functions.
  5. Use an if statement 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, use printf to display that the number is even.
  6. If the condition in step 5 is false, use an else statement 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 is stdio.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 using printf and scanf functions.
  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 is stdio.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 using printf and scanf functions.
  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 is stdio.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 using printf and scanf functions.
  5. Use an if statement 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, use printf to display that the number is odd.
  6. If the condition in step 5 is false, use an else statement 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 is stdio.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 using printf and scanf functions.
  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;
}
Milind Bhatt

A Tech Educator, youtuber and blogger. Having in total 22 years of teaching experience in the field of computer science, data science, artificial intelligence and machine learning.

Share
Published by
Milind Bhatt
Essential Web Terminology for Servlets

Essential Web Terminology for Servlets

All about java servlets Part-2 What is a website? Website is a collection of related…

2 years ago
Understanding Machine Learning

Understanding Machine Learning

Let's train the machine. Machine learning is a growing technology which enables computers to learn…

2 years ago
Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.

Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.

“Unless you try to do something beyond what you have already mastered, you will never…

2 years ago
What are the Common Coding Mistakes.

What are the Common Coding Mistakes.

Whenever you decide to start coding and you want to become budding coder or programmer…

2 years ago

7. Components of C language

C components are essential elements of a C program, including comments, preprocessor directives, functions, statements,…

2 years ago

Essential Web Terminology for Servlets

All about java servlets Part-2 What is a website? Website is a collection of related…

2 years ago