Friday, 12 August 2016

Introduction to if -else :


Suppose you go to market with 1000 rs and you have to buy chips ,cold drink , pen ,books.
So let us suppose that you think like a robot.
1. find price of chips.
2. find price of cold drink
3. find price of pen.
4. find price of books.
5. total the price and you remember  the total.
6. check the total with amount which you have:
     Now time to take a decision .
7. If total is less than 1000(your amount)'
     you buy everything and get the change.
8. If total is more than 1000 :
      then :
              return the books.
                           now again check the total
                                   if total is less than 1000:
                                              purchase and get change.
                                  if not then dont purchase anything

so this continues....

let us write  a c program using this.


#include<stdio.h>
int main()
{
        int ch.co.p.b;
         printf("enter the amount of chips , cold drink, pen books");
         scanf("%d %d %d %d", &ch,&co,&p,&b);
     
         int amount=1000;
     
          int total=ch+co+p+b;
        int change=0;
     // now the comparison started ///////////    
   
      if(total<=1000)
     {
                 printf("I will buy all");
                 change=1000-total;
                printf(" I got the change %d",change);
     }
     else    //it means total > 1000
     {
               printf("i will return the books ");
              total=total-b;
     
              if (total <=1000)
              {
                    printf(" I will buy all  except books ") ;
                     change=1000-total ;
                  printf("i got change %d",change);
              }
             else
            {
                printf(" I am leaving and I am angry");
            }
     }
 
}
 
Try reading the code using the flowchart given and comment your problems.

Happy coding.



     
       


No comments:

Post a Comment