Lab 1 (C Programming) Solution
Feb 15, 2019programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
1) Write a program to display “Hello World” in C.
Source Code:
#include<stdio.h>
int main(){
printf("Hello World");
return 0;
}
Output:
Hello World
2) Write a program to add two numbers (5 and 7) and display its sum.
Source Code:
#include<stdio.h>
int main()
{
int x = 5, y = 7, sum;
sum = x + y;
printf("SUM = %d", sum);
return 0;
}
Output:
SUM = 12
3) Write a program to multiply two numbers (30 and 40) and display its products
Source Code:
#include<stdio.h>
int main()
{
int x = 30, y = 40, product;
product = x * y;
printf("PRODUCT = %d", product);
return 0;
}
Output:
PRODUCT = 1200
4) Write a program to calculate the total surface area of the sphere having its radius (r = 10 cm) and display the total surface area.
Source Code:
#include<stdio.h>
#define PI 3.1416
int main()
{
float area, radius = 10;
area = 4 * PI * radius * radius;
printf("Total surface area = %f sq. cm", area);
return 0;
}
Output:
Total surface area = 1256.640015 sq. cm
5) Write a program to calculate the simple interest of the given principle = 5000, rate = 5.5, time = 3.
Source Code:
#include<stdio.h>
int main()
{
float principle = 5000, rate = 5.5, time = 3, simple_interest;
simple_interest = (principle * time * rate) / 100;
printf("Simple Interest = %f", simple_interest);
return 0;
}
Output:
Simple Interest = 825.000000
6) Write a program to find the discounted price of shoes with tag price = 5000, discount = 15 percent.
Source Code:
#include<stdio.h>
int main()
{
float tag_price = 5000, discount = 15, discounted_price;
discounted_price = tag_price - tag_price * discount / 100;
printf("Discounted Price = %f", discounted_price);
return 0;
}
Output:
Discounted Price = 4250.000000
7) Write a program to find the area of triangle having sides 13cm, 12 cm and 9cm.(Use math.h library to find the square root i.e. double sqrt(double num)
Source Code:
#include<stdio.h>
#include<math.h>
int main()
{
float side1 = 13, side2 = 12, side3 = 9, area, sp;
sp = (side1 + side2 + side3)/2;
area = sqrt(sp * (sp - side1) * (sp - side2) * (sp - side3));
printf("Area of Triangle = %f", area);
return 0;
}
Output:
Area of Triangle = 52.153618