You are on page 1of 7

Computer Fundamentals-I

Roll No._________________

Exp 2: Page 1 of 7

Computer Fundamentals-I

Laboratory Session-2
Use of scanf(), if-else and switch statement
In this Lab Session you will learn how to use scanf() function for formatted input from keyboard. You will also learn the structure of if-else and switch statements. You will be graded on the basis of the exercise programs given at the end. Following program inputs two integers from the user and prints their sum. /* Addition program */ #include <stdio.h> int main() { int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable to store sum */ printf( "Enter first integer: " ); scanf( "%d", &integer1 ); printf( "Enter second integer: " ); scanf( "%d", &integer2 ); sum = integer1 + integer2; /* prompt */ /* read integer */ /* prompt */ /*read integer */

/* assign total to sum */ /* print sum */

printf( "Sum is %d.\n", sum ); return 0; }

Compile and run the program. Program will first ask for the first integer. Type an integer and press ENTER key. It will ask for the second integer. Type another integer and press ENTER key. Program will then print the sum of the two integers entered. A sample output of the program is: Enter first integer: 12 Enter first integer: 45 Sum is 57.

} else { /* Number entered was odd */ printf( "You have entered an odd number. /* prompt */ scanf( "%d". /* read integer */ if( number%2 == 0 ) { /* Number entered was even */ printf( "You have entered an even number. } return 0. . &number ).\n" ).Computer Fundamentals-I Exp 3: Page 2 of 7 To input variables of different data types using scanf() function. following conversion specifications are used: Data types long double double float unsigned long int long int unsigned int int short char scanf conversion specifications %lf %lf %f %lu %ld %u %d %hd %c Following example shows how to make decisions using if-else statement: /* This program takes an integer from the user and tells whether the number is even or odd */ #include <stdio.\n" ). } A sample output of the program is: Enter an integer: 12 You have entered an even integer. /* number to be input by user */ printf( "Enter an integer: " ).h> int main() { int number.

\n" ). } .\n" ). } else { /* Number entered was odd */ printf( "Number entered is +ve odd.Computer Fundamentals-I Exp 3: Page 3 of 7 Following program shows how we can use nested-if statements in our program: #include <stdio. } else { /* Number entered was odd */ printf( "Number entered is -ve odd.h> int main() { int number.\n" ).\n" ). } return 0. } } else if( number < 0 ) { /* Number is -ve */ if( number%2 == 0 ) { /* Number entered was even */ printf( "Number entered is -ve even. &number ). /* number to be input by user */ printf( "Enter an integer: " ).\n" ). /* prompt */ scanf( "%d". /* read integer */ if( number > 0 ) { /* Number is +ve */ if( number%2 == 0 ) { /* Number entered was even */ printf( "Number entered is +ve even. } } else { /* Number is zero */ printf( "Number entered is zero.

printf(“Enter Second Number: “). scanf( "%d". num2). printf(“Enter First Number: “). . num3). num3. &num3 ). scanf( "%d". printf(“Enter Third Number: “). if( num1 > num2 && num1 > num3 ) { printf(“First Number %d is largest”. This program calculates the largest of the three integers entered by the user: #include <stdio. &num2 ). } A typical output of the program is: Enter First integer: 11 Enter Second integer: 12 Enter Third integer: 14 Third Number 14 is largest. &num1 ).Computer Fundamentals-I A sample output of the program is: Enter an integer: -11 Number entered is -ve odd. num2. } if( num2 > num1 && num2 > num3 ) { printf(“Second Number %d is largest”. num1).h> int main() { int num1. } if( num3 > num1 && num3 > num2 ) { printf(“Third Number %d is largest”. } return 0. Exp 3: Page 4 of 7 Following program shows how we can use multiple conditions in an if statement. scanf( "%d".

scanf("%d". int choice. /* READ first operand */ printf("Please enter the second operand: "). #include <stdio. break.h> int main(void) { int num1=0. /* two numbers to work on */ /* result of calculation */ printf("Please enter the first operand: "). int result = 0. scanf("%d". printf("1 = add\n\t"). printf("2 = substract\n\t"). num2=0. &choice). printf("Denominator cannot be zero. break. printf("3 = multiply\n\t"). case 3: result = num1 * num2. printf("4 = divide\n").\n").Computer Fundamentals-I Exp 3: Page 5 of 7 Following program shows how to make a simple calculator using switch statement. &num2). /* READ calculation type */ switch ( choice ) { case 1: result = num1 + num2.num2. case 4: if( num2 == 0 ) { result = 0. &num1). case 2: result = num1 . printf("\n\nPleas make your selection now:\n\t"). scanf("%d". break. } else . /* READ second operand */ printf("\n\nWhat would you like to do?\n\n\t").

default: printf("Invalid option selected\n").34 ). This variable represents the marks of a student.Computer Fundamentals-I { result = num1 / num2.h> #include <stdio. 3) Consider the following code: #include <math. } printf( "Result is %d. printf(“Power is %f. A typical output of the program should be similar to this: Enter Number 1: 52 Enter Number 2: 20 Number 1 = 20 Number 2 = 52 2) Write a program which inputs an integer from user and stores it in a variable named marks. Program should tell the grade of the student according to following table: 91-100 A+ 81-90 A 71-80 B 61-70 C 51-60 D 0-60 F A typical output of the program should be similar to this: Enter marks: 52 Your grade is D. power ). break. double power = pow( 1. } .2.34 ). result). return 0. square_root ). } break.”. } Exp 3: Page 6 of 7 Exercises 1) Write a program that prompts the user to enter two integers and then swaps the contents of these two integer variables.”.h> int main() { double sqare_root = sqrt( 2.\n". return 0. printf(“Square root is %f. 3.

4 added options are to calculate power. Use num1 as parameters. tan(double). pow( double a. abs(double).Computer Fundamentals-I Exp 3: Page 7 of 7 Compile and run the program. Also take num1 and num2 as double in your program. cos(double) and sqrt(double) takes only one parameter. cosine and square root. sine. sin(double). . Rewrite the calculator program so that it gives 8 options. sqrt(double) is used to calculate the square root. Other useful functions in math. double b ) is used to calculate ab.h are sin(double). cos(double).