Program: Iterative Persistence Please help complete program below in C language.
ID: 3597166 • Letter: P
Question
Program: Iterative Persistence
Please help complete program below in C language.
Multiplying the digits of an integer and continuing this process gives the surprising result that the sequence of products always arrives at a single digit number. For example, 27 14-4 4000 0 The number of times products need to be calculated to reach a single digit is called the persistence number of that integer. Thus, the persistence of 715 is 3, the persistence of 27 is 2, the persistence of 4000 is 1, and the persistence of 9 is 0. You are to write a program that will continually prompt the user to enter a positive integer until EOF has been entered via the keyboard. For each number entered, your program should output the persistence of the numberExplanation / Answer
1 #include<stdio.h>
2 #include<string.h>
3 int main(int argc,char** argv)
4 {
5 int n,m,count;
6 char a[100];
7 char *p;
8 for(;;)
9 {
10 printf("ENTER A NUMBER OR EOF");
11 fgets(a,100,stdin);
12 if(sscanf(a,"%d",&n)==1)
13 {
14 count=0;
15 do
16 {
17 m=1;
18 do
19 {
20 m=m*(n%10);
21 n=n/10;
22 }while(n>0);
23 n=m;
23 n=m;
24 ++count;
25 }while(m>=10);
26 printf("the persistant is %d ",count);
27 }
28 else
29 {
30 if(strcmp(a,"EOF")==0)
31 break;
32 }
33 }
34 return 0;
35 }
36
output:
ENTER A NUMBER OR EOF715
the persistant is 3
ENTER A NUMBER OR EOF27
the persistant is 2
ENTER A NUMBER OR EOF4000
the persistant is 1
ENTER A NUMBER OR EOF
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.