Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is what I have right now, however it is not working correctly. I don\'t kno

ID: 3812753 • Letter: T

Question

This is what I have right now, however it is not working correctly. I don't know how to get it to work with the "continually prompt the user to enter a positive integer until EOF has been entered via the keyboard."

Programming Exercise 3: Non-recursive (Iterative) Persistence of a Number (10 points) Multiplying the digits of an integer and continuing the process gives the surprising result that the of always arrives at a digit number. For example, 715 35 15 27 14 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 number of 715 is 3, the persistence number of 27 is 2, the persistence number of 4000 is 1, and the persistence number 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 call a persistence function. The result from calling persistence should be displayed to the user.

Explanation / Answer

Hi Code seems to be working fine when I use do while instead of while loop. Please change the program as follows.

Regarding EOF pressing from keyboard, it should be ^z or ^c (control-z or control-c). I have used Microsoft visual c++ and control-c worked for me. Here EOF is nothing but -1 (I have printed it on console). Please try

// CHegg2.cpp : Defines the entry point for the console application.
//

#include<stdio.h>
#include "stdafx.h"
#include<iostream>
using namespace std;

int persistence(int number);

int main(int argc, char *argv[])
{
int number;
do
{
printf("Enter a positive number: ");
scanf("%d", &number);
       if(number!=EOF)
printf("Persistence number is %d. ", persistence(number));
}while(number != EOF);
return 0;
}

int persistence(int number)
{
int n, r, count = 0, p;
n = number;
while(n > 9)
{
p = 1;
do
{
r = n % 10;
p = p * r;
n = n / 10;
} while (n > 0);

count++;
n = p;
printf("%d ", p);
}
return count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote