Header Ads Widget

WAP in C to take two numbers from the user without using scanf and print their sum

 Code:-

 here we use getch() for taking input of numbers.  As we know getch() take a character from the user .

But we can add only numbers which are less than 10. because getch() hold the screen untill we press a key so it can take only a character from the user.

#include<stdio.h>
int main()
{
char ch;
int a,b;
clrscr();
printf("Enter first number \n");
// taking first character
ch=getch();
a=ch;
a=a-48; // 48 is the ascii of 0
printf("Enter second number\n");
// taking second character
ch=getch();
b=ch;
b=b-48; // 48 is ascii of 0
printf("Sum of %d and %d is %d",a,b,a+b);
getch();
return 0;
}


Output:-

Enter first number
Enter second number
sum of 2 and 4 is 6


Video Solution:-




Post a Comment

0 Comments