Tuesday, 29 January 2019

Operators&Expresiions(part_4)

A) Write  A C program to convert Centigrade to Fahrenheit and vice versa.
B) Using ternary operator( conditional), write a C program to find the largest of three numbers.

Answer(A):
Convert Fahrenheit to Centigrade

#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
clrscr();
printf("\n Enter Temp in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit-32) / 1.8;
printf("\n Temperature in Celsius : %.2f ", celsius);
getch();
}
Convert Centigrade to Fahrenheit
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
clrscr();
printf("\n Enter Temp in Celsius : ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("\n Temperature in Fahrenheit : %.2f ", fahrenheit);
getch();
}
Answer(B):
Normal program without using conditional operator ypu can write in this way.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b,c;
printf(“Enter the three number”);
scanf(“%d %d %d”,&a,&b,&c);
if(a>b && a>c)
{
printf(“ a is greatest”);
}
Else if(b>a && b>c)
{
printf(“b is greatest”);
}
Else
{
printf(“c is greatest”);
}
getch();
}
But by conditional operator you have to advantage line of code will be less lets see,
#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b,c,d;
printf(“Enter the value of a,b,c”);
scanf(“%d %d %d”, &a &b &c  ):
d=a>b? (a>c? a:c) : (b>c?b:c);
print(“Greatest number is %d”, d);
getch();

}

No comments:

Post a Comment

Basic Electronics Engineering (EC101)

Basic Electronics Engineering (EC101)