Write a statement for each of the following: Print 1234 right-justified in a 10
ID: 3694828 • Letter: W
Question
Write a statement for each of the following: Print 1234 right-justified in a 10 digit field. Print 123.456789 in exponential notation with a sign (+ or minus) and 3 digits of precision. Read a double value into variable number. Print 100 in octal form preceded by 0. Read a string into character array string. Show what is printed by each of the following statements. If a statement is incorrect, indicate why. printf ("%-10 d , 10000); printf ("%c ", 'This is a string"); printf ("% ld %+ld ", 1000000, 1000000); printf ("% 10.2E ", 444.93738); printf ("%d ", 10.987);Explanation / Answer
4.
a) printing of right justified 1234 of ten digit field.
example program for right and left justification
// Right and left justifying integer printing
#include <stdio.h>
int main(void)
{
printf("Right justifying and left justifying values. ");
printf(" Compare the output with the source code. ");
printf("-------------------------------------------- ");
printf("%10s%10d%10c%10f ", "hello", 7, 'a', 1.23);
printf("%-10s%-10d%-10c%-10f ", "hello", 7, 'a', 1.23);
return 0;
}
by executing this sample program one can understand the left and right justifications .
b)
printing of 1234.56789 in exponential form with +/- and 3 digits of precision.
printf("%E",1234.56789);
out put is - 1.234568E+0.2. in exponential form. we can add + symbol before E in formate specifier to get it or the value should be preceded with - to get it.
and to get 3 precisions in the output
printf("%10.3E",123.456789);
out put is - 1.235E+0.2 will get.this means 10.2 means two precisions 10.3 means 3 precisions we wil get.
c) if we read double value into integer varible the value after precision will be truncated because range of the integer value is less than the range of the doble value
ex; 1234.567890 if we try to print this value we will get only 1234 .
d)
printing of octal number of 100 into octal form preceded by 0 is
int i=0;
printf("%d%d",i,144); because octal number of 100 is 144 base 8.
e)
read a string into character arry
char name[15];
scanf("%s",name);
it will take a name without blank spaces.it we enter space it will be truncated.
or
char name[15];
gets(name);
it wiil read a name with blank spaces.
5.
a)
this will yield the out put as 10000 but the position of the cursor will be moved forward or afterward printing the output for some specific spaces as we indicate.
b)
string is trying to print with the character formate specifier %c so we will get unexptected characters as result because string should be used %s as a format specifier obly.
out put may ! or any different character.
c)
for the first %ld 1000000 is goign to be printed for the second it is preceded by + as %+ld so + symbol wil be preceded with out put as +1000000.
other symbols like -,* etc wont work .
d)
out put is 4.45E+0.2 because we indicate to print two precisions after decimal so the following output we got.
e)
out put is unpredicted or garbage value will be printed because by using integer formate specifier we are tyring to pring a floating constant value so is the out put.
all these out puts may vary compiler to compiler
thank u
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.