+ 1

... 👇

Write a program to enter two characters (1 and 0) from the user. Store the characters as integers. Enter one character using the getchar() function and the other using the getc() function. Find the sum of 1 and 0 and store the result in another interger called 'sum'. Now display both the ASCII value and the corresponding character of the variable 'sum' using the printf function. Write down your observations.

3rd Nov 2021, 2:45 PM
Giggling_ddl.
9 Answers
+ 2
Inside the second printf function call, I printed: i)Sum=%d,sum Here, the sum of the two variables stored in the "sum" variable is printed ii) Corresponding character=%c,(char)sum Here, I'm printing a character and that is the variable sum casted to a character. If sum is 65 for example, then the ASCII character of 65 is 'A'. Like this, the ASCII character of the sum value is printed. Understood?
4th Nov 2021, 7:07 AM
Rishi
Rishi - avatar
+ 1
Ans:- #include <stdio.h> int main() { int c,c2; printf("Enter two characters\n"); c=getc(stdin); c2=getchar(); int sum=c+c2; printf("%d+%d"=c,c2); putchar(sum); return 0; } Is this one correct? Actually I'm not getting the output of this programming!
3rd Nov 2021, 2:47 PM
Giggling_ddl.
+ 1
Swagatika Pradhan you need to print both the ASCII value of the sum and the sum itself, so do like this ........... printf("ASCII:%d\nSum:%d",(char)sum, sum); .......... Try this and tell me if this worked
3rd Nov 2021, 3:07 PM
Rishi
Rishi - avatar
+ 1
No! It's showing error 😕🙁
4th Nov 2021, 5:26 AM
Giggling_ddl.
+ 1
Oops I made a little mistake. Here is the right code completed #include <stdio.h> int main() { int c, c2; printf("Enter two characters\n"); c = getc(stdin); c2 = getchar(); int sum = c + c2; printf("Sum=%d\nCorresponding character=%c",sum,(char)sum); return 0; }
4th Nov 2021, 5:56 AM
Rishi
Rishi - avatar
+ 1
can you please explain me from the printf statement!!🙏
4th Nov 2021, 6:15 AM
Giggling_ddl.
+ 1
Yaa done!
8th Nov 2021, 6:16 AM
Giggling_ddl.
+ 1
#include <stdio.h> int main() { int a,b,sum; printf("Please type in one character:\n"); a = getc(stdin); b = getchar(); /* You should enter the two characters without pressing enter*/ sum= a+b; printf("The character you just entered is: %c\n", sum); printf("The sum you just entered is: %d\n", sum); return 0; } (It can be done,in this way;too)
8th Nov 2021, 6:17 AM
Giggling_ddl.
- 1
Кто знает, как модули добавлять?
5th Nov 2021, 8:34 AM
Spy
Spy - avatar