c - Loop iterates as many time as the number of characters in input -


i know title weird. didn't know how put in words.

basically, have loop asks user if want run program again, "would run again (y/n). if enter 1 character loop 1 time. if enter 5 characters "hello" iterate 5 times.

ex. run again? (y/n)

q

would run again? (y/n)

hello

would run again? (y/n)

would run again? (y/n)

would run again? (y/n)

would run again? (y/n)

would run again? (y/n)

this code:

 char a='y';    while(a=='y' || a=='y')    {    printf("enter p value: \n");    scanf("%d",&p);    printf("enter q value: \n");    scanf(" %d",&q);    printf("enter k value: \n");     scanf(" %d",&k);     (i=p; i<q+1; i++)    {       q=i;       sum=0;       count=0;       while(q>0)       {          count++;          r = q%10;          sum = sum + pow(r,k);          q = q/10;       }        if ( == sum && i>1 && count==k )       {          printf("%d\n",i);        }       i++;    }    a='z';    while(a !='y' && !='y' && a!='n' && a!='n' )    {       printf("would run again? (y/n) \n");       scanf(" %c", &a);    }  

this because, scanf takes input arguments buffer. every valid argument
(one matches format) scanf performs job, if encounters invalid
argument(that not match format) leaves(skips) argument in buffer.

now, in code giving "hello" input , using format specifier "%c"
scanf interprets input 'h', 'e', 'l', 'l', 'o', since have used "%c" thus
reads input character character , since 5 characters valid
character input output gets printed 5 characters.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -