+ 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.
9 odpowiedzi
+ 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?
+ 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!
+ 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
+ 1
No! It's showing error 😕🙁
+ 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;
}
+ 1
can you please explain me from the printf statement!!🙏
+ 1
Yaa done!
+ 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)
- 1
Кто знает, как модули добавлять?