Sei sulla pagina 1di 12

Examples

• Eng Mosa

CMSC 104, Version 9/01 1


example
• #include <stdio.h>
• #include<conio.h>
• int main ()
• {
• /* variable definition: */
• int a, b;
• int c;
• float f;
• /* actual initialization */
• a = 10;
• b = 20;
• c = a + b;
• printf("value of c : %d \n", c);

• f = 70.0/3.0;
• printf("value of f : %f \n", f);
• getch();
• return 0;
CMSC 104, Version 9/01 2
example
• #include <stdio.h>
• #include<conio.h>
• int main ()
• {
• /* variable definition: */
• int a, b;
• int c;
• /* user initialization */
• printf("enter value\n");
• scanf("%d",&a);
• printf("enter value\n");
• scanf("%d",&b);
• c = a - b;
• printf("value of c : %d \n", c);
• getch();
• return 0;
CMSC 104, Version 9/01 3
• #include <stdio.h>
• #include<conio.h>
• int main ()
• {
• /* variable definition: */
• int x;
• int y;
• /* variable initialization */
• printf("enter value\n");
• scanf("%d",&x);
• printf("enter value\n");
• scanf("%d",&y);
• int temp;
• temp=x;
• x=y;
• y=temp;
• printf("value of c : %d \n", x);
• printf("value of c : %d \n", y);
• getch();
• return 0;

CMSC} 104, Version 9/01 4
IF–THEN–ELSE STRUCTURE

• The algorithm for the flowchart is as follows:


If A>B then
print A
Else if
print B
Else if
else

CMSC 104, Version 9/01 5


Comparison operator

• > greater than 5 > 4 is TRUE


• < less than 4 < 5 is TRUE
• >= greater than or equal 4 >= 4 is TRUE
• <= less than or equal 3 <= 4 is TRUE
• == equal to 5 == 5 is TRUE
• != not equal to 5 != 4 is TRUE
• Note == for comparison if(x==5)
• = for assignment x=5; actual value

CMSC 104, Version 9/01 6


example
• #include <stdio.h>
• #include<conio.h>
• int main() /* Most important part of the program! */
• {
• int age; /* Need a variable... */
• printf( "Please enter your age \n" ); /* Asks for age */
• scanf( "%d", &age ); /* The input is put in age */

• if ( age < 100 ) { /* If the age is less than 100 */


• printf ("You are pretty young!\n" ); /* Just to show you it works... */
• }
• else if ( age == 100 ) { /* I use else just to show an example */
• printf( "You are old\n" );
• }
• else {
• printf( "You are really old\n" ); /* Executed if no other statement is */
• }
• getch();
• return 0;


CMSC} 104, Version 9/01 7
• #include <stdio.h>
• #include<conio.h>
• int main() /* Most important part of the program! */
• {
• int value; /* Need a variable... */

• printf( "Please enter your num \n" ); /* Asks for num */


• scanf( "%d", &value ); /* The input is put in num */
• if ( value > 0 ) { /* If the num is greater than 0 */
• printf ("positive!\n" ); /* positive... */
• }
• else if ( value < 0 ) { /* negative */
• printf( "negative\n" );
• }
• else {
• printf( "equality\n" ); /* Executed if no other statement is */
• }
• getch();
• return 0;

• }

CMSC 104, Version 9/01 8


• #include <stdio.h>
• #include<conio.h>
• int main() /* Most important part of the program! */
• {
• char value; /* Need a variable... */
• printf( "Please enter your martied status \n" ); /* Asks for martied status */
• scanf( "%c", &value ); /* The input is put in status */
• if ( value== 's' )
• { /* If the status is s than 0*/
• printf ("single!\n" ); /* single... */
• }
• else if ( value== 'm' )
• { /* m */
• printf( "married\n" );
• }
• else
• {
• printf( "egaged\n" ); /* Executed if no other statement is */
• }
• getch();
CMSC 104, Version 9/01 9
Note

• The result of 10 modulo 5 is 0 because the


remainder of 10 / 5 is 0.
• The result of 7 modulo 5 is 2 because the
remainder of 7 / 5 is 2.

• 5%7 =5

CMSC 104, Version 9/01 10


• #include <stdio.h>
• #include<conio.h>
• int main () {
• /* variable definition: */
• int a;
• /* user initialization */
• printf("enter value\n");
• scanf("%d",&a);
• if(a%2==0)
• {
• printf("even\n");
• }
• else
• {
• printf("odd");
• }
• getch();
• return 0;
• }

CMSC 104, Version 9/01 11


Tasks

• write c program that read total second from


user and print for it with hour , minute,
seconds
• Write c program that make calculator
• Write c program that swap two variable
without using temp variable

CMSC 104, Version 9/01 12

Potrebbero piacerti anche