So , i am given a list of inputs:
hello
123456
124
5223
food
7
what i must do is , look at the every single element of the list, and check if the cross sum is equal to 7 (for example: 124 = 1 + 2 + 4 = 7, meaning , the number is valid) So the output , we get , must be:(in console it should looks like this:)
hello
hello is invalid
123456
123456 is invalid
124
124 is valid
5223
5223 is invalid
food
food is invalid
7
7 is valid
Here's the program i wrote :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 99
int cross_sum(int length ,char *name){
int sum = 0;
for(int i = 0; i < length ; i++){
if(name[i] != 0){
sum = sum +( name[i] -48);
}
}
return sum;
}
int check_crosssum( int number){
if( number % 10 == 7)
{
return 0;
}
return -1;
}
int main (void) {
char name[MAXN] ;
int sum = 0;
while (scanf("%98[^\n]", name) == 1) {
int len = strlen(name);
sum = cross_sum(len,name);
int pruf_summe = check_crosssum(sum);
if( pruf_summe == 0 && len <= 20 ){
printf("%s is valid \n" , name);
}
else{
printf("%s is invalid \n" , name);
}
}
return 0;
}
when i run the program, all it does it looks at the first element then ends it. So the output, which i am getting is:
hello
hello is invalid
----------
(program exited with code: 0)
How can i process each inputs individually with help of while loop? Or is there any other way, where programms runs till no input is given! i also tried with
while(1)
it did't seem to work. Any idea , or suggestions would be great!