The sum calculation is fine, however, for product the numbers used creates a answer larger then 10 integers, which gives an incorrect answer. I understand that its because I am using int. But when I tried float, that also didn't work.
Can someone explain how to make my product answer able to printout answers larger then 10 integers.
For example, the expected output of 6,20,4,16,15,11,6,3,19, 18 is 7800883200 but my system printsout 72076004000
Thank you
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
if(argc<2){
printf("Invalid Input: filename value missing\n");
}
else{
char filename[50];
strcpy(filename,argv[1]);
FILE* fptr = fopen(filename,"r");
if(fptr==NULL){
printf("File not found!\n");
}
else{
int arr[10];
int i;
int val;
for(i=0;i<10;i++){
fscanf(fptr,"%d",&val);
arr[i] = val;
}
int sum = 0;
for(i=0;i<10;i++){
sum= sum + arr[i];
}
int product = 1;
for(i=0;i<10;i++){
product= product * arr[i];
}
printf("Sum: %d\n",sum);
printf("Product: %d\n",product);
fclose(fptr);
}
}
return 0;
}