Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

K. J.

Somaiya College of Engineering,


Mumbai-77 (A Constituent College of Somaiya
Vidyavihar University)

Batch: F3 Roll No.: 16010321124

Experiment / assignment / tutorial No. 2

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date


TITLE: Write a program to accept 3 numbers from the user and find the largest of the
3 numbers using
If - else if-else
Ternary operator

AIM: Write a program to accept 3 numbers from the user and find the largest of the 3
numbers using
If - else if-else
Ternary operator
______________________________________________________________________
Expected OUTCOME of Experiment:

_____________________________________________________________________
Books/ Journals/ Websites referred:

1. Programming in ANSI C, E. Balagurusamy, 7 th Edition, 2016, McGraw-Hill


Education, India.
2. Structured Programming Approach, Pradeep Dey and Manas Ghosh, 1 st Edition,
2016, Oxford University Press, India.
3. Let Us C, Yashwant Kanetkar, 15th Edition, 2016, BPB Publications, India.

_____________________________________________________________________
Problem Definition:

Ask user to input three numbers. Compare three numbers to find the largest of them
using
1. Nested if else statement
2. Using ternary operator
Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering,


Mumbai-77 (A Constituent College of Somaiya Vidyavihar University)
Algorithm:

Using if-else-if:

Department of Science and Humanities


Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)

Using ternary operator:

Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)


K. J. Somaiya College of Engineering,
Mumbai-77 (A Constituent College of Somaiya
Vidyavihar University)

Implementation details:

Using if-else-if:

SOURCE CODE

#include <stdio.h>

int main()
{
int A, B, C; //declaring input variables

printf("Enter the value of first number"); //taking input for 1st


number scanf("%d", &A);

printf("Enter the value of second number"); //taking input for 2nd


number scanf("%d", &B);

printf("Enter the value of third number"); //taking input for 3rd


number scanf("%d", &C);

if(A>B)
{
if(A>C)
{
printf("Largest Number is %d",A);
}

else
{
printf("Largest Number is %d",C);
}
}

else
{
if(B>C)
{
printf("Largest Number is %d",B);
}

else
{
printf("Largest Number is %d",C);
}
}

return(0);

Department of Science and Humanities


Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering,


Mumbai-77 (A Constituent College of Somaiya
Vidyavihar University)

Using ternary operator:

SOURCE CODE

#include <stdio.h>

int main()
{
int n1, n2, n3; //declaring input variables

printf("Enter the value of first number"); //taking input for base


1 scanf("%d", &n1);

printf("Enter the value of second number"); //taking input for base


2 scanf("%d", &n2);

printf("Enter the value of third number");


scanf("%d", &n3);

printf("Largest number is %d", n4);

return(0);
}
Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering,


Mumbai-77 (A Constituent College of Somaiya
Vidyavihar University)

Output(s):

Using If-else-if:
Using ternary operator:

Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering,


Mumbai-77 (A Constituent College of Somaiya
Vidyavihar University)

Conclusion:

In this experiment, the use of ternary operators and if-else-if statement was learnt.
Ternary operators are a shorter and more efficient method of implementing decision
making in many cases as compared to if-else-if statement. They also have a shorter
syntax.

Post Lab Descriptive Questions

1. Explain bitwise operators with examples


2. Write a code snippet to perform left shifting of bits by some positions 3.
Write associative rules and precedence table of various operators 4. What
are different storage class specifiers in C?

A1. Bitwise Operators are used for manipulating data at the bit level, also called bit level
programming. Bitwise operates on one or more bit patterns or binary numerals at the level of
their individual bits.

A2. Given below is a snippet to shift bits by 2 positions:

int a = 5;
a<<=2;

print(“Shifted number is %d” a);

Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)

A3. The table below specifies the associativity of operators in C:


Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)

K. J. Somaiya College of Engineering,


Mumbai-77 (A Constituent College of Somaiya
Vidyavihar University)

A4. The different storage class specifiers are as follows:


- Auto
- Extern
- Register
- Static

Date: _____________ Signature of faculty in-charge

Department of Science and Humanities

Page No PIC Sem I 2021-22(Odd Sem)

You might also like