2 DataTypes FormattingStrings IO
2 DataTypes FormattingStrings IO
SA=2lh+2lb+2bh
V=l*b*h
Note: Have all parameters as integers.
3. Write a ‘C’ program to find the simple interest and Compound interest for the Amount
(P), Rate of Interest (R) and Number of years (N)
Note:
4. Write a C program that will read a number from the user that represents the radius (r) of a
sphere as input and outputs the sphere’s diameter (2r), circumference (2πr), surface area
(4πr2), and volume . Define a symbolic constant PI with value 3.1416
5. Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result)
(3.31 x 10-8 x 2.01 x 10-7) / (7.16 x 10-6 + 2.01 x 10-8)
6. Mass of Earth is (5.97 × 1024) kg and mass of Moon is (7.35 × 1022) kg. Write a program
to calculate the total mass of the two? (remember to use exponential format to display
the result approximate to 2 decimal places)
7. Write a program to read a character from the user. Print the character and its
corresponding ASCII on the screen.
8. Write a program to get a number in decimal number system. Print the value and its
corresponding octal and hexadecimal number using different format specifiers.
Note: format specifiers
%d - to print value in integer format
%o - to print value in octal format
%x - to print value in hexadecimal format (letters will print in lowercase)
%X - to print value in hexadecimal format (letters will print in uppercase)
9. The following program will demonstrate the working of different escape sequences like
new line, tab space, inverted commas etc in printf statements. Type the code and note
down the outputs.
#include <stdio.h>
int main()
{
printf("Good\nMorning!"); //use of \n
printf("\nGood\tMorning!"); // use of \t
printf("\n\"Good Morning!\""); //use of \"
printf("\nGood\bMorning!"); //use of \b
printf("\nGood\rMorning!"); //use of \r
return 0;
}
10. We can get formatted values through scanf() in c, this example will demonstrate you to
read formatted day in DD/MM/YYYY format using scanf().
Generally, we cannot read anything between the integer values using scanf(), for
example - if we want to provide 3 values together either we have to give space or
enter to separate them.
But, scanf() also provide some characters that can be placed between the format
specifiers, which will separate the given input matching with the same characters
and store the values in corresponding variables.
For example: If we want to enter a date (day, month and year) without using any
kind of scanf() formatting, we need to enter values like this:
#include <stdio.h>
int main()
{
int day,month,year;
printf("Enter date (in DD/MM/YYYY) ");
scanf("%02d/%02d/%04d",&day,&month,&year);
printf("Entered time is %02d/%02d/%04d\n",day, month, year);
return 0;
}
Execute the above code with following inputs and note down the outputs
i. 12/12/2001
ii. 1/2/2022