Friday, 2 November 2012

The If Else statement


•to execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false

example:

•If his basic salary is less than RM 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above RM 1500, then HRA = RM 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
so the coding is as follow:
#include <stdio.h>
int main(void)
{
    int basicsallary, a, aa, sallary, sallaries;
    // get input
    printf("\nplease enter your basic sallary=");
    scanf("%d",&basicsallary);
    a = basicsallary - (0.1*basicsallary);
    aa = basicsallary - 100 ;
    sallary = a + 500 ;
    sallaries = aa + 800 ;
   
    //condition
                 if (basicsallary > 1500)
                 {
                  printf("your new basic sallary is = %d", a);
                 }
                 if (basicsallary < 1500)
                 {
                  printf("your new basic sallary is = %d,aa");
                 }
                 if (a > 3000)
                 {
                 printf("your new total sallary is = %d", sallary);
                 }
                 if(aa < 3000)
                 {
                 printf("your new total sallary is = %d",sallaries);
                 }
                  scanf("%d",&basicsallary);  
                 
return 0;          
}

NUMBER 1 chapter 2


•Many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation.
 

•a decision control instruction can be implemented in C using:

–The if statement

–The if-else statement
 
    –The conditional operators

 Example of flowchart
example of coding:
#include <stdio.h>
int main (void)
{
     int jyear,cyear,serv;
     printf("please enter your join year=\n");
     scanf("%d",&jyear);
     printf("please enter the year now=\n");
     scanf("%d",&cyear);
     serv = cyear - jyear;
     if(serv>3)
     {
               printf("wahhhh dapat bonus!!\n");
     }
     scanf("%d",&jyear);
return 0;
}   
 

CHAPTER 2

CONSIST OF:

1-Decisions!decisions!
2-The If statement
3-The If-Else statement
4-A word of caution
5-The conditional operators

ESCAPE SEQUANCE

# An escape sequances starts with a backslash and followed by 1 or more special characters as follows:

=> \n -for newline
=> \t -for horizontal tab
=> \a -for sound alert
=> \\ -for notes(will not effect the program)
=> \" -double coute.use to print double quote character

EXAMPLE:
#include<stdio.h>
void main(void)
{
printf("\a\a\Hi! Welcome to Programming..:)\n");
printf("Thanks for using C++\a\n")
}  

example of define program


#include <stdio.h>

#define X1  b+c

#define X2  X1+X1

#define X3  X2*c+X1-d
 
#define X4  2*X1+3*X2+4*X3

main( )

{

  int b=2;

  int c=3;

  int d=4;

  int e=X4;

  printf(" %d  %d  %d  %d  %d ", e, X1, X2, X3, X4);

  return 0;
}

Thursday, 1 November 2012

 A First C Program
// A first C  program 

#include <stdio.h> 



//(this is header)

main( )

{

  printf( “Welcome  to C!\n”);

  printf( “Thank you for using

 me!\n”);

  return 0;

}
Basic Structure of C program


#main( )  - Every program must have a main( ) function and it is a first function that will be called.

The main( ) function is the one to which control is passes when the program is executed


•{ }  These are known as braces or curly brackets. 


•The opening curly brackets indicates that a block of statements is about to begin. 
 
•The statements  of the functions are enclosed in these curly brackets.
  

•The closing curly brackets terminates the block of the statements.