Write a C Program to Check Whether a Number is Even or Odd

Write a Program to Check Whether a Given Number is Even or Odd in C

C programming nerchukuntunnappudu conditions and operators ni practical ga understand cheskovadaniki Even or Odd number program oka simple and useful example. Ee program dwara user enter chesina number ni C program read chesi, aa number even aa odd aa ani check cheyochu.

Manaki basic mathematics lo oka number 2 tho completely divisible aithe danini even number antam. For example, 2, 4, 6, 8, 10, 12 lanti numbers even numbers.

2 tho completely divisible kakapothe aa number odd number. For example, 1, 3, 5, 7, 9, 11 lanti numbers odd numbers.

C programming lo ee logic ni check cheyadaniki modulus % operator ni use chestam. % operator division tarvata migile remainder ni return chestundi.

Oka number ni 2 tho divide chesinappudu remainder 0 vaste, aa number even number ani artham. Remainder 0 kakapothe odd number ani artham.

Ee concept ni C lo if-else conditional statement tho easy ga implement cheyochu.

C Program to Check Even or Odd Number

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

int n;

printf(“Enter a number: “);
scanf(“%d”, &n);

if (n % 2 == 0)
{
printf(“Given number is even”);
}
else
{
printf(“Given number is odd”);
}

return 0;

}

Output 1

Enter a number: 4

Given number is even

Output 2

Enter a number: 21

Given number is odd

Explanation of the Program

Ippudu ee program ni step by step ga understand cheskundam.

1. #include <stdio.h>

#include <stdio.h>

Ee line C program lo standard input and output functions kosam stdio.h header file ni include

chestundi.

Manam ee program lo printf() and scanf() functions ni use chestunnam. Ee functions ni use

cheyadaniki stdio.h header file avasaram.

printf() output ni screen meeda display cheyadaniki use chestam.

scanf() user nunchi input read cheyadaniki use chestam.

2. int main()

int main()

main() anedi C program execution start ayye main function.

Simple ga cheppalante, C program run chesinappudu execution generally main() function

nunchi start avutundi.

Ikkada int anedi main() function return type. Anduke program ending lo return 0; use

chestunnam.

3. Variable Declaration

int n;

Ikkada n ane variable ni declare chesam.

int anedi integer data type. Decimal values lekunda whole numbers ni store cheyadaniki int

use chestam.

For example:

10

25

100

-15

lanti integer values ni int variable lo store cheyochu.

Ikkada manam n ki immediate ga value assign cheyaledu. User enter chese number ni later

scanf() dwara n lo store chestam.

4. printf()

printf(“Enter a number: “);

Ee printf() statement user ki oka message display chestundi.

Screen meeda:

Enter a number:

ani kanipistundi.

User ki input enter cheyali ani indicate cheyadaniki ee message useful ga untundi.

5. scanf()

scanf(“%d”, &n);

scanf() function user enter chesina value ni read chesi variable lo store chestundi.

Ikkada %d integer value ni read cheyadaniki use chestam.

&n ante n variable yokka memory address ni scanf() ki provide chestunnam. Dini valla user

enter chesina integer value n variable lo store avutundi.

For example, user:

4

enter chesthe, n value 4 avutundi.

User:

21

enter chesthe, n value 21 avutundi.

6. if Statement

if (n % 2 == 0)

Ikkade program lo main logic untundi.

if anedi C lo conditional statement. Oka condition ni check chesi, condition true aithe if block

lo unna code execute chestundi.

Ikkada manam:

n % 2 == 0

ane condition ni check chestunnam.

7. What Does % Operator Do?

% anedi modulus operator.

Modulus operator division tarvata migile remainder ni return chestundi.

For example:

10 % 2 = 0

11 % 2 = 1

20 % 2 = 0

25 % 2 = 1

Even or odd check cheyadaniki ee remainder concept chala important.

Number ni 2 tho divide chesinappudu remainder 0 vaste number even.

Remainder 1 vaste number odd.

Anduke program lo:

if (n % 2 == 0)

ani condition use chesam.

8. Why Do We Use == 0?

Ikkada:

n % 2 == 0

lo == comparison operator.

Idi rendu values equal aa kada ani check chestundi.

n % 2 remainder ni calculate chestundi.

Aa remainder 0 tho equal aithe condition true avutundi.

Example:

n = 4

4 % 2 = 0

0 == 0 → true

Kabatti if block execute avutundi.

9. printf() Inside if Block

if (n % 2 == 0)
{
printf(“Given number is even”);
}

Condition true aithe ee printf() statement execute avutundi.

Example:

User 4 enter chesadu.

4 % 2 = 0

Condition true.

Output:

Given number is even

10. else Block

else
{
printf(“Given number is odd”);
}
Condition false aithe else block execute avutundi.

Example:

User 21 enter chesadu.

21 % 2 = 1

1 == 0 false.

Kabatti if block execute avvadu. Program else block ki velli:

Given number is odd

ani print chestundi.

Output 1 Explanation

First output lo user 4 ane number enter chesadu.

Program n variable lo 4 ni store chestundi.

Tarvata condition:

4 % 2 == 0

check chestundi.

4 ni 2 tho divide cheste remainder 0 vastundi.

Kabatti condition true avutundi.

Anduke if block lo unna:

Given number is even

ane message output lo display avutundi.

Output 2 Explanation

Second output lo user 21 ane number enter chesadu.

n variable lo 21 store avutundi.

Tarvata program:

21 % 2 == 0

ane condition check chestundi.

21 ni 2 tho divide cheste remainder 1 vastundi.

Kabatti:

1 == 0

false.

Condition false kabatti if block skip ayi else block execute avutundi.

Anduke output:

Given number is odd

ani vastundi.

Another Example with Different Input

Ippudu same logic ni another example tho chuddam.

#include <stdio.h>

int main()
{
int number;

printf(“Enter a number: “);

scanf(“%d”, &number);

if (number % 2 == 0)
{
printf(“%d is an even number”, number);
}
else
{
printf(“%d is an odd number”, number);
}

return 0;

}

Output

Enter a number: 50

50 is an even number

Ee example lo number ane variable use chesam. Variable name change ayina logic same ga untundi.

50 % 2 remainder 0 kabatti 50 even number.

Even and Odd Numbers – Simple Logic

Even number:

Number % 2 == 0

Odd number:

Number % 2 != 0

For example:

12 % 2 = 0 → Even

17 % 2 = 1 → Odd

28 % 2 = 0 → Even

35 % 2 = 1 → Odd

Ila % operator use chesi number even aa odd aa easy ga identify cheyochu.

Common Mistakes Beginners Should Avoid

Even or odd program simple ga unna, beginners konni small mistakes chestaru.

First mistake % place lo / operator use cheyadam.

Correct:

n % 2 == 0

Division result kosam / use chestam, remainder kosam % use chestam.

Second mistake = and == ni confuse cheyadam.

= → Assignment operator

== → Comparison operator

Condition check cheyadaniki:

n % 2 == 0

ani use cheyali.

Third mistake scanf() lo & miss cheyadam.

Correct:

scanf(“%d”, &n);

Fourth mistake stdio.h include cheyakunda printf() and scanf() use cheyadam. Standard input/output functions kosam header file include cheyadam important.

Why This Program Is Useful for Beginners

Even or odd program chinna program ayina C programming lo several important concepts ni okesari practice cheyadaniki help chestundi.

Ee single program lo:

  • Variable declaration
  • Integer data type
  • User input
  • printf()
  • scanf()
  • Modulus operator
  • Comparison operator
  • if-else
  • Main function
  • Return statement

lanti basic concepts anni practice cheyochu.
Anduke C programming beginners ki Even or Odd program oka useful practice problem.

Conclusion

C programming lo given number even aa odd aa ani check cheyadam simple conditional program. Ee program kosam user nunchi integer value ni scanf() dwara read chesi, % modulus operator tho aa number ni 2 tho divide chesinappudu vachina remainder ni check chestam.

Remainder 0 aithe number even ani, remainder 0 kakapothe number odd ani decide chestam. Ee decision ni if-else statement tho implement cheyochu.

Ee example dwara beginners ki variables, scanf(), printf(), modulus operator, comparison operator and conditional statements meeda basic understanding vastundi. Ee logic ni practice chesina tarvata, similar ga positive or negative number, largest of two numbers, divisible by 5 or not lanti basic C programs kuda easy ga practice cheyochu.

Leave a Comment