Sum of 2 numbers with 9 digits
Hi! I'm fairly new to C. I wrote a simple c program, a calculator. But when I tried to add 123456789+123456789 it prints the answer as 246913584. I'm not sure what went wrong. Could someone help me?
Written in Notepad++ compiled in gcc
Code:
#include<stdio.h>
main()
{
float n1,n2,sum,sub,div,prod;
char sym,choice;
system("cls");
printf("This is a calculator:\n");
printf("\nEnter the expression:\n\n");
cal:
scanf("%f%c%f",&n1,&sym,&n2);
sum=n1+n2;
sub=n1-n2;
prod=n1*n2;
div=n1/n2;
if(sym=='+')
{
printf("\nSum = %.2f\n\n",sum);
goto cal;
}
if(sym=='-')
{
printf("\nDifference = %.2f\n\n",sub);
goto cal;
}
if(sym=='*')
{
printf("\nProduct = %.2f\n\n",prod);
goto cal;
}
if(sym=='/')
{
printf("\nQuotient = %.2f\n\n",div);
goto cal;
}
if(sym=='?')
{
getch();
}
}
https://www.dropbox.com/s/z38tq1ylbz7qlpd/calc.png