Sunday, 9 December 2018

IF-ELSE STATEMENT IN C

IF-ELSE STATEMENT IN C




If-else statement is decision-making statement. if-else statement is an extension of if statement. We try to understand 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 will appropriate then you will get a flight but if any one 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, when if-condition is true then the block of code with if condition will execute otherwise block of code of else will execute.

Syntax:         
                                                    
if (condition)
{

// block of code

}
else
{

// block of code

FLOW CHART OF if-else statement in C





if-else statement in C
                                                  Flow Chart of the if-else statement in C


Program 1: Write a program to find out given number is odd or even.

Theory


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

Algorithm  

                                                                                                                                    

Step1: Start the program.
Step2: Input Data: num=13.
Step3:  Number is divided by 2.
          If the remainder is zero then given number is even else given number is odd.
Step4: Stop the program.

Flow Chart of Odd/Even Number Using  if-else statement in C




Flow chart of odd/even number program

Program


#include <stdio.h>
int main()
{              
    int num=13, remainder;
    remainder= 13%2;
    if (remainder==0)
    {
        printf ("Number is even  %d \n", num);
    }
    else
    {
        printf ("Number is odd %d \n",  num);
    }
    return 0;
}

Output



if-else statement in C

Program 2: Write a program to find out given year is a leap year or not.


 Theory


A year is considered as a leap year when it is divided by 4. When the remainder is zero it is a leap year otherwise it is not a leap year.


For example         

year =1990


                              Remainder = 1990 % 4 = 2


                             1990 year is not a leap year.


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.


Flow Chart of Leap Year Using if-else statement in C



if-else statement in C

Flow Chart of the leap year program

Program


#include <stdio.h>
int main()
{
    int year, remainder;
    printf ("Enter year");
    scanf ("%d", &year);
    remainder = year%4;
    if (remainder == 0)
    {                                      
        printf ("%d year is a leap year \n", year);
    }
    else
    {
        printf ("%d year is not a leap year \n", year);
    }
    return 0;
}

Output



if-else statement in C


Program 3: Write a program to find out given number is prime or not.


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

Algorithm
Step1: Start the program.
Step2:  Enter number.
Step3: If a number is divided by 1 and itself then given number is prime number otherwise.
Step4: Stop the program.
             

Flow Chart of  Prime Number using if-else statement in C             

               

if-else statement in C


Flow chart of prime number program using  if-else statement in C

Program


#include <stdio.h>
int main()
{               
    int num=4,i,count=0;
    for (i=1;i<=num;i++)
    {
        if (num%i==0)
        {
            count++;
        }

    }
    if(count==2)
    {
        printf("prime number is %d\n", num);
    }
    else
    {
        printf ("Not a prime number %d \n", num);
    }
    return 0;
}

Output



if-else statement in C

Program 4: Write a program to find out given number is palindrome or not.


Theory




A number is said to be palindrome when it is reversed it always occur same.


For example 



number  = 16361
   

When we reverse it, we occurred the same number. So, 16361 is palindrome number.
               

If we reverse 163 then we get 361. So, 163 is not a palindrome number.




Algorithm         


                   

Step1: Start the program.

Step2: Input Data: num = 131, remainder, result=0, a.
            a = num
Step3: while (num !=0)
           remainder = num%10
            result=result*10+remainder;
            num=num/10;
Step4: if enter number is equal to output then that number is palindrome otherwise it is not a  palindrome number.

Step5: Stop the program.


Flow Chart of Palindrome Number Using if-else statement in C



if-else statement in C


Flow Chart of palindrome number using if-else statement in C



Program



#include <stdio.h>

int main()

{       

    int num=131, remainder, result=0,a;

    a=num;

    while (num !=0)

    {

        remainder = num%10;

        result=result*10+remainder;

        num=num/10;

    }

    if (a==result)

    {

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

    }

    else

    {

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

    }

    return 0;

}


Output



if-else statement in C


Program 5: Write a program to find out given number is Armstrong or not.


Theory



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

For example:

Number = 123

Result = (1)3 + (2)3 + (3)3          = 1+ 8+27 = 36

Because, 36 is not equal to 123, so it is not an Armstrong number.



Algorithm     


             

Step1: Start the program.
Step2: Input Data: num=371
Step3:  remainder = num%10
            sum=sum+pow(remainder,3)
            num=num/10
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.




Flow Chart of Armstrong number program using if-else statement in C






if-else statement in C

Flow chart of finding out Armstrong number using if-else statement in C


Program



#include <stdio.h>
#include <math.h>
int main ( )
{           
    int num = 371, remainder, i,  sum=0, a;
    a=num;
    while (num!=0)
    {
        remainder = num%10;
        sum =sum+pow(remainder,3);
        num=num/10;
    }
    if(a==sum)
    {
        printf ("%d is armstrong number \n",a);
    }
    else
    {
        printf ("%d is armstrong number\n",a);
    }
    return 0;
}


Output

if-else statement in C

 








No comments:

Post a Comment