Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, September 27, 2022

C program to check whether the input number is odd or even using function

C program to check whether the input number is odd or even using function





Introduction:

In C program to check whether the input number is odd or even using function. It can be done by dividing the number by 2 and checking if the remainder is 0 or 1. However, if you want to reuse this logic in your program, you can create a function to check if a number is odd or even.

Logic to check if a number is odd or even:

To check if a number is odd or even, we can use the modulus operator "%". The modulus operator returns the remainder of a division operation. If the remainder is 0, then the number is even; otherwise, it is odd.

Writing a C program to check if a number is odd or even using a function

To write a C program to check if a number is odd or even using a function, we need to follow the steps below:
  1. Declare the function prototype.
  2. Define the function.
  3. Call the function from the main function and pass the input number as an argument.
  4. Display the output message.

Coding:

#include<stdio.h>
void find(int n);
main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
find(num);
}
void find(int n)
{
if(n%2 == 0)
printf("%d is an even number\n", n);
else
printf("%d is an odd number\n", n);
}

Explanation of the coding:


The above C program checks whether the input number is odd or even by using a function called "find".

The main function prompts the user to enter a number and stores it in the variable "num". It then calls the "find" function, passing the value of "num" as an argument.

The "find" function takes an integer parameter "n" and checks whether it is even or odd using the modulus operator (%). If the remainder when "n" is divided by 2 is 0, then it is even and the function prints a message indicating that. If the remainder is not 0, then it is odd and the function prints a message indicating that.

The output of the program depends on the user's input. If the user enters an even number, the program will output a message saying so. If the user enters an odd number, the program will output a message indicating that as well.

This program is an example of how functions can be used in C to break down a program into smaller, more manageable pieces. By separating the code that checks whether a number is odd or even into a function, it can be reused in other parts of the program, making the code more modular and easier to maintain.

Output:


Another method:

#include <stdio.h>
int isOddOrEven(int num);
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(isOddOrEven(num) == 0)
printf("%d is an even number.\n", num);
else
printf("%d is an odd number.\n", num);
return 0;
}
int isOddOrEven(int num) {
return num % 2;
}

FAQs

1) What is a function in C?
Ans: In C, a function is a block of code that performs a specific task. Functions are used to break down large programs into smaller, more manageable pieces. They can take inputs, perform operations on those inputs, and return outputs.

2) What is the modulo operator in C?
Ans: The modulo operator % in C returns the remainder of a division operation. For example, 5 % 2 would return 1, because 2 goes into 5 two times with a remainder of 1.

3) What is a compiler in C?
Ans: In C, a compiler is a software program that converts human-readable code into machine-readable code. This process is called compilation. The resulting machine-readable code can be executed by the computer's processor.

4) Why is it important to check whether a number is even or odd in programming?
Ans: Checking whether a number is even or odd is a common programming task. It's important because it can help you solve a variety of problems. For example, you might need to perform different operations on even and odd numbers, or you might need to sort a list of numbers based on whether they are even or odd.

Conclusion


Writing a C program to check whether the input number is odd or even using function is a great way to learn the fundamentals of programming. By following the steps in this guide, you should now have a good understanding of how to write, compile, and run a C program. Remember to experiment with different numbers and see what happens.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages