Unit 2 - Fundamentals of C
2.1 - Write a C Program to convert temperature from Fahrenheit to Celsius and vice versa. [S-10, W-16, S-18, W-19]
#include <stdio.h>
int main()
{
float fh,cl;
int choice;
printf("\n1: Convert temperature from Fahrenheit to Celsius.");
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);
if(choice ==1)
{
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2)
{
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else
{
printf("\nInvalid Choice !!!");
}
return 0;
}
2.2 - Develop an application program to convert and print distance between two cities in meters, feet, inches & centimeters. The distance between the two cities (In KM) is input through keyboard. [W-17]
#include<stdio.h>
int main()
{
float km, m, cm, feet, inch;
printf("Enter distance in kilometers: ");
scanf("%f",&km);
m = 1000 * km;
cm = 1000 * 100 * km;
feet = 3280.84 * km;
inch = 39370.08 * km;
printf("The distance in Feet: %f\n", feet);
printf("The distance in Inches: %f\n", inch);
printf("The distance in Meters: %f\n", m);
printf("The distance in Centimeters: %f\n", cm);
return 0;
}
2.3 - Write a program that reads two numbers from the keyboard and gives their addition, subtraction, multiplication, division and modulo. [W-17]
#include<stdio.h>
int main()
{
int a,b;
printf("Enter First Number : ");
scanf("%d",&a);
printf("\nEnter Second Number : ");
scanf("%d",&b);
printf("\nAddition of %d & %d is : %d", a, b, a+b);
printf( "\nSubtraction of %d & %d is : %d", a, b, a-b);
printf("\nMultiplication of %d & %d is : %d", a, b, a*b);
printf("\nDivision of %d & %d is : %d", a, b, a/b);
printf("\nModulo of %d & %d is : %d", a, b, a%b);
return 0;
}
2.4 - Write a program to find out an area of a circle. [S-17]
#include<stdio.h> int main() { float r,area; printf("Enter the radius : "); scanf("%f",&r); area = 3.14*r*r; printf("Area of a circle : %.2f",area); return 0; }
2.5 - Write a program to find area of triangle. [S-20]
#include <stdio.h>
int main()
{
float base, hight, area;
printf("Enter base of the triangle : ");
scanf("%f", &base);
printf("Enter hight of the triangle : ");
scanf("%f", &hight);
area=(base*hight)/2;
printf("Area of the triangle = %.2f", area);
return 0;
}
2.6 - Write code to find out largest of 2 numbers using ternary operator. [S-17]
#include <stdio.h>
int main()
{
int a, b, num;
printf("Enter 2 numbers : \n");
scanf("%d %d", &a, &b);
(a > b) ? (num = a) : (num = b);
printf("Largest of %d and %d is %d\n", a, b, num);
return 0;
}
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know