Using if statement in C: A Beginner's Guide


The if statement is a simple decision-making and branching statement and it is used to control the flow of the program execution. An if statement consists of a Boolean expression followed by one or more statements. The if statement is written with the if keyword, followed by a condition in parentheses, with the code to be executed in between curly brackets.

  Syntax:
     if( condition )
     {
        // body of the statements will execute if the condition is true ;
     }

  • The condition evaluates to either true or false.
  • True is always a non-zero value, and false is a value that contains zero.

The code is a C program that demonstrates the use of the if statement in C. Here is an explanation of each line of the code:

  • char name[10]; declares a character array variable named name with a maximum size of 10 characters.
  • int age; declares a variable age of type int.
  • printf("\nEnter Your Name : "); prints a message prompting the user to enter their name.
  • scanf("%s",name); reads a string input from the user and stores it in the variable name.
  • printf("\nEnter The Age : "); prints a message prompting the user to enter their age.
  • scanf("%d",&age); reads an integer input from the user and stores it in the variable age.
  • if(age>=18) checks if the value of the variable age is greater than or equal to 18.
  • printf("\n %s age is %d Eligible For Vote",name,age); prints a message stating that the user is eligible to vote if the above if condition is true.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

Source Code

//if statement
 
#include<stdio.h>
int main()
{
    char name[10];
    int age;
    printf("\nEnter Your Name : ");
    scanf("%s",name);
    printf("\nEnter The Age : ");
    scanf("%d",&age);
    if(age>=18)
    {
        printf("\n %s  age is %d Eligible For Vote",name,age);
    }
    return 0;
}
 
 
To download raw file Click Here

Output

Enter Your Name : Siva
Enter The Age : 21
Siva  age is 21 Eligible For Vote

C Programming Conditional Statement Problems

1.while purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses.

2. The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of service are not greater than 3, then the program should do nothing.

3. If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary

4. The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules: Write a program to calculate the division obtained by the student.

  1. Percentage above or equal to 60 - First division
  2. Percentage between 50 and 59 - Second division
  3. Percentage between 40 and 49 - Third division
  4. Percentage less than 40 – Fail

5. A company insures its drivers in the following cases:

  1. If the driver is married.
  2. If the driver is unmarried, male & above 30 years of age.
  3. If the driver is unmarried, female & above 25 years of age.

6.Write a program to calculate the salary as per the following table

Gender Years of Service Qualifications Salary
Male>= 10Post-Graduate15000
Male>= 10Graduate10000
Male< 10Post-Graduate10000
Male< 10Graduate7000
Female>= 10Post-Graduate12000
Female>= 10Graduate9000
Female< 10Post-Graduate10000
Female< 10Graduate6000

7.If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

8.Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number. (Hint: Use the % (modulus) operator)

9.A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.

10.If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

11.Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

12.Find the absolute value of a number entered through the keyboard.

13.Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter

14.Any year is entered through the keyboard, write a program to determine whether the year is leap or not. Use the logical operators && and ||.

15.A certain grade of steel is graded according to the following conditions: A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii)
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.

16. A library charges a fine for every book returned late. For the first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.

17.In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C