Write a C program that calculates and displays the sum of all fourdigit numbers in the line. The only include to use is <stdio.h>. The only keywords:void, int, char, while,if, else, and return. The only library functions:printf(), getchar().The only predefined constant:EOF.
Input output
3153\n 3153
00020005\n 7
999578585 error
456 5443 error
6574y435 error
`Hey,
Note: In case of any queries, just comment in box I would be very happy to assist all your queries
#include <stdio.h>
int main()
{
char ch[1000];
scanf("%s",ch);
int i=0;
while(ch[i]!='\0')
{
if(ch[i]<'0'||ch[i]>'9')
{
printf("error\n");
return 0;
}
i++;
}
if(i%4!=0)
{
printf("error\n");
return 0;
}
int sum=0,ct=i;
for(i=0;i<ct;i=i+4)
{
sum=sum+(ch[i]-'0')*1000+(ch[i+1]-'0')*100+(ch[i+2]-'0')*10+(ch[i+3]-'0');
}
printf("%d\n",sum);
return 0;
}
Kindly revert for any queries
Thanks.
Get Answers For Free
Most questions answered within 1 hours.