Tuesday, October 18, 2011

Example no. 3, page 92


#include<stdio.h>
#include<conio.h>
#define s scanf
#define p printf
int main()
{
    int m, n;
    for(m =2; m <=4; m++)
    for(n = 5; n >= 3; n--)
    p("%d\n", m + n);
    getch();
    return 0;
}


Example no. 2, page 92 output


#include<stdio.h>
#include<conio.h>
#define s scanf
#define p printf

int main()
{
    int m, n, z, h;
    m= 6;
    n= 0;
    for(h =1; h <= m; h++)
    {
          z = 2 * h ;
          n = n + 3;
          p("H = %d\n", h);
          p("z = %d\n", z);
          p("n = %d\n", n;
         
          }
          getch();
          return 0;
     }

Friday, October 14, 2011

IF Statement (Number 9)


Write a program that will display "IT'S COLD!" if the temperature is less than 20, "IT'S HOT!" if the temperature is greater than 30, "COOL CLIMATE!" otherwise.


     #include<stdio.h>
     #include<conio.h>
     #define p printf
     #define s scanf

     int main()
     {
         int T;
  
         p("Enter temperature: ");
         s("%d",&T);
  
              if ( T < 20 )
                  {
                  p("IT'S COLD!");
                  }
             else
             if ( T > 30 )
                 {
                 p("IT'S HOT!");
                 }
            else
                {
                p("COOL CLIMATE!");
                }
       getch ();
       return 0;
    }  

Thursday, October 13, 2011

IF Statement (Number 11)


Write a program to determine the equivalent grade of each student in a class as follows:


      a. Read in the student's name, midterm grade, minor B, and final exam ratings.
      b. Determine the final grade of the student by formula:
          final grade = 0.30 of midterm grade + 0.10 of minor B + 0.60 of final exam.
      c. Determine the equivalent grade for the numerical value obtained by the following grading marks:


                    98 - 100 = 4.00
                    95 - 97 = 3.75
                    92 - 94 = 3.50
                    89 - 91 = 3.25
                    86 - 88 = 3.00
                    83 - 85 = 2.75
                    80 - 82 = 2.50
                    77 - 79 = 2.25
                    74 - 76 = 2.00
                    71 - 73 = 1.75
                    68 - 70 = 1.50                   
                    64 - 67 = 1.25
                    60 - 63 = 1.00                   
                    below 60= 0.00
  
(Solution)


     #include<stdio.h>
     #include<conio.h>
     #define p printf
     #define s scanf


     int main()
     {
         int MidG, MinB, Fin;
         float FG;
         char SN;
  
         p("Enter student's name: ");
         s("%s",&SN);
         p("Enter midterm grade: ");
         s("%d",&MidG);
         p("Enter minor B: ");
         s("%d",&MinB);
         p("Enter final exam: ");
         s("%d",&Fin);
  
         fg = ( MidG * .30 ) + ( MinB * .10 ) + ( Fin * .60 );
         p("Your final grade is %.2f or equivalent to ",FG);
  
              if( FG >= 98 && FG <= 100)
                  {
                  p("4.00");
                  }
              else
              if( FG >= 95 && FG <= 97)
                  {
                  p("3.75");
                  }
              else
              if( FG >= 92 && FG <= 94)
                  {
                  p("3.50");
                  }
              else
              if( FG >= 89 && FG <= 91)
                  {
                  p("3.25");
                  }
              else
              if( FG >= 86 && FG <= 88)
                  {
                  p("3.00");
                  }
              else
              if( FG >= 83 && FG <= 85)
                  {
                  p("2.75");
                  }
              else
              if( FG >= 80 && FG <= 82)
                  {
                  p("2.50");
                  }
              else
              if( FG >= 77 && FG <= 79)
                  {
                  p("2.25");
                  }
              else
              if( FG >= 74 && FG <= 76)
                  {
                  p("2.00");
                  }
              else
              if( FG >= 71 && FG <= 73)
                  {
                  p("1.75");
                  }
              else
              if( FG >= 68 && FG <= 70)
                  {
                  p("1.50");
                  }
              else
              if( FG >= 64 && FG <= 67)
                  {
                  p("1.25");
                  }
              else
              if( FG >= 60 && FG <= 63)
                  {
                  p("1.00");
                  }
              else
                  {
                  p("0.00");
                  }
         getch ();
         return 0;
      }

IF Statement (Age)


Write a program that will display "Pretty Young" if age is lesser than 18,
and will display "You Old" if age is  greater than 18.


(Solution)




#include<stdio.h>
   #include<conio.h>

   int main()
   {
          int AGE;
     
          printf("Enter age:");
          scanf("%d",&AGE);
   
          if ( AGE <= 18 )
         {
               printf("Pretty Young!");
         }
         else    
        {
               printf("Your Old!");
       }
       getch();
       return 0;
}

IF Statement (Numer 8)

Write a program segment that will ask the user if he wants to compute the perimeter or the area of a triangle. If the perimeter is wanted, ask the measure of the three sides and compute for the perimeter. If the are is wanted, ask for the measures of the base and height and compute for the area. Display the computed value.




(Solution)


#include<stdio.h>
#include<conio.h>

int CHOICE, PERIMETER, A, B, C, AREA, BASE, HEIGHT;

int main()
{
       printf("This program will get the perimeter and are of Triangle\n");
       printf("Choices\n");
       printf("1.] Perimeter\n");
       printf("2.] Area\n");
       printf("Enter Choice [1 or 2]: ");
       scanf("%d",&choice);

       if (choice == 1)
          {
              printf("Enter Side A: ");
              scanf("%d",&A);
              printf("Enter Side B: ");
              scanf("%d",&B);
              printf("Enter Side C: ");
              scanf("%d",&C);
              PERIMETER = A + B + C;
              printf("The perimeter of the area of a triangle is %d",PERIMETER);
                   }

      if (choice == 2)
          {
              printf("Enter Base: ");
              scanf("%d",&BASE);
              printf("Enter Height: ");
              scanf("%d",&height);
              AREA = (BASE * HEIGHT)/2;
              printf("The area of a triangle is %d",AREA);
                  }
getch();
return 0;
}

IF Statement (Number 10)

Write a program that gives discount of 100 pesos to a costumer if shirt bought is XL and the price is greater than 500; a discount of 50 pesos if the shirt bought is large and the price is greater than 400.


 

(Solution)

#include<stdio.h>
#include<conio.h>
#define s scanf
#define p printf

int main()
{
    int PRICE, EKS;

    p("Choices:\n");
    p("1.] XL\n");
    p("2.] L\n");


    p("Enter T-Shirt Size: ");
    s("%d", &EKS);



    switch(EKS)

    {
    case 1:
   
         p("Enter Price: ");
         s("%d", &PRICE);
   
         if(PRICE>= 500)
     
         p("You Have Discount 100!");
   
         break;
   
   
   
   
    case 2:
              p("Enter Price: ");
              s("%d", &PRICE);
         
              if(PRICE >= 400)
         
              p("You have Discount 50!");
         
              break;
         
   
         default:
                  p("thank you!");
             
                  break;
                  }
         getch();
         return 0;
         }

IF Statement(Grade)

Write a program that will input a student's grade and will display
"Congratulations you PASSED!" if the student's grade is greater than or equal 60. Otherwise "Sorry you FAILED!".
 
#include<stdio.h>
#include<conio.h>
#define p printf
#define s scanf
int GRADE;
int main( )
{


    p("Enter Grade of Student:");
    s("%d",&GRADE);
    if(GRADE >= 60)
    p("Congratulations you PASSED!");
    else
    p("Sorry you FAILED!");
    getch();
    return 0;
}

Tuesday, October 11, 2011

Even numbers being looped(iteraive)

Write a program that asks the user to enter a number n and display the first even numbers. Example: if n=5, the first 5 even numbers are 2, 4, 6, 8 and 10. 


(Solution)

#include<stdio.h>
#include<conio.h>

int main( )
{
   int ONE, TWO;
   printf("Enter Number: ");
   scanf("%d",&TWO);
   for (ONE=0; ONE<=TWO; ONE+=2)
     {
          printf ("%d\n",ONE);
      }

getch( );
return 0;
}

Monday, October 10, 2011

Odd Numbers Being Looped(Iterative)


Write a program that asks the user to enter a number n and display the first odd numbers. Example: if n=5, the first 5 even numbers are 1, 3, 5, 7 and 9.

(Solution)

#include<stdio.h>

#include<conio.h>
#define s scanf
#define p printf
int main( )
{
          int M, N, O;

          p("Enter number amount of odd: ");
          s("%d", &N);
          
          for(M=1; M<=N; M++)
          {
                  O = M + M - 1;
                  p("%d\n", O);
           }
getch( );
}