Tech Unleashed

Wohoo!! We're on YouTube

Tuesday, 25 January 2022

C program for "Any integer is input through keyboard. write a program to find out whether it is an odd number or even number."

Problem: C program for "Any integer is input through keyboard. write a program to find out whether it is an odd number or even number." 

Logic used :
  • Find the remainder of the number using modulus operator with 2.
  • If the remainder is equal to 0, the number is even.
  • If the remainder is not equal to 0, the number is odd.
Demo :
if(num%2 == 0){
        printf("%d is even.",num);
    }else{
        printf("%d is odd.",num);
    }

C program :

// Program by Shyam Kanth

#include<stdio.h>

int main(){
    int num;
    printf("Enter the number : ");
    scanf("%d",&num);
    if(num%2 == 0){
        printf("%d is even.",num);
    }else{
        printf("%d is odd.",num);
    }
    return 0;
}

Codes used :
  • #include < stdio.h > : Header file for input-output
  • int : Integer data type
  • num : Variable used
  • printf : Function for displaying message
  • scanf : Function for taking input
  • if-else : Conditional statement
  • %d : Format specifier for integer type data
Do comments, if any doubt
Know about me : Shyam Kanth

No comments:

Post a Comment