If Statement Program With Explanation

If Statement Programs With Explanation And Code
\\ If you have any query or questions on my post, then please comment below //

Write a program to read 4 values a, b, c and d from the keyboard. And evaluate the ratio of (a+b) to (c-d) and print the result only when c-d not equal to 0. 

Solution: In the above program First, you have to find ratio, so for that first you have to find the value of (a+b) and (c+d). Then divide both the value and you will get the ratio. Now Second Thing in this program is to print value only when the result of (c-d) is not equal to 0. (In c program not equal to 0 write in “!=0”). 
------------------------------------------------------------------------
#include<stdio.h> 
#include<conio.h> 
void main(

int a,b,c,d,x,y,z;
clrscr();
printf(“Enter The Value Of a,b,c and d”); 
scanf (“%d%d%d%d”, &a, &b, &c, &d); 
x=a+b; 
y=c-d; 
z=x/y;                   // Checking ratio 
if (y!=0)              // Print only when c-d is not equal to 0. 
  { 
    printf(“%d”,z); 
   } 
getch();

------------------------------------------------------------------------------- 

Write a program to add two numbers and print only when the addition of two numbers is greater than 10.

Solution: First we have to add 2 numbers, then check the result is greater than 10 or not, this is one of the simple if statement example. 

------------------------------------------------------------------------
#include<stdio.h> 
#include<conio.h> 
void main(

int a,b,c;
clrscr();
printf(“Enter Two Numbers:”); 
scanf (“%d%d”, &a, &b); 
c=a+b;                //Adding 2 Numbers
if (c>10)              // Print only when c-d is not equal to 0.   { 
    printf(“%d”,c); 
   } 
getch();

-------------------------------------------------------------------------------

Write a program to print given message, according to user input: 
If user give input as 1 then print message "One Button Pressed". And if user give input as any other number then it will not show anything.

Solution: In the above program you see we have to print message according to user input. And here our question is divided into two parts:
First: if the user gives the input as One (1) then print message as "One Button Pressed".
Second: if the user gives the input anything else, then 1 then it will not show anything.
So for the first part we use if statement as "if (number ==1)" then print a message, but in the second part we don't have to write anything. Because here we don't have to show anything to the user. And for that we no need to write anything. It will automatically show a blank screen.
------------------------------------------------------------------------
#include<stdio.h> 
#include<conio.h> 
void main(

int a;
clrscr();
printf(“Enter A Number:”); 
scanf (“%d”, &a); 

if (a == 1)              // Print only when a is equal to 1.   { 
    printf(“ One Button Pressed"); 
   } 
getch();

------------------------------------------------------------------------------- 
if you have any query or you find any error in the above code then please comment below !!!!!

Click Here To Know How To Download Above Video Tutorials Or YouTube Videos Free


 Enter Your Email Id To Get Our Programming, Web Development, Video Tutorial, Job Updates In Mail :

Labels: