**The C programming language**is a procedural programming language. In which statements are executed sequentially when no condition is applied. If the condition is applied, then the sequence of execution of the statement might be changed (based on condition evaluation). Conditional statements are also known as decision making statements in C.

**See Also: Operators in C**

### Categorization of Conditional Statement in C

Conditional statement in C is categorized as:

A) If statement

B) If-else statement in C

### IF Statement in C

if (Condition)

{

//Block of the statements

}

If statement is a conditional statement in C, if the condition inside the parenthesis is true, then the block of the statement will execute. True is a non-zero value, and false is a zero value. For instance,

**See Also: Pointers in C**
if (a>10)

{

printf ("Hello\n");

}

#### Flow Chart of If Statement

**C program which demonstrates working of if statement**

#include <stdio.h>

int main()

{

int a, b; //Initialize variables a, b

printf ("Enter the value of a and b");

scanf ("%d%d", &a, &b);

printf ("Entered value of a and b is %d %d\n", a, b);

if (a<b)

//Check condition a is smaller than b or not. If this condition is true, it prints a is smaller than b otherwise prints b is greater than b.

{

printf ("a is smaller than b\n");

}

printf ("b is greater than a\n");

return 0;

}

**Output**

Enter the value of a and b 10 6

Entered value of a and b is 10 6

b is greater than a

### If Else Statement in C

If else statement is a decision-making statement. This statement is an extension of if statement. We try to understand the if else statement using a real-life example. Suppose you want to go to Bangalore from Delhi, to reach Bangalore you have two ways either you will go through flight or by train. You cannot use both flight and train at the same time. When your passport, flight tickets, identification id appropriates then, you will get a flight, but if anyone of the things is missed, then flight may be missed. Now, what's the next option you will go by train. From this example, you can understand that if all conditions are full-filled, then the statement attached with that condition will execute otherwise other option will choose.

Similarly, for the if else statement in C, when the condition is true (non-zero value), then the block of code with if condition will execute otherwise block of code of else will execute.

**Syntax**

if (Condition)

{

//Block of statements

}

else

{

//Block of statements

}

####
**Flow Chart of If Else Statement in C**

### C Program 1

**Write a program to find out the given number is odd or even (Use if else statement in C).**

To find out the given number is even or odd. The number is divided by 2. After division, if the remainder is zero, then the number is even otherwise odd.

**See Also: Dangling Pointer in C****Algorithm**

Step1- Start the program

Step2- Enter data: num = 13

Step3- Number is divided by 2.(num%2)

If the remainder is zero, then the given number is even else number is odd.

Step4- Stop the program

**Code**

#include <stdio.h>

int main()

{

int num, remainder; //Declare Variable

printf ("Enter the number");

scanf ("%d", &num);

remainder = (num%2);

if(remainder ==0)

{

printf ("The number is even %d \n", num);

}

else

{

printf ("The number is odd %d \n", num);

}

return 0;

}

**Output**

Enter the number 6

The number is even 6

### C Program 2

**Write a program to find out the given year is a leap year or not (Using if else statement in C).**

A year is considered a leap year when it is divided by 4. When the remainder (year%4) is zero, it is a leap year; otherwise, it is not a leap year. For instance,

Year = 1990

Remainder = 1990%4

Remainder = 2

**Algorithm**

Step1- Start the program

Step2- Year = 1990

Step3- remainder = year%4 (If the remainder is equal to zero then given year is a leap year else given year is not a leap year).

Step4- Stop the program

**Code**

#include <stdio.h>

int main()

{

int year, remainder; //Declaration of Variable

printf ("Enter the year");

scanf ("%d", &year);

remainder = year%4;

if (remainder == 0)

{

printf ("%d is a leap year \n", year);

}

else

{

printf ("%d is not a leap year \n", year);

}

return 0;

}

**Output**

Enter the year 2019

2019 is not a leap year

### C Program 3

**Write a program to find out the given number is prime or not (Using if else statement in C)**

Those number which is divided by 1 or itself are prime numbers. For instance, 2,3,5,7,19 etc.

**See Also: Preprocessor directive in C****Algorithm**

Step1- Start the program

Step2- Enter the number

Step3- If a number is divided by 1 and itself then given number is a prime number otherwise entered number is not a prime number.

Step4- Exit the program

**Code**

#include <stdio.h>

int main()

{

int num,i,count=0; //Declaration of Variables

printf ("Enter the number\n");

scanf ("%d", &num);

for (i=1;i<=num;i++)

{

if (num%i==0)

{

count++;

}

}

if(count==2)

{

printf("A Prime number is %d\n", num);

}

else

{

printf ("Not a prime number %d \n", num);

}

return 0;

}

**Output**

Enter the number 5

A prime number is 5

### C Program 4

**Write a program to find out the given number is a palindrome or not (Using if else statement in C)**

A number is said to be palindrome when it is reversed it always occur the same. For instance,

number = 16361

When we reverse it, we occurred the same number. So, 16361 is a palindrome number. Suppose, if we try to reverse 163 then we get 361. So, 163 is not a palindrome number.

**Algorithm**

Step1- Start the program

Step2- Declare variables like a number, remainder, result, a

Step3-Initialize variable a = number

Step4- while (number!=0)

remainder = number%10

result = result * 10 + remainder

number = number/10

Step5- If enter number is equal to the output, then that number is palindrome; otherwise, it is not a palindrome number.

Step6-Stop the program

**Code**

#include <stdio.h>

int main()

{

int number=131, remainder, result=0,a; //Initialize variables

a=number;

while (number!=0)

{

remainder = number%10;

result=result*10+remainder;

number = number/10;

}

if (a == result)

{

printf ("%d is a palindrome number \n",a);

}

else

{

printf ("%d is not a palindrome number \n",a);

}

return 0;

}

**Output**

131 is a palindrome number

### C Program 5

**Write a program to find out the given number is Armstrong or not. (Using if else statement in C)**

A number is said to be Armstrong number when the sum of cube of each digit is equal to the number itself. For instance,

Number = 123

Result = (1) ^3 +(2)^3+(3)^3

Result = 1+8+27

Result = 36

Because resultant is not equal to the number, so it is not an Armstrong number.

**Algorithm**

Step1- Start the program

Step2- Enter Data: num = 371

Step3-remainder = num%10

sum = sum + pow(remainder,3)

num = num/a0

Step4- When the sum of cube of each digit is equal to the entered number, then that number is Armstrong number otherwise not an Armstrong number.

Step5- Stop the program

**Code**

#include <stdio.h>

#include <math.h>

int main ( )

{

int num = 371, remainder, i, sum=0, a; //Initialization of variables

a=num;

while (num!=0)

{

remainder = num%10;

sum =sum+pow(remainder,3);

num=num/10;

}

if(a==sum)

{

printf ("%d is Amstrong number \n",a);

}

else

{

printf ("%d is armstrong number\n",a);

}

return 0;

}

**Output**

371 is an Armstrong number

## No comments:

## Post a Comment