Place Your Query
-
C Programming
-
- Control flow-based C Programs
- Enjoy Conditional Programming in C using If...else and switch statement.
- Good, Now write a C program to display "Hello World" on the screen.
- Write a C program to display Your Name, Address and City in different lines.
- C program to find sum of n numbers
- Write a C program For Addition of two numbers using Function.
- Write a Program to Find Simple Interest and Compound Interest.
- Write a Program to Convert Celsius To Fahrenheit and Vice Versa.
- Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.
- Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
- Write a C Program to Design a Simple Menu Driven Calculator
- Simple Expression Based C Programs
-
- 7. Components of C language
- 1. Introduction to C Programming Language
- 10. Operator Precedence and Associativity
- 11. Comments in C Programming
- 14. Fundamental Control Structure Statements in C [Part-1]
- 15. Fundamental Control Structure Statements in C [Part-2]
- 16. Looping Statements [Fundamental Control Structure Statements in C. #Part-3]
- 17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]
- 2. Computer Languages
- 3. Interpreters vs Compilers vs Assemblers in programming languages
- 4. C Program Structure
- 5. Compile and Execute C Program
- 6. Errors in C Program
- 8. C Datatypes
- 9. Operators in C
- Control flow-based C Programs
- Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.
- 18. Fundamentals of C Functions
- Show Remaining Articles (3)Collapse Articles
-
-
Java
-
AI ML
-
FAQs
- A Program to find Tokens in C code?
- What are the Common Coding Mistakes.
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
-
Python Interview Questions
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
Table of Contents
< All Topics
Print
Control flow-based C Programs
Updated14 March 2024
ByMilind Bhatt
1.Write a program to find that given number is odd even (using if.. else statement)
The steps to create a program to find whether a given number is odd or even using if…else statement:
- Start by including the necessary header file for input/output operations in C, which is
stdio.h
. - Define the main function.
- Declare a variable to store the input number, for example,
int num;
. - Ask the user to input a number using
printf
andscanf
functions. - Use an
if
statement to check if the number is even. This can be done by checking if the remainder of the number divided by 2 is equal to 0, i.e.,if (number % 2 == 0)
. If this condition is true, useprintf
to display that the number is even. - If the condition in step 5 is false, use an
else
statement to display that the number is odd. - End the main function and the program.
Type the following code in your IDE (preferably Dev C++)
// ODD EEVEN using if...else statement
#include<stdio.h>
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if(num%2 == 0)
{
printf("%d is EVEN.", num);
}
else
{
printf("%d is ODD.", num);
}
return 0;
}
2. Write a program to find that given number is odd even (using switch statement)
The steps to create a program to find whether a given number is odd or even using switch statement:
- Start by including the necessary header file for input/output operations in C, which is
stdio.h
. - Define the main function.
- Declare a variable to store the input number and remainder, for example,
int num, rem;
. - Ask the user to input a number using
printf
andscanf
functions. - compute
num % 2 and store the result on variable rem
. - Use a switch statement to get the value of remainder. The possible remainder values are either 1 (i.e num is odd) or 0 (i.e num is even). Here we have used 1 under case value: (use printf() statement to print ODD) and zero (0) under default: (use printf() statement to print EVEN) part of the switch statement.
- End the main function and the program.
Type the following code in your IDE (preferably Dev C++)
// ODD EEVEN using switch statement
#include<stdio.h>
int main()
{
int num, rem;
printf("Enter positive integer: ");
scanf("%d", &num);
rem = num%2;
switch(rem)
{
case 1: printf("%d is ODD.", num);
break;
default: printf("%d is EVEN.", num);
break;
}
return 0;
}
3. Write a program to find that given number is odd even (using conditional operator. i.e. ?:;)
The steps to create a program to find whether a given number is odd or even using conditional operator-based statement:
- Start by including the necessary header file for input/output operations in C, which is
stdio.h
. - Define the main function.
- Declare a variable to store the input number and result, for example,
int num, res;
- Ask the user to input a number using
printf
andscanf
functions. - Use an?:;statement to check remainder as (num%2) and if remainder is 0 then print even otherwise print odd.
- End the main function and the program.
// ODD EEVEN using conditional operator
#include<stdio.h>
int main()
{
int num, res;
printf("Enter positive integer: ");
scanf("%d", &num);
res = (num%2)==0 ? printf("%d is EVEN.", num) : printf("%d is ODD.", num);
return 0;
}
4. Write a C program to check odd or even using Bitwise operator.
The steps to create a program to find whether a given number is odd or even using if…else statement:
- Start by including the necessary header file for input/output operations in C, which is
stdio.h
. - Define the main function.
- Declare a variable to store the input number, for example,
int num;
. - Ask the user to input a number using
printf
andscanf
functions. - Use an
if
statement to check if the number is even. This can be done by checking if the remainder of the number using bitwise and (i.e. &) by 1 is equal to 1, i.e.,if (number & 1 == 1)
. If this condition is true, useprintf
to display that the number is odd. - If the condition in step 5 is false, use an
else
statement to display that the number is even. - End the main function and the program.
// ODD EEVEN using bitwise operator
#include<stdio.h>
int main()
{
int num, res;
printf("Enter positive integer: ");
scanf("%d", &num);
if(num & 1 == 1)
{
printf("%d is ODD.", num);
}
else
{
printf("%d is EVEN.", num);
}
return 0;
}
5. Write a C program to check leap year.
The steps to create a program to find whether a given year is leap year or not using if…else statement:
- Start by including the necessary header file for input/output operations in C, which is
stdio.h
. - Define the main function.
- Declare a variable to store the input year, for example,
int year;
- Ask the user to input a year using
printf
andscanf
functions. - A leap year is a year which is either fully divisible by 400 or the year is fully divisible by 4 but not by 100. So, we jointly compute these two conditions with logical OR operation.
- if the result of any part of if condition is true then print the given year is leap otherwise print the given year is not a leap year.
- End the main function and the program.
#include <stdio.h>
int main() {
int year;
//year = 1904;
printf("\n Enter the year :");
scanf("%d", &year);
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("A %d is a leap year", year);
else
printf("A %d is not a leap year", year);
return 0;
}