You are on page 1of 2

9. Find out the output of following c program.

#include<stdio.h>
void main () {
int x = 10;
x += (x++) +(++x) +x;
printf ("%d", x);
}
Answer: 46
Reason: Let a be x++, b be ++x, c be x
Now initially x = 10.
x++ gives the current value of x and increments at the end
so, a = 10.
Now x = 11 (Since x is incremented).
++x gives the incremented value of x so b = 12
Now x = 12 (Due to incrementation).
since x is 12 c is 12
Now a+b+c is 10+12+12 which gives 34.
considering this x+=a+b+c
on expanding,
x = x + (a+b+c).
Now since a+b+c is 34 and x = 12
x = x + 34
Therefore x = 12 + 34 which gives 46
13.Write a c program to read a four-digit number and print in words.

#include<stdio.h>
void main (){
    int digits [4] = {0}, num;
    char ones [][20] = {"one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"};
    char _11_to_19[][20] = {"ten", "eleven",
"twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nin
eteen"};
    char tens [][20] = {"twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
    printf ("Enter a four-digit number:");
    scanf ("%d", &num);
    for (int i = 0; num > 0; i++, num /= 10)
        digits[3-i] = num % 10;
    if(digits[0] != 0)
        printf ("%s thousand ", ones[digits[0] - 1]);//Printing thousands part
    if(digits[1] != 0)
        printf ("%s hundred ", ones[digits[1] - 1]);//Printing hundreds part
    if((digits[0] || digits[1]) && (digits[2] || digits[3]))
        printf ("and ");
    if(digits[2] != 1){                        
        if(digits[2] != 0)
            printf("%s ", tens[digits[2] - 2]);//Printing tens part
        if(digits[3] != 0)
            printf ("%s", ones[digits[3] - 1]);//Printing one’s part
    }
    else
        printf ("%s ", _11_to_19[digits[3]]);/*Printing the numbers fall under
11-19*/
}

You might also like