Simple if, if-else, nested if-else and else-if ladder
- Take two int values from user and print greatest among them using if/else and ternary operator.
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter num1\n");
scanf("%d", &num1);
printf("Enter num2\n");
scanf("%d", &num2);
printf("Using if else : \n");
if (num1 > num2)
{
printf("%d is greater number\n",num1);
}
else
{
printf("%d is greater number\n",num2);
}
// Ternary operator
printf("Using ternary operator : \n");
(num1 > num2) ? printf("%d is greater number\n",num1) : printf("%d is greater number\n",num2);
return 0;
}
- Take values of length and breadth of a rectangle from user and check if it is square or not.
#include <stdio.h>
int main()
{
int length, breadth;
printf("Enter length\n");
scanf("%d", &length);
printf("Enter breadth\n");
scanf("%d", &breadth);
if (length == breadth)
{
printf("It is a square\n");
}
else
{
printf("It is a rectangle\n");
}
return 0;
}
- A shop will give discount of 10% if the cost of purchased quantity is more than 1000. Ask user for quantity Suppose, one unit will cost 100. Judge and print total cost for user.
#include <stdio.h>
int main()
{
int quantity, price, discount;
printf("Enter quantity\n");
scanf("%d", &quantity);
price = quantity * 100;
if (price > 1000) {
discount = price * 0.1;
printf("Total cost is %d\n", price - discount);
}
else {
printf("Total cost is %d\n", price);
}
return 0;
}
/*
Output :
Enter quantity
20
if Total cost is 1800
*/
- A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.
- A school has following rules for grading system:
a. Below 25 – F
b. 25 to 45 – E
c. 45 to 50 – D
d. 50 to 60 – C
e. 60 to 80 – B
f. Above 80 – A
Ask user to enter marks and print the corresponding grade.
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks\n");
scanf("%d", &marks);
if (marks < 25){
printf("F\n");
}
else if(marks >=25 && marks <45){
printf("E\n");
}
else if(marks >=45 && marks <50){
printf("D\n");
}
else if(marks >=50 && marks <60){
printf("C\n");
}
else if(marks >=60 && marks <80){
printf("B\n");
}
else if(marks >=80 && marks <100){
printf("A\n");
}
else{
printf("Invalid marks\n");
}
return 0;
}
/*
Output :
Enter marks
65
B
Enter marks
30
E
Enter marks
20
F
*/
- Take input of age of 3 people by user and determine oldest and youngest among them.
- A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
- A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
Modify this question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( ‘Y’ or ‘N’ ) and print accordingly.
- Write a program to check whether a entered character is lowercase ( a to z ) or uppercase ( A to Z ).
- If
x = 2
y = 5
z = 0
then find values of the following expressions:
a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)
- Write a program to print absolute vlaue of a number entered by user.
#include <stdio.h>
int main()
{
int x;
printf("Enter a number\n");
scanf("%d", &x);
if (x < 0)
{
x = x * (-1);
}
printf("Absolute value is %d\n", x);
return 0;
}
/*
Output :
Enter a number
2
Absolute value is 2
Enter a number
-2
Absolute value is 2
*/
- C program to find maximum between three numbers
- C program to check positive, negative or zero using simple if or if else
- C program check whether a number is even or odd
- C program to check whether a character is alphabet or not
- C program to check vowel or consonant
- C program to check whether a character is alphabet, digit or special character
- C program to check whether a character is Uppercase or Lowercase
- C program to enter week number and print day of week
- C program to find number of days in month
- C program to count total number of notes in given amount
- C program to check whether triangle is valid or not if angles are given
- C program to check whether triangle is valid or not if sides are given
- C program to check whether triangle is equilateral, scalene or isosceles
- C program to find all roots of a quadratic equation
- C program to calculate profit or loss
- C program to enter student marks and find percentage and grade
List of switch case programming exercises
- C program to print day of week name using switch case
- C program to print number of days in a month using switch case
- C program to check vowel or consonant using switch case
- C program to find maximum between two numbers using switch case
- C program to check even or odd number using switch case
- C program to check positive negative or zero using switch case
- C program to find all roots of a quadratic equation using switch case
- C program to create calculator using switch case and functions
Programs on loops
- C program to print all natural numbers from 1 to n using while loop
- C program to print natural numbers in reverse from n to 1
- C program to print all alphabets from a to z using while loop
- C program to print all even numbers from 1 to n
- C program to print all odd numbers from 1 to n
- C program to find sum of natural numbers from 1 to n
- C program to find sum of even numbers between 1 to n
- C program to find sum of odd numbers from 1 to n
- C program to print multiplication table of a given number
- C program to count number of digits in an integer
- C program to find first and last digit of any number
- C program to find sum of digits of a number
- C program to find reverse of a number
- C program to check whether a number is palindrome or not
- C program to count frequency of digits in an integer
- C program to print number in words
- C program to print ASCII values of all characters
- C program to find power of a number using for loop
- C program to find all factors of a number
- C program to find factorial of a number
- C program to find HCF (GCD) of two numbers
- C program to find LCM of two numbers
- C program to check whether a number is prime number or not
- C program to print all prime numbers between 1 to n
- C program to find prime factors of a number
- C program to check whether a number is armstrong number or not
- C program to check whether a number is perfect number or not
- C program to find perfect numbers between 1 to n
- C program to check whether a number is Strong number or not
- C program to print strong numbers between 1 to n
- C program to print fibonacci series upto n terms
- C program to find ones complement of a binary number
- C program to find twos complement of a binary number
- C program to convert Binary to Decimal number system
- C program to convert Binary to Octal number system
- C program to convert Binary to Hexadecimal number system
- C program to convert Octal to Binary number system
- C program to convert Octal to Decimal number system
- C program to convert Octal to Hexadecimal number system
- C program to convert Decimal to Binary number system
- C program to convert Decimal to Octal number system
- C program to convert Decimal to Hexadecimal number system
- C program to convert Hexadecimal to Binary number system
- C program to convert Hexadecimal to Octal number system
- C program to convert Hexadecimal to Decimal number system
- C program to print pascal triangle
- C program to print hollow right triangle star pattern