Jun 24, 2020

C program to print given number is even or odd using function

Ex: write a C program to print given number is even or odd using function. How to write a C program to print given number is even or odd using function. C program to print given number is even or odd using function.


Input from user:

Enter the number: 24

Expected output:

Given number is even




Step by step logic of the given program:

1. First declare function give meaningful name say even().

2.  Inside the main() accept input(number) from user, declare varible say no.

3.  Call function and store returned value in variable r.

4.  If  returned value is 0 if(r==0) print is even otherwise print is odd.

5. inside the function check number is even or odd using if(no%2==0) return 0 else return 1.





Program:

#include<stdio.h>
int even(int);

int main()

{
int no,r=0;
printf("Enter the number\n");
scanf("%d",&no);

r=even(no);

if(r==0)
printf("Given number is even");
else
printf("Given number is odd");
}
int even(int no)
{
if(no%2==0)
{
return 0;
}
else
{
return 1;
}
}



Above prgram shows the following output:






Share This:    Facebook Twitter

0 comments:

Post a Comment

If you have any doubts, please discuss here...