Write a C program to compare two ages using ternary operator.

 TASK-

Write a c program to find the younger age between two ages. First enter the ages and then find the younger age print by the program using the ternary operator.



TERNARY OPERATOR:

Its same as if -- else condition e.g 

BY IF---ELSE;;

if (x>y)
z=x;
else
z=y;

BY TERNARY OPERATOR:

z=x>y ? x : y; //here we can write whole condition in one line.

CODE:

#include<stdio.h>

int main(){

int friend1,friend2,age;

printf("Enter age of friend1 ");//Asking age of 1st friend

scanf("%d",&friend1); //taking input

printf("\nEnter age of friend2 ");//Asking age of 2nd friend

scanf("%d",&friend2); //taking input

age= (friend1<friend2)? friend1: friend2; //Condition using ternary operators

printf("\younger age is %d",age);

getchar();

return 0;

}

Expected Output:




NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

1 Comments

Post a Comment
Previous Post Next Post