➤ If-else Statement in C
➤ Programs on if-else
➤ Switch Case in C
➤ Switch case Programs
➤ Conditional Operator
➤ While loop in C
➤ Do-while loop in C
➤ While vs do-while
➤ For loop in C
➤ Break keyword in C
➤ Continue keyword in C
➤ Break vs Exit in C
➤ Goto keyword in C
☕️ Flow Control Programs
➤ Largest in 3 Numbers
➤ Find Grade of student
➤ Find the absolute value
➤ Vowel or Consonant
➤ Leap Year Program
➤ Simple calculator in C
➤ Check Odd or Even
➤ Roots of Quadratic Equation
➤ Find Reverse of Number
➤ Factors of a number in C
➤ Generate Multiplication table
➤ Find Power of a Number
➤ Find GCD and LCM
➤ Find factorial of Number
➤ Count Number of Digits
➤ Sum of digits in Number
➤ Sum of N Natural Numbers
➤ Sum of Squares of Natural No.
➤ Find Sum of Odd Numbers
➤ Find the Sum of Series
➤ Find Fibonacci series in C
➤ Sum of the Fibonacci series
➤ Sum until enters +ve numbers
➤ Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
➤ Celsius to Fahrenheit
➤ Fahrenheit to Celsius
➤ Decimal ↔ Binary
➤ Decimal ↔ Octal
➤ Octal ↔ Binary in C
☕️ Number Programs in C
➤ Prime Number in C
➤ Strong Number in C
➤ Krishnamurthy Number
➤ Neon Number in C
➤ Palindrome number
➤ Perfect Number in C
➤ Armstrong Number
☕️ Pattern Programs in C
➤ Pattern programs in C
➤ Printing pattern using loops
➤ Floyd’s triangle Program
➤ Pascal Triangle Program
➤ Pyramid Star Pattern in C
➤ Diamond Pattern Programs
➤ Half Diamond pattern in C
➤ Print Diamond Pattern
➤ Hollow Diamond Pattern
➤ Diamond Pattern of Numbers
Previously we learned about if-else statements now, we will write some simple if-else programs in C. These all are basic programs, you can write them easily. Few more programs are also there, see all top C Program Examples.
Prerequisites to solve these problems:- If else statement in C
Also see:- If statement Quiz in C, If-else statement Quiz in C
C Program to Check Number is Multiple of 7 or not
Program:- Write a C program to check whether the given number is of multiple of 7 or not.
If the number is divisible by 7 then we can say that the number is multiple of 7 otherwise the number is not multiple of 7.
#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d",&number);
if(number%7==0)
{
printf("%d is multiple of 7.",number);
}
else
{
printf("%d is not multiple of 7.",number);
}
return 0;
}
Output for the different test-cases:-
Enter a number: 14
14 is multiple of 7.
Enter a number: 20
20 is not multiple of 7.
C Program to Find Positive or Negative
Program:- Write a program to check whether the given number is positive or negative or zero.
If the number is less than zero then the number is positive and if the number is greater than zero then the number is negative. When both conditions become false then the number is zero.
#include<stdio.h>
int main()
{
float number;
printf("Enter a number: ");
scanf("%f",&number);
if(number>0)
{
printf("%.2f is a positive number",number);
}
else if(number<0)
{
printf("%.2f is a negative number",number);
}
else
{
printf("Number is zero");
}
return 0;
}
Output for the different test-cases:-
Enter a number: 5
5.00 is a positive number
Enter a number: -9
-9.00 is a negative number
Enter a number: 0
Number is zero
C Program to Find Even or Odd using if-else
Program:- Write a c program to check the given number is even or odd using if else conditional statements.
A number is called even number when the number is divisible by 2. If the number is not divisible by 2 then it is called an odd number.
#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 for the different test-cases:-
Enter a number: 10
10 is an even number
Enter a number: 9
9 is an odd number
There are many other ways that also can be used for checking the number is odd or even. Know more:- Find odd-even in C
Finding Largest in Two Number
Sample input/outputs:-
Enter two numbers: 10 15
b is big.
Enter two numbers: 15 10
a is big.
Enter two numbers: 10 10
Both are the same.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two number: ");
scanf("%d %d",&a,&b);
if(a>b)printf("a is big.");
else if(b>a) printf("b is big.");
else printf("Both are the same.");
return 0;
}
We take two variables a and b to store input variables. Using if-else-if conditional statement now comparing a and b. if a is greater than b then the variable a is big. Otherwise, if b is greater than a then b is big. Else both variable holds the same value.
Finding Largest in Three Number
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three number: ");
scanf("%d %d %d", &a, &b, &c);
if(a==b && a==c) printf("All are equal.");
else if(a>b && a>c) printf("a is big.");
else if(a==b && a>c) printf("a & b are big.");
else if(b==c && b>a) printf("b & c are big.");
else if(a==c && a>b) printf("a & c are big.");
else if(b>c) printf("b is big.");
else printf("c is big.");
return 0;
}
Output for the different test-cases:-
Enter three number: 10 15 12
b is big.
Enter three number: 12 10 15
c is big.
Enter three number: 15 12 10
a is big.
Enter three number: 12 12 10
a & b are big.
Enter three number: 10 12 12
b & c are big.
Enter three number: 12 10 12
a & c are big.
Check Candidate is Eligible for Voting or Not
Program:- Write a program to check whether the candidate is eligible for voting or not by accepting the age value from the user.
If the candidate’s age is more than or equal to 18 years then he/she is eligible for voting. So, if statement we will write condition as (age>=18)
#include<stdio.h>
int main()
{
float age;
printf("Enter the age of the candidate: ");
scanf("%f",&age);
if(age>=18)
{
printf("The candidate is eligible for voting.");
}
else
{
printf("The candidate is not eligible for voting.");
}
return 0;
}
Output for the different test-cases:-
Enter the age of the candidate: 17.9
The candidate is not eligible for voting.
Enter the age of the candidate: 18
The candidate is eligible for voting.
Program to Evaluate the Result of a Student
Program:- Write a C program to evaluate the result of a student by accepting marks in three subjects. If any subject mark is less than 35 then the student is failing. For average greater than equal to 60, the student is first-class else it is 2nd class. If the student does not fail then also display the total mark and average mark.
#include<stdio.h>
int main()
{
int mark1, mark2, mark3, total;
float avg;
printf("Enter marks of three subjects: ");
scanf("%d %d %d",&mark1, &mark2, &mark3);
if(mark1>=35 && mark2>=35 && mark3>=35)
{
total = mark1 + mark2 + mark3;
avg = total/3;
printf("Total marks = %d\n",total);
printf("Average mark = %.2f\n", avg);
if(avg>=60)
{
printf("Result is first class.");
}
else
{
printf("Result is second class.");
}
}
else
{
printf("Result is fail");
}
return 0;
}
Output:-
Enter marks of three subjects: 70 80 85
Total marks = 235
Average mark = 78.00
Result is first class.
Check Character is an Uppercase Alphabet or not using If Else Program in C
Program:- Write a program to check whether the given character is an uppercase alphabet or not using the if-else conditional statement in C.
The uppercase letter is from ‘A’ to ‘Z’. The ASCII value of ‘A’ and ‘Z’ are 65 and 90 respectively. If any character’s ASCII value is within the range of 65 to 90 then it is an uppercase alphabet.
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
{
printf("%c is an uppercase alphabet.",ch);
}
else
{
printf("%c is not an uppercase alphabet.",ch);
}
return 0;
}
Output for the different test-cases:-
Enter a character: K
K is an uppercase alphabet.
Enter a character: p
p is not an uppercase alphabet.
In this program inside if condition we can also directly use the characters ‘A’ and ‘Z’ instead of 65 and 90.
if(ch>='A' && ch<='Z')
The above if condition is valid, and we can use it in our program. Know more about:- ASCII value of all characters
Check Character is a Lowercase Alphabet or Not using If Else Program in C
Program:- Write a program to check whether the given character is a lowercase alphabet or not.
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(ch>=97 && ch<=122)
{
printf("%c is a lowercase alphabet.",ch);
}
else
{
printf("%c is not a lowercase alphabet.",ch);
}
return 0;
}
Output for the different test-cases:-
Enter a character: K
K is not a lowercase alphabet.
Enter a character: k
k is a lowercase alphabet.
Similarly to the previous program, here also we can use characters ‘a’ and ‘z’ in place of 97 and 122. Below if condition is also valid.
if(ch>='a' && ch<='z')
Check Character is an Alphabet or not using if else program in C
Program:- Write a C program to check character is an alphabet or not using if-else conditional statements.
ASCII value of A=65 & Z=90
ASCII value of a=97 & z=122
All Values from 65 to 90 or 97 to 122 are an alphabet.
#include<stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c",&ch);
if ((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
{
printf("%c is an alphabet.\n",ch);
printf("ASCII value of %c is %d\n",ch,ch);
}
else printf("%c is not an alphabet.",ch);
return 0;
}
Output for the different test-cases:-
Enter any character: A
A is an alphabet.
ASCII value of A is 65
Enter any character: a
a is an alphabet.
ASCII value of a is 97
Enter any character: 0
0 is not an alphabet.
Enter any character:?
? is not an alphabet.
If any character is an alphabet then its ASCII value range is from 65 to 90 and 97 to 122. The character whose ASCII value is not in the above-given range then the character is not an alphabet.
Using an if-else conditional statement we can check the character is an alphabet or not? If the character is not an alphabet then it will display “character is not an alphabet”, else it will display the corresponding ASCII value of the character.
if ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
In the above program, we can use ‘A’, ‘Z’, ‘a’, and ‘z’ directly. No compile-time error or run time error occurs. This time again comparison will be based on the ASCII value of ‘A’, ‘Z’, ‘a’ and ‘z’, not directly using alphabets.
Calculate the Net Amount using if else Program in C
Program:- Write a program to calculate the net amount by accepting the amount values from the user.
Amount in $ | Discount (%) |
>= 1000 | 10% |
Otherwise | 5% |
#include<stdio.h>
int main()
{
double amount, finalamount;
int discount;
printf("Enter the total Amount: ");
scanf("%lf",&amount);
if(amount>=1000)
{
discount = (amount * 10)/100;
}
else
{
discount = (amount * 5)/100;
}
finalamount = amount - discount;
printf("Total payable amount = %.2f", finalamount);
return 0;
}
Output for the different test-cases:-
Enter the total Amount: 2000
Total payable amount = 1800.00
Enter the total Amount: 500
Total payable amount = 475.00
Check Eligibility for Marriage
Problem description:- Read a person/girl’s age and gender. Determine that he/she is eligible for marriage or not. Eligibility for marriage for boys is 21 years and for girls 18 years.
Sample Input & Outputs:
Enter Gender:- M
Enter Age:- 25
Eligible
Enter Gender:- F
Enter Age:- 17
Not Eligible
Enter Gender:- S
Enter Age:- 30
Invalid Gender
#include<stdio.h>
int main()
{
int age;
char gen;
printf("Enter Gender: ");
scanf("%c",&gen);
printf("Enter Age: ");
scanf("%d",&age);
if(gen=='M'||gen=='m'||gen=='F'||gen=='f')
{
if( age>=21 || (gen=='F'|| gen=='f') && age>=18)
printf("Eligible");
else printf("Not Eligible");
}
else printf("Invalid Gender");
return 0;
}
Check Character is a lower, upper, digit or special character
Program:- write a program to read a character and find out it is a lower or upper or digit or special character.
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(ch >='a' && ch <= 'z')
printf("Lower case character.");
else if(ch >='A' && ch <= 'Z')
printf("Upper case character.");
else if(ch >='O' && ch <= '9')
printf("Digit");
else printf("Special character.");
return 0;
}
Output for the different test-cases:-
Enter a character: V
Upper case character.
Enter a character: k
Lower case character.
Enter a character: 1
Special character.
Enter a character: ?
Special character.
The above program can also be written directly using ASCII values.
if(ch >=97 && ch <= 122)
printf("Lower case character.");
else if(ch >=65 && ch <= 90)
printf("Upper case character.");
else if(ch >=48 && ch <= 57)
printf("Digit");
else printf("Special character.");
We can also do the same task using predefined functions. In C language, islower(), isupper(), isdigit() is given inside “ctype.h
” header file. The functions islower(), isupper() and isdigit() returns 0 or false if condition is false otherwise it returns 1 or true.
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if(islower(ch)) printf("Lower case character.");
else if(isupper(ch)) printf("Upper case character.");
else if(isdigit(ch)) printf("Digit");
else printf("Special character.");
return 0;
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!