Reading User Input Into Variables
1. Which of the following statements is/are true.
a. printf format specifier for double is %lf
Ans: False. double is printed using %f format specifier.
b. We can read a double and an int in a single scanf
Ans: True. scanf(“%lf %d”, a_double, an_int);
c. We can declare a double and an int in a single statement.
Ans: False. Refer Q3 in Variable and Data Type chapter
d. %d is the format specifier for int for printf as well as scanf
Ans: True.
2. Consider this statement from above example.
fahrenheit = ((celsius * 9)/5) + 32;
Would following statement generate same output?
fahrenheit = (celsius * (9/5)) + 32;
Ans: No. Refer explanation of Q6 in Opertors chapter.
3. Write a program that reads height in inches and converts it into centimetres.
#include <stdio.h> int main (int argc, char * argv[]) { double inches = 0; printf("Enter length in inches: "); scanf("%lf", &inches); printf("%f inches = %f cms\n", inches, inches*2.54); return 0; }
4. Write a program that reads three integers and prints their sum.
#include <stdio.h> int main (int argc, char * argv[]) { int num1 = 0, num2 = 0, num3 = 0; printf("Enter 1st number: "); scanf("%d", &num1); printf("Enter 2nd number: "); scanf("%d", &num2); printf("Enter 3rd number: "); scanf("%d", &num3); printf("%d + %d + %d = %d\n", num1, num2, num3, num1+num2+num3); return 0; }
5. Write a program that reads length and breadth of a rectangle and prints its area and perimeter.
#include <stdio.h> int main (int argc, char * argv[]) { int length = 0, breadth = 0; printf("Enter length of rectangle: "); scanf("%d", &length); printf("Enter breadth of rectangle: "); scanf("%d", &breadth); printf("Area of rectangle = %d\n", length*breadth); printf("Perimeter of rectangle = %d\n", 2*(length+breadth)); return 0; }
6. Write a program that reads radius of a circle and prints its area and circumference.
#include <stdio.h> int main (int argc, char * argv[]) { int radius = 0; double pi = 22.0/7; double circumference = 0, area = 0; printf("Enter radius of circle "); scanf("%d", &radius); circumference = 2 * pi * radius; area = pi * radius * radius; printf("Circle of radius %d has circumference %f and area %f\n", radius, circumference, area); return 0; }
7. Write a program that reads in two angles of a triangle and prints the third angle.
#include <stdio.h> int main (int argc, char * argv[]) { int angle1 = 0, angle2 = 0; printf("Enter first angle "); scanf("%d", &angle1); printf("Enter second angle "); scanf("%d", &angle2); printf("Three angles of triangle are %d %d and %d", angle1, angle2, (180-angle1-angle2)); return 0; }
8. Write a program that reads in principal amount, interest rate, and tenure of a deposit and computes simple and compound interest.
For computing compound interest, we have to compute power of a number. To compute power, we use a math library function pow. pow(x,y) gives us value x raised to power y.
#include <stdio.h> #include <math.h> /* We need this to use pow */ int main (int argc, char * argv[]) { double principal = 0, time = 0, rate = 0, simple = 0, compound = 0, temp = 0; printf("Enter principal amount "); scanf("%lf", &principal); printf("Enter duration of deposit "); scanf("%lf", &time); printf("Enter interest rate percent "); scanf("%lf", &rate); simple = (principal * rate * time)/100; temp = pow(1+(rate/100), time); /* pow(x,y) evaluates to x raised to power y */ compound = (principal * temp) - principal; printf("Simple interest = %f\nCompound interest = %f\n", simple, compound); return 0; }
9. Write a program to convert number of days into years, weeks, and days. E.g. If user inputs 2000 days then program should print 5 years 25 weeks. Assume 365 days a year.
#include <stdio.h> int main (int argc, char * argv[]) { int days_in = 0, years = 0, weeks = 0, days; printf("Enter number of days "); scanf("%d",&days_in); years = days_in/365; days = days_in%365; weeks = days/7; days = days%7; printf("%d days is %d years %d weeks and %d days\n", days_in, years, weeks, days); return 0; }