Chapter 2 - Let Us C
If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.
#include <stdio.h>
int main(){
int n,rev = 0;
printf("Enter a 5 digit number : ");
scanf("%d",&n);
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
rev = (rev*10)+(n%10);
n = n/10;
printf("The reversed number is %d",rev);
return 0;
}
Step 1: Declare variables `n` for input and `rev` for reversed number.
Step 2: Take user input for 5-digit number.
Step 3: Extract each digit using modulo (%) and division (/).
Step 4: Build reversed number by multiplying by 10 and adding each digit.
Step 5: Print the reversed number.
Step 2: Take user input for 5-digit number.
Step 3: Extract each digit using modulo (%) and division (/).
Step 4: Build reversed number by multiplying by 10 and adding each digit.
Step 5: Print the reversed number.
Write a program to receive Cartesian co-ordinates (x, y) of a point and convert them into polar co-ordinates (r, ф).
#include <stdio.h>
int main(){
int x,y;
printf("Enter the x-coordinate : ");
scanf("%d",&x);
printf("Enter the y-coordinate : ");
scanf("%d",&y);
printf("The polar form of (%d,%d) is (%f,%f)",x,y,sqrt((x*x)+(y*y)),atan2(y,x));
return 0;
}
Step 1: Declare variables for x and y coordinates.
Step 2: Take user input for x and y coordinates.
Step 3: Calculate r using formula: √(x² + y²).
Step 4: Calculate θ using formula: arctan(y/x).
Step 5: Print the polar coordinates (r,θ).
Step 2: Take user input for x and y coordinates.
Step 3: Calculate r using formula: √(x² + y²).
Step 4: Calculate θ using formula: arctan(y/x).
Step 5: Print the polar coordinates (r,θ).
Write a program to input the 3 sides of triangle and print its area
#include <stdio.h>
#include <math.h>
int main(){
float a, b, c, s;
printf("Enter the first side of the triangle : ");
scanf("%f", &a);
printf("Enter the second side of the triangle : ");
scanf("%f", &b);
printf("Enter the third side of the triangle : ");
scanf("%f", &c);
s = (a + b + c) / 2;
printf("Area of the triangle is %f", sqrt(s * (s - a) * (s - b) * (s - c)));
}
Step 1: Declare variables for sides (a,b,c) and semi-perimeter (s).
Step 2: Take user input for three sides.
Step 3: Calculate semi-perimeter: s = (a + b + c)/2.
Step 4: Calculate area using Heron's formula: √(s(s-a)(s-b)(s-c)).
Step 5: Print the calculated area.
Step 2: Take user input for three sides.
Step 3: Calculate semi-perimeter: s = (a + b + c)/2.
Step 4: Calculate area using Heron's formula: √(s(s-a)(s-b)(s-c)).
Step 5: Print the calculated area.
Write a program to receive values of latitude (L1, L2) and longitude (G1, G2), in degrees, of two places on the earth and output the distance (D) between them in nautical miles.
#include <stdio.h>
#include <math.h>
int main(){
float l1,l2,g1,g2,d;
print("Enter the values of latitude in degrees : ");
scanf("%f %f",&l1,&l2);
print("Enter the values of longitude in degrees : ");
scanf("%f %f",&g1,&g2);
l1 = l1*3.14/180;
l2 = l2*3.14/180;
g1 = g1*3.14/180;
g2 = g2*3.14/180;
d = 3963*acos((sin(l1)*sin(l2)) + (cos(l1)*cos(l2)*cos(g2-g1)));
printf("The distance between the two places is %f nautical miles",d);
return 0;
}
Step 1: Declare variables for latitudes (l1,l2) and longitudes (g1,g2).
Step 2: Take user input for coordinates in degrees.
Step 3: Convert degrees to radians (multiply by π/180).
Step 4: Calculate distance using great circle formula.
Step 5: Print distance in nautical miles.
Step 2: Take user input for coordinates in degrees.
Step 3: Convert degrees to radians (multiply by π/180).
Step 4: Calculate distance using great circle formula.
Step 5: Print distance in nautical miles.
Wind-chill factor is the felt air temperature on exposed skin due to wind. The wind-chill temperature is always lower than the air temperature.
#include <stdio.h>
#include <math.h>
int main(){
float t,v,wcf;
printf("Enter the speed of the wind in km/h : ");
scanf("%f",v);
printf("Enter the temperature in celcius : ");
scanf("%f",t);
wcf = 35.74+(0.6215*t)+((0.4275*t-35.75)*pow(v,0.16));
printf("The wind chill factor is %f",wcf);
return 0;
}
Step 1: Declare variables for temperature (t), wind speed (v), and wind chill factor (wcf).
Step 2: Take user input for wind speed and temperature.
Step 3: Calculate wind chill using formula:
35.74 + (0.6215t) + (0.4275t - 35.75)v0.16.
Step 4: Print the calculated wind chill factor.
Step 2: Take user input for wind speed and temperature.
Step 3: Calculate wind chill using formula:
35.74 + (0.6215t) + (0.4275t - 35.75)v0.16.
Step 4: Print the calculated wind chill factor.
Write a program to take 10 floating inputs and print their mean, variance and standard deviation
#include <stdio.h>
#include <math.h>
int main(){
float rad;
printf("Enter the angle in radians : ");
scanf("%f",&rad);
printf("sin(%f) = %f\n",rad,sin(rad));
printf("cos(%f) = %f\n",rad,cos(rad));
printf("tan(%f) = %f\n",rad,tan(rad));
printf("cot(%f) = %f\n",rad,1/tan(rad));
printf("cosec(%f) = %f\n",rad,1/sin(rad));
printf("sec(%f) = %f",rad,1/cos(rad));
return 0;
}
Step 1: Declare variable `rad` for angle in radians.
Step 2: Take user input for angle.
Step 3: Calculate basic trigonometric functions (sin, cos, tan).
Step 4: Calculate derived functions (cot, cosec, sec).
Step 5: Print all trigonometric values.
Step 2: Take user input for angle.
Step 3: Calculate basic trigonometric functions (sin, cos, tan).
Step 4: Calculate derived functions (cot, cosec, sec).
Step 5: Print all trigonometric values.
Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.
#include <stdio.h>
int main(){
int C,D;
printf("Enter the value of C : ");
scanf("%d",&C);
printf("Enter the value of D : ");
scanf("%d",&D);
C = C+D;
D = C-D;
C = C-D;
printf("After Swapping the values\nC = %d\nD = %d",C,D);
return 0;
}
Step 1: Declare variables C and D.
Step 2: Take user input for both variables.
Step 3: Swap values without using third variable:
• C = C + D (store sum in C)
• D = C - D (get original C value in D)
• C = C - D (get original D value in C)
Step 4: Print swapped values.
Step 2: Take user input for both variables.
Step 3: Swap values without using third variable:
• C = C + D (store sum in C)
• D = C - D (get original C value in D)
• C = C - D (get original D value in C)
Step 4: Print swapped values.
Comments