5.3 – programs to practice – if statement

by subbu on September 14, 2013

1) Accept any number and print whether the number is a positive,  negative or zero.

/* program to find zero, positive or negative */
#include<stdio.h>
int main()
{
float n;
printf("Enter any number:");
scanf("%f",&n);
if(n==0)/* checking for zero*/
  printf("Zero");
if(n>0)/*checking for positive*/
  printf("Positive number");
if(n<0)/*checking for negative*/
  printf("Negative number");
return 0;
}

Execution 1:
Enter any number: 25.56
Positive number

Execution 2:
Enter any number: -40.89
Negative number

Execution 3:
Enter any number: 0
Zero

Example explained:
At any given case only a single condition will be true, left all the conditions will be false.

2) Accept any integer and print whether the number is an even or odd.

/* program to check even or odd */
#include<stdio.h>
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
if(n%2==0)/*checking for even*/
  printf("Even number");
if(n%2==1)/*checking for odd*/
  printf("Odd number");
return 0;
}

Execution 1:
Enter any integer: 25
Odd number

Execution 2:
Enter any integer: 24
Even number

3) Accept any two numbers and print whether equal, if not print the biggest number.

/*program to print biggest number*/
#include<stdio.h>
int main()
{
float a,b;
printf("Enter two numbers:");
scanf("%f%f",&a,&b);
if(a==b)
  printf("Equals");
if(a>b)
  printf("Biggest number %6.2f",a);
if(b>a)
  printf("Biggest number %6.2f",b);
return 0;
}

Execution 1:
Enter two numbers: 10.89  10.89
Equals

Execution 2:
Enter two numbers: 40.34  80.89
Biggest number 80.89

Execution 3:
Enter two numbers: 80.14 20.88
Biggest number 80.14

4) Accept the price, quantity of a product and print the bill, discount and the net bill. Allow 5% discount if bill exceeds to 500 else allow 3% discount.

/* program to print bill, discount and net_bill*/
#include<stdio.h>
int main()
{
int qty;
float price,bill,dis,net_bill;
printf("Price:");
scanf("%f",&price);
printf("Quantity:");
scanf("%d",&qty);
bill=price*qty;
if(bill>500)
  dis=bill*5/100;
if(bill<=500)
  dis=bill*3/100;
net_bill=bill-dis;
printf("Bill %8.2f",bill);
printf("\nDiscount %8.2f",dis);
printf("\nNet bill %8.2f",net_bill);
return 0;
}

Execution:
Price:45.75
Quantity:12
Bill   549.00
Discount    27.45
Net bill   521.55

5) Accept any digit from 0 to 6 and print appropriate day. For example Sunday for 0, Monday for 1… Saturday for 6

/* program to print days */
#include<stdio.h>
int main()
{
int n;
printf("Enter any digit from 0 to 6:");
scanf("%d",&n);
if(n==0)
  printf("Sunday");
if(n==1)
  printf("Monday");
if(n==2)
  printf("Tuesday");
if(n==3)
  printf("Wednesday");
if(n==4)
  printf("Thursday");
if(n==5)
  printf("Friday");
if(n==6)
  printf("Saturday");
return 0;
}

Execution 1:
Enter any digit from 0 to 6: 3
Wednesday

Execution 2:
Enter any digit from 0 to 6: 1
Monday

Summery:

While solving a problem, we need to identify all the possibilities and their solutions. While writing program, if conditional statements are prepared to check individual possibilities and their solutions are written under appropriate conditional statements.

While executing the program, conditions are checked one after another and proper statements are executed according to the result of conditions (true or false).

Previous post:

Next post: