Cse 122
Cse 122
Objectives:
Theory:
#include <stdio.h>
int main ()
{/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" ); }
else {
}
printf("value of a is : %d\n", a);
You can set up an if-else statement to test for multiple conditions. The following example uses two
conditions so that if the first test fails, we want to perform a second test before deciding what to do:
if (x%2==0)
{
printf(“x is an even number”);
}
else
{
if (x>10)
{
printf(“x is an odd number and greater than 10”);
}
else
{
printf(“x is an odd number and less than 10”);
}
}
This brings up the other if-else construct, the if, else if, else. This construct is useful where
two or more alternatives are available for selection.
The syntax is
if(condition)
statement 1;
else if (condition)
statement 2;
.....................
.....................
else if(condition)
statement n-1;
else
statemens n ;
The various conditions are evaluated one by one starting from top to bottom, on reaching a
condition evaluating to true the statement group associated with it are executed and skip other
statements. If none of expression is evaluate to true, then the statement or group of statement
associated with the final else is executed.
Code 1: Write a program that will read two numbers and it will find the largest number between them
#include <stdio.h>
#include<conio.h>
main ()
{
int a,b;
printf("Enter an integer: \n");
scanf("%d%d", &a,&b);
{
if (a>b)
printf("The a=%d is bigger!\n",a);
else
printf ("b=%d is bigger ",b);
}
}
Code 2: Write a code in C to check if a number is even or odd
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Exercise 2:
1. Write a program that will read three numbers and it will find the largest number among them.
2. Write Algorithm of the program
3. Draw the flowchart of the program