Monday, March 27, 2023

 Input and Output Statement

1. Write a program that converts Centigrade to Fahrenheit.

     #include <stdio.h> float temp_f; /* degrees fahrenheit */ float temp_c; /* degrees centigrade */ char line_text[50]; /* a line of input */ int main() { printf("Input a temperature (in Centigrade): "); fgets(line_text, sizeof(line_text), stdin); sscanf(line_text, "%f", &temp_c); temp_f = ((9.0 / 5.0) * temp_c) + 32.0; printf("%f degrees Fahrenheit.\n", temp_f); return(0); }

2. Write a C program that calculates the volume of a sphere. 

#include <stdio.h> float myradius; /* radius of the sphere */ float myvolume; /* volume of the sphere (to be computed) */ char line_text[50]; /* a line from the keyboard */ /* the value of pi to 50 places, from wikipedia */ const float PI = 3.14159265358979323846264338327950288419716939937510; int main() { printf("Input the radius of the sphere : "); fgets(line_text, sizeof(line_text), stdin); sscanf(line_text, "%f", &myradius); myvolume = (4.0 / 3.0) * PI * (myradius * myradius * myradius); /* volumn=(4/3) * pi * r^3*/ printf("The volume of sphere is %f.\n", myvolume); return(0); }

3.Write a program in C that reads a forename, surname and year of birth and displays the names and the year one after another sequentially 

  #include <stdio.h> int main() { char firstname[20], lastname[20]; int bir_year; printf("Input your firstname: "); scanf("%s", firstname); printf("Input your lastname: "); scanf("%s", lastname); printf("Input your year of birth: "); scanf("%d", &bir_year); printf("%s %s %d\n", firstname, lastname, bir_year); return 0; }

4.Write a program in C to calculate the sum of three numbers with input on one line separated by a comma.

 #include <stdio.h> int num1,num2,num3; /* declaration of three variables */ int sum; char line_text[50]; /* line of input from keyboard */ int main() { printf("Input three numbers separated by comma : "); fgets(line_text, sizeof(line_text), stdin); sscanf(line_text, "%d, %d, %d", &num1, &num2, &num3); sum = num1+num2+num3; printf("The sum of three numbers : %d\n", sum); return(0); } 

5.Write a C program to perform addition, subtraction, multiplication and division of two numbers

 #include <stdio.h> int main() { int num1, num2; int sum, sub, mult, mod; float div; /* * Read two numbers from user separated by comma */ printf("Input any two numbers separated by comma : "); scanf("%d,%d", &num1, &num2); /* * Performs all arithmetic operations */ sum = num1 + num2; sub = num1 - num2; mult = num1 * num2; div = (float)num1 / num2; mod = num1 % num2; /* * Prints the result of all arithmetic operations */ printf("The sum of the given numbers : %d\n", sum); printf("The difference of the given numbers : %d\n", sub); printf("The product of the given numbers : %d\n", mult); printf("The quotient of the given numbers : %f\n", div); printf("MODULUS = %d\n", mod); return 0; }