Monday, March 28, 2011

EXERCISE (IF...ELSE & SWITCH...CASE)


2. CALCULATE (IF...ELSE)

CALCULATE:
a) RECTANGULAR
b) TRIANGLE
c) CIRCLE
d) FAHRENHEIT
e) CELCIUS

Your Selection:

Your Selection is _________ and you are calculate _________. The answer is _____________
 ANSWER :

#include <iostream.h>
main()
{
      //declare
      float b,h,r,w,area;
      float c,f,cel,fah;
      float pi=3.14;
      int selection;
      //out
      cout<<"Please choose you choice"<<endl;
      cout<<"1.Rectangular";
      cout<<"\n2.Triangle";
      cout<<"\n3.Circle";
      cout<<"\n4.Fahrenheit";
      cout<<"\n5.Celcius"<<endl;
      cout<<"Your Selection is:"<<endl;
      cin>>selection;
      switch (selection)
      {
         case 1: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the width"<<endl;
                     cin>>w;
                     //process
                     area = w*h;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Rectangular"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 2: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the base"<<endl;
                     cin>>b;
                     //process
                     area = 0.5*b*h;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Triangle"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 3: cout<<"Please input the radius"<<endl;
                     cin>>r;
                     //process
                     area = pi*r*r;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Circle"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 4: cout<<"Please insert the celcius"<<endl;
                     cin>>c;
                     //process
                     fah= 9/5*(c+32);
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Fahrenheit"<<endl;
                     cout<<"The answer is:"<<fah;
                     break;
         case 5: cout<<"Please insert the fahrenheit"<<endl;
                     cin>>f;
                     //process
                     cel = 5/9*(f+32);
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Celcius"<<endl;
                     cout<<"The answer is:"<<cel;
                     break;
         default :cout<<"Invalid Selection";
          return 0;
             }
}


HOMEWORK 2

#include <iostream.h>
main ()

{
//variable
float sub1,sub2,sub3,sub4,sub5,cpa;
//input1
cout<<"insert subject1:";
cin>>sub1;
//input2
cout<<"insert subject2:";
cin>>sub2;
//input3
cout<<"insert subject3:";
cin>>sub3;
//input4
cout<<"insert subject4:";
cin>>sub4;
//input5
cout<<"insert subject5:";
cin>>sub5;
//process
cpa=((sub1*3)+(sub2*3)+(sub3*2)+(sub4*2)+(sub5*4))/5;
//output
cout<<"subject 1:"<<sub1<<endl;
cout<<"subject 2:"<<sub2<<endl;
cout<<"subject 3:"<<sub3<<endl;
cout<<"subject 4:"<<sub4<<endl;
cout<<"subject 5:"<<sub5<<endl;
cout<<"CPA is:"<<cpa<<endl;
return 0;
}

HOMEWORK 1

#include<iostream.h>
main()

{
//variable
float as1,as2,as3,as4,as5,mid,final,total;
//input1
cout<<"assignment 1:";
cin>>as1;
//input2
cout<<"assignment 2:";
cin>>as2;
//input3
cout<<"assignment 3:";
cin>>as3;
//input4
cout<<"assignment 4:";
cin>>as4;
//input5
cout<<"assignment 5:";
cin>>as5;
//input6
cout<<"midterm:";
cin>>mid;
//input7
cout<<"final:";
cin>>final;
//formula
total = as1+as2+as3+as4+as5+mid+final;
//output
cout<<"assignment 1 is:"<<as1<<endl;
cout<<"assignment 2 is:"<<as2<<endl;
cout<<"assignment 3 is:"<<as3<<endl;
cout<<"assignment 4 is:"<<as4<<endl;
cout<<"assignment 5 is:"<<as5<<endl;
cout<<"mid score is:"<<mid<<endl;
cout<<"final score is:"<<final<<endl;
cout<<"total is:"<<total;
return 0;
}


Friday, March 25, 2011

Sifir

#include <iostream.h>
main ()
{
     int i,j;
     for (i=1;i<=12;i++)
     {
         cout<<"\n";
         for (j=1;j<=12;j++)
         {cout<<" "<<i*j;}
         }
           return 0;
         }

TIN MINUMAN

#include <iostream.h>
main()
{
      //declare
      int selection;
      float money,balance,price,addm,add;
      //out
      cout<<"\n******************";
      cout<<"\nMesin Tin Minuman";
      cout<<"\n------------------";
      cout<<"\n1.PEPSI";
      cout<<"\n2.COCA COLA";
      cout<<"\n3.F&N MIRINDA";
      cout<<"\n4.F&N ORANGE";
      cout<<"\n5.100 PLUS";
      cout<<"\n******************";
      //input
      cout<<"\nPlease Choose Your Drink=";
      cin>>selection;
  
      cout<<"\nPlease Enter MONEY: ";
      cin>>money;
  
      switch (selection)
      {
             case 1: cout<<" Your drink is Pepsi";
                     price = 2.00;
                     break;
             case 2: cout<<" Your drink is Coca Cola";
                     price = 2.00;
                     break;
             case 3: cout<<" Your drink is F&N Mirinda";
                     price = 1.80;
                     break;
             case 4: cout<<" Your drink is F&N Orange";
                     price = 1.80;
                     break;
             case 5: cout<<" Your drink is 100 Plus";
                     price = 2.50;
                     break;
             default: cout<<"invalid selection";
             }
  
      if (money>price)
      {
                      balance = money - price;
      }else if (money<price)
                            {
                                add = price - money;
                                cout<<"Please add (RM" <<add<< ")= ";
                                cin>>addm;
                                balance = money + addm;
                                }
                                cout<<"Your balance is ";
                                return 0;
}