#include<stdio.h>
void darab(void);
void tolak(int a,float b);
char zzz(void);
//------------------------------------------------------------------------------
int main()
{
int a;
float b;
char kotak;
a=10;
b=5.5;
darab();
tolak(a,b);
kotak = zzz();
printf("%c",kotak);
getchar();
return (0);
}
//------------------------------------------------------------------------------
void darab(void)//get nothing return nothing
{
int a, b, c;
a=2;
b=2;
c=a*b;
printf("total =a*b=%d\n",c);
}
//------------------------------------------------------------------------------
void tolak(int a,float b)//get something return nothing
{
float sum;
sum=a-b;
printf("total =a-b=%.2f\n",sum);
//xpayah letak return (0); sbb dye return nothing
}
//------------------------------------------------------------------------------
char zzz(void)//get nothing return something
{
return('a');
}
Programming 1 Nor Shiella Wahab
Thursday, 8 November 2012
Monday, 5 November 2012
Chapter 4-Loops
There are 4 loops :
Definiton of loops:
->Loops is use to repeat a program so that we do not need to re-write the coding again and again.
the output is:
- The while loops
- The do-while loops
- The for loops
- The odd loops
Definiton of loops:
->Loops is use to repeat a program so that we do not need to re-write the coding again and again.
•There are
three methods by way of which we can repeat a part of a program. They are:
–(a) Using a while statement
–(b) Using a for statement
–(c) Using a do-while statement
The while loop:
initialize loop counter ;
while ( test condition )
{
do this ;
and this ;
increment loop counter ;
}
The do-while loop:
do
{
this
;
and
this ;
and
this ;
and
this ;
}
while ( this condition is true ) ;
Do-while loop will still run even the first time is an error.
The for loops:
for ( initialize counter; test counter; increment
counter
)
{
do this ;
and this ;
}
The odd loops:
example of odd loop
#include <stdio.h>
int main(void )
{
char another ;
int num ;
do
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;
}
int main(void )
{
char another ;
int num ;
do
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;
}
thats the end of chapter 4...
Friday, 2 November 2012
Chapter 3-Switch case
SWITCH CASE:
decisions using switch case
#include <stdio.h>
int main ( )
{
int marks,status;
printf("\nEnter marks = ");
scanf("%d",&marks);
if (marks > 79 && marks < 100)
printf("\nA,Pass\n");
else if(marks > 60 && marks < 79)
printf("\nB,Pass\n");
else if(marks > 40 && marks < 59)
printf("\nC,Pass\n");
else
printf("\nF,Fail\n");
scanf("%d",&marks);
return 0;
}
example of switch case..:
and the flowchart:
and this is my example:
#include<stdio.h>
int main(void)
{
int select,area,w,h;
printf("**********program to calculate area rectangle********\n");
printf("* please select an option *\n");
printf("* *\n");
printf("* 1.rectangle *\n");
printf("* 2.exit *\n");
printf("**********************************************\n");
scanf("\n%d,",&select);
switch(select)
{
case 1:
printf("program to calculate rectangle\n");
//printf("calculate the area of rectangle");
printf("key in the width for calculation = ");
scanf("\n%d",&w);
printf("key in the height for calculation = ");
scanf("\n%d",&h);
area=w*h;
goto answer;
break;
default:
printf("wrong case selelcted!!!");
goto exit;
}
answer:printf("the area is = %d\n",area);
exit:;
scanf("\n%d,",&select);
}
decisions using switch case
#include <stdio.h>
int main ( )
{
int marks,status;
printf("\nEnter marks = ");
scanf("%d",&marks);
if (marks > 79 && marks < 100)
printf("\nA,Pass\n");
else if(marks > 60 && marks < 79)
printf("\nB,Pass\n");
else if(marks > 40 && marks < 59)
printf("\nC,Pass\n");
else
printf("\nF,Fail\n");
scanf("%d",&marks);
return 0;
}
example of switch case..:
void
main(void)
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in
case 1 \n" ) ;
break ;
case 2 :
printf ( "I am in
case 2 \n" ) ;
break ;
case 3 :
printf ( "I am in
case 3 \n" ) ;
break ;
default :
printf ( "I am in
default \n" ) ;
}
}
and the flowchart:
and this is my example:
#include<stdio.h>
int main(void)
{
int select,area,w,h;
printf("**********program to calculate area rectangle********\n");
printf("* please select an option *\n");
printf("* *\n");
printf("* 1.rectangle *\n");
printf("* 2.exit *\n");
printf("**********************************************\n");
scanf("\n%d,",&select);
switch(select)
{
case 1:
printf("program to calculate rectangle\n");
//printf("calculate the area of rectangle");
printf("key in the width for calculation = ");
scanf("\n%d",&w);
printf("key in the height for calculation = ");
scanf("\n%d",&h);
area=w*h;
goto answer;
break;
default:
printf("wrong case selelcted!!!");
goto exit;
}
answer:printf("the area is = %d\n",area);
exit:;
scanf("\n%d,",&select);
}
Logical Operators
•C
allows usage of three logical operators, namely, &&, || and !. These
are to be read as
‘AND’ ‘OR’ and ‘NOT’ respectively.
•Don’t
use the single symbol | and &. These single symbols also
have a meaning. They are bitwise operators
example question:
•Example 2.4
: The marks obtained by a
student in 5 different subjects are input through the keyboard. The student
gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 -
Fail
so the coding are as follow:
#include <stdio.h>
int main ( )
{
int marks,status;
printf("\nEnter marks = ");
scanf("%d",&marks);
if (marks > 79 && marks < 100)
printf("\nA,Pass\n");
else if(marks > 60 && marks < 79)
printf("\nB,Pass\n");
else if(marks > 40 && marks < 59)
printf("\nC,Pass\n");
else
printf("\nF,Fail\n");
scanf("%d",&marks);
return 0;
}
int main ( )
{
int marks,status;
printf("\nEnter marks = ");
scanf("%d",&marks);
if (marks > 79 && marks < 100)
printf("\nA,Pass\n");
else if(marks > 60 && marks < 79)
printf("\nB,Pass\n");
else if(marks > 40 && marks < 59)
printf("\nC,Pass\n");
else
printf("\nF,Fail\n");
scanf("%d",&marks);
return 0;
}
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;
}
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
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;
}
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
1-Decisions!decisions!
2-The If statement
3-The If-Else statement
4-A word of caution
5-The conditional operators
Subscribe to:
Posts (Atom)