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
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.
#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 program shows the following output:
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.
C program to print given number is even or odd using function:
#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;
}
}
If you like this article, then don't forget to share it with your friends and colleagues. If you have any doubts and feedback then feel free to use below comment section.
No comments:
Post a Comment
If you have any doubts, please discuss here...👇