Chapter 1 > Introduction to C Programming Language
Assignments based on basic computations
1. Write a program in C to calculate the simple interest. Input:
P=Principal amount. R=Rate of interest. T= Time (in years)
Calculate: Simple Interest = P*R*T/100
2,. Write a program in C to convert temperature from Celsius to Fahrenheit Input:
Temperature in Celsius (C). Calculate: F=9/5*C+32
3. Write a program in C to find area and perimeter of a rectangle. Input: Length
and breadth. Calculate:
Area = length x breadth Perimeter = 2 x (length + breadth)
4. Write a program in C to convert days into years, weeks, and days. Input: Total
number of days. Calculate:
Number of years = days / 365, Number of weeks = (days % 365)/ 7, Remaining
days = (days % 365) % 7
5. Write a program in C to find the volume of a sphere. Input: Radius (r).
Calculate: volume =4/3* π*r3, use π=3.1416
6. Write a program in C to convert minutes into hours and minutes. Input: Total
minutes Calculate: hours = minutes/60; remaining minutes = minutes%60
7. Write a program in C to add two times taken in hours, minutes, and second
format (T1= {hour1, minute1, second 1}, T2 = {hour2, minute1, second2}).
Input: Two times (T1, T2). Calculate: hours = minutes/60; remaining minutes =
minutes%60, remaining seconds = seconds %60
8. Write a program in C to find distance travelled Input: Speed (km/h) and time
(hours). Calculate: Distance = speed x time
9. Write a program in C to calculate Body Mass Index (BMI). Input: Weight (kg)
and height (m). Calculate: BMI = weight / (height x height)
10. Write a program in C to calculate force. Input: Mass (kg) and acceleration
(m/s?) Calculate: Force = mass x acceleration
11. Write a program in C to calculate kinetic energy. Input: Mass (kg) and velocity
(m/s). Calculate: KE =0.5 x mass x velocity
12. Write a program in C to calculate potential energy. Input: Mass (kg) and
height (m). Calculate: PE = mass x g x height (use g=9.8)
13. Write a program in C to calculate work done. Input: Force and displacement.
Calculate: Work = force x displacement
14. Write a program in C to convert speed from m/s to km/h. Input: Speed in
m/s. Calculate: Speed in km/h = speed x 3.6
15. Write a program in C to calculate momentum. Input: Mass (kg) and velocity
(m/s). Calculate: Momentum = mass x velocity
16. Write a program in C to find frequency from time period. Input: Time period
(T). Calculate: Frequency = 1/T
17. Write a program in C to calculate wavelength. Input: Velocity and frequency.
Calculate: Wavelength = velocity / frequency
18. Write a program in C to calculate current using Ohm's Law. Input: Voltage
and resistance. Calculate: Current = voltage / resistance
19. Write a program in C to calculate power in an electrical circuit. Input: Voltage
and current Calculate: Power = voltage x current
20. Write a program in C to find final velocity using first equation of motion Input:
Initial velocity (u), acceleration (a), and time (t). Calculate: v=u + a*t
21. Write a program in C to find distance travelled using second equation of
motion Input: Initial velocity (u), acceleration (a), and time (t). Calculate:
s=u*t+0.5*a*t
22. Write a program in C to find final velocity using third equation of motion
Input: Initial velocity (u), acceleration (a), and displacement (s) Calculate: v2
=u2+2*a*s; then √𝑢2 + 2 ∗ 𝑎 ∗ 𝑠
23. Write a program in C to find impulse. Input: Mass (m) and change in velocity
(v- u). Calculate: Impulse = m x (v - u)
24. Write a program in C to find average velocity. Input: Initial velocity (u) and
final velocity (v). Calculate: Average velocity = (u + v)/2
25. Write a program in C to calculate acceleration Input: Change in velocity (v -
u) and time (t). Calculate: a=(v-u)/t
26. Write a program in C to find time taken to reach a certain velocity. Input:
Initial velocity (u), final velocity (v), and acceleration (a). Calculate: t=(v-u)/a
27. Write a program in C to find initial velocity.
Input: Final velocity (v), acceleration (a), and time (t). Calculate: u=v-a*t
28. Write a program in C to find distance travelled at constant speed, Input:
Speed (v) and time (t). Calculate: Distance=v x t
29. Write a program in C to calculate gravitational force using Newton's law of
gravitation. Input: Masses m1, m2 and distance (r). Calculate: F= G*m1*m2/r2 use
G= 6.674*10-11
Solutions:
1. Simple Interest
#include <stdio.h>
int main() {
float P, R, T, SI;
printf("Enter Principal, Rate, Time: ");
scanf("%f%f%f", &P, &R, &T);
SI = (P * R * T) / 100;
printf("Simple Interest = %.2f", SI);
return 0;
}
2. Celsius to Fahrenheit
#include <stdio.h>
int main() {
float C, F;
printf("Enter temperature in Celsius: ");
scanf("%f", &C);
F = (9.0/5.0)*C + 32;
printf("Temperature in Fahrenheit: %.2f", F);
return 0;
}
3. Area and Perimeter of Rectangle
#include <stdio.h>
int main() {
float l, b, area, peri;
printf("Enter length and breadth: ");
scanf("%f%f", &l, &b);
area = l * b;
peri = 2 * (l + b);
printf("Area = %.2f, Perimeter = %.2f", area, peri);
return 0;
}
4. Days to Years, Weeks, Days
#include <stdio.h>
int main() {
int days, years, weeks, rem_days;
printf("Enter total days: ");
scanf("%d", &days);
years = days / 365;
weeks = (days % 365) / 7;
rem_days = (days % 365) % 7;
printf("%d Years, %d Weeks, %d Days", years, weeks, rem_days);
return 0;
}
5. Volume of Sphere
#include <stdio.h>
#define PI 3.1416
int main() {
float r, volume;
printf("Enter radius: ");
scanf("%f", &r);
volume = (4.0/3.0) * PI * r * r * r;
printf("Volume = %.2f", volume);
return 0;
}
6. Minutes to Hours and Minutes
#include <stdio.h>
int main() {
int minutes, hrs, mins;
printf("Enter total minutes: ");
scanf("%d", &minutes);
hrs = minutes / 60;
mins = minutes % 60;
printf("%d Hours and %d Minutes", hrs, mins);
return 0;
}
7. Add Two Times (hh:mm:ss)
#include <stdio.h>
int main() {
int h1, m1, s1, h2, m2, s2;
int h, m, s;
printf("Enter time 1 (hh mm ss): ");
scanf("%d%d%d", &h1, &m1, &s1);
printf("Enter time 2 (hh mm ss): ");
scanf("%d%d%d", &h2, &m2, &s2);
s = s1 + s2;
m = m1 + m2 + s / 60;
h = h1 + h2 + m / 60;
s %= 60;
m %= 60;
printf("Sum = %02d:%02d:%02d", h, m, s);
return 0;
}
8. Distance Travelled
#include <stdio.h>
int main() {
float speed, time, distance;
printf("Enter speed and time: ");
scanf("%f%f", &speed, &time);
distance = speed * time;
printf("Distance = %.2f km", distance);
return 0;
}
9. BMI Calculation
#include <stdio.h>
int main() {
float weight, height, bmi;
printf("Enter weight(kg) and height(m): ");
scanf("%f%f", &weight, &height);
bmi = weight / (height * height);
printf("BMI = %.2f", bmi);
return 0;
}
10. Force = mass × acceleration
#include <stdio.h>
int main() {
float m, a, force;
printf("Enter mass and acceleration: ");
scanf("%f%f", &m, &a);
force = m * a;
printf("Force = %.2f N", force);
return 0;
}
11. Kinetic Energy
#include <stdio.h>
int main() {
float m, v, ke;
printf("Enter mass and velocity: ");
scanf("%f%f", &m, &v);
ke = 0.5 * m * v * v;
printf("Kinetic Energy = %.2f J", ke);
return 0;
}
12. Potential Energy
#include <stdio.h>
int main() {
float m, h, pe;
const float g = 9.8;
printf("Enter mass and height: ");
scanf("%f%f", &m, &h);
pe = m * g * h;
printf("Potential Energy = %.2f J", pe);
return 0;
}
13. Work Done
#include <stdio.h>
int main() {
float force, disp, work;
printf("Enter force and displacement: ");
scanf("%f%f", &force, &disp);
work = force * disp;
printf("Work done = %.2f J", work);
return 0;
}
14. m/s to km/h
#include <stdio.h>
int main() {
float ms, kmh;
printf("Enter speed (m/s): ");
scanf("%f", &ms);
kmh = ms * 3.6;
printf("Speed in km/h = %.2f", kmh);
return 0;
}
15. Momentum
#include <stdio.h>
int main() {
float m, v, momentum;
printf("Enter mass and velocity: ");
scanf("%f%f", &m, &v);
momentum = m * v;
printf("Momentum = %.2f kg·m/s", momentum);
return 0;
}
16. Frequency = 1 / Time
#include <stdio.h>
int main() {
float T, f;
printf("Enter time period: ");
scanf("%f", &T);
f = 1 / T;
printf("Frequency = %.2f Hz", f);
return 0;
}
17. Wavelength = velocity / frequency
#include <stdio.h>
int main() {
float v, f, wl;
printf("Enter velocity and frequency: ");
scanf("%f%f", &v, &f);
wl = v / f;
printf("Wavelength = %.2f m", wl);
return 0;
}
18. Ohm's Law: I = V / R
#include <stdio.h>
int main() {
float V, R, I;
printf("Enter voltage and resistance: ");
scanf("%f%f", &V, &R);
I = V / R;
printf("Current = %.2f A", I);
return 0;
}
19. Electrical Power
#include <stdio.h>
int main() {
float V, I, P;
printf("Enter voltage and current: ");
scanf("%f%f", &V, &I);
P = V * I;
printf("Power = %.2f W", P);
return 0;
}
20. First Equation of Motion: v = u + at
#include <stdio.h>
int main() {
float u, a, t, v;
printf("Enter u, a, t: ");
scanf("%f%f%f", &u, &a, &t);
v = u + a * t;
printf("Final velocity = %.2f", v);
return 0;
}