If Statement

 

1.What is if in C ?

Ans:  The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statements is executed otherwise not.


Working of if :


Syntax  :

 if (condition)----if condition is true 

 {

        Statement; -----Execute this ststement

  }

 

  Example:

 

1. Write a program to find that the number is positive numbers.

 

#include<stdio.h>    //Header file

int main ()                 //main

{

        int num;     // Declaring variable

        printf("Enter any number");   

        scanf("%d",&num);

        if (num>0)

        {

                printf("Positive Number");

        }

}

 

  Output:




2. Write a program to find that he number is even or not .

#include<stdio.h>   //Header file
int main()         //main
{
    int num;   // Declaring variable
    printf("enter your favourite number");
    scanf("%d",&num);
    if(num%2==0)
    {
printf("%d is even",num);
    }
}

  Output:


3.Write a program to see that the two numbers enter are equal.

 #include<stdio.h>  //Header file

int main()    //main

{

int a,b;   // Declaring variable

printf("assgin a value for a");

scanf("%d",&a);

printf("assgin a value for b");

scanf("%d",&b);

if(a==b)

{

printf("the numbers are equal");

}

}   

Output:      

 




4. Write a program to find number of notes in the amount enter.

#include<stdio.h>
int main ()
{
int note2=500,note3=200,note4=100,note5=50,note6=20,note7=10,coin1=5,coin2=2,coin3=1,amount,a;
printf("Enter the amount");
scanf("%d",&amount);
if(amount>=500)
{
note2=amount/note2; // 2400 -4 
amount=amount%500; // 2400/4 
printf("Number of 500 notes is %d\n",note2);
}
if (amount>=200)
{
note3=amount/note3; // 2
amount=amount%200; //
printf("Number of 200 notes is %d\n",note3);
}
if (amount>=100)
{
note4=amount/note4; 
amount=amount%100; 
printf("Number of 100 notes is %d\n",note4);
}
if (amount>=50)
{
note5=amount/note5; // 2
amount=amount%50; //
printf("Number of 50 notes is %d\n",note5);
}
if (amount>=20)
{
note6=amount/note6; // 2
amount=amount%20; //
printf("Number of 20 notes is %d\n",note6);
}
if (amount>=10)
{
note7=amount/note7; // 2
amount=amount%10; //
printf("Number of 10 notes is %d\n",note7);
}
if (amount>=5)
{
coin1=amount/coin1; // 2
amount=amount%5; //
printf("Number of 5 coin is %d\n",coin1);
}
if (amount>=2)
{
coin2=amount/coin2; // 2
amount=amount%2; //
printf("Number of 2 coin is %d\n",coin2);
}
if (amount>=1)
{
coin3=amount/coin3; // 2
amount=amount%1; //
printf("Number of 1 coin is %d\n",coin3);
}
}

Output: