Example 1: Adding Two Integers
Source Code:
Example 2: Decision Making: Equality and Relational Operators
Source Code:
Output:
Source Code:
/* Fig. 2.5: fig02_05.c
2 Addition program */
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
int integer1; /* first number to be input by user */
int integer2; /* second number to be input by user */
int sum; /* variable in which sum will be stored */
printf( "Enter first integer\n" ); /* prompt */
scanf( "%d", &integer1 ); /* read an integer */
printf( "Enter second integer\n" ); /* prompt */
scanf( "%d", &integer2 ); /* read an integer */
sum = integer1 + integer2; /* assign total to sum */
printf( "Sum is %d\n", sum ); /* print sum */
system("pause");
return 0; /* indicate that program ended successfully */
} /* end function main */
Output:
Example 2: Decision Making: Equality and Relational Operators
Source Code:
/* Fig. 2.13: fig02_13.c
Using if statements, relational 3 operators, and equality operators */
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
int num1; /* first number to be read from user */
int num2; /* second number to be read from user */
printf( "Enter two integers, and I will tell you\n" );
printf( "the relationships they satisfy: \n" );
scanf( "%d%d", &num1, &num2 ); /* read two integers */
if ( num1 == num2 ) {
printf( "%d is equal to %d\n", num1, num2 );
} /* end if */
if ( num1 != num2 ) {
printf( "%d is not equal to %d\n", num1, num2 );
} /* end if */
if ( num1 < num2 ) {
printf( "%d is less than %d\n", num1, num2 );
} /* end if */
if ( num1 > num2 ) {
printf( "%d is greater than %d\n", num1, num2 );
} /* end if */
if ( num1 <= num2 ) {
printf( "%d is less than or equal to %d\n", num1, num2 );
} /* end if */
if ( num1 >= num2 ) {
printf( "%d is greater than or equal to %d\n", num1, num2 );
} /* end if */
system("pause");
return 0; /* indicate that program ended successfully */
} /* end function main */
Output:
0 komentar:
Posting Komentar