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

Please write out your logic flow, as well as the functions for 1) multiply2Digit

ID: 3886374 • Letter: P

Question



Please write out your logic flow, as well as the functions for 1) multiply2Digit and 2) giveAdvice
You should NOT use arrays, pointers, strings.

You are given a task by a numerologist to write a program, numerology.c, which gives advice to a client based on a number provided by the client. The number num provided is a non-zero integer and it is the only input to your program. To give appropriate advice, we check both the sign (positive or negative) and the absolute value of num. From the absolute value of num, we need to derive a digit dgt To obtain the digit dgt from the (absolute value) of num, we follow a process called multiply2Digit as given below We begin with the given absolute value of num, denoted as n, and multiply all digits in n. This produces a new number n . If n, contains just one digit, then dgt is n · If n' contains more than one digit, then we repeat the multiply2Digit process again but now on n'. We repeat this process until we obtain a number with one digit, and assign that number to dgt · Shown below are two examples on how the digit dgt is obtained from the number num Example1 12935 12935 Example2 8281 8281 Iteration 1: 8 x 2 x 8 x 1 = 128 nuim Absolute value of nun Iteration 1: 1 x 2 x 9 x 3 x 5 = 270 Iteration 2: 1 x2 x8 16 Iteration 3: 1 x 6 6 Multiplying the digits Iteration 2: 2 x 7 x 0 = 0 Digit dgt produced Table 1. Examples for the multiply2Digit process The advice given by your program depends on the digit dgt produced from num and the sign of num (positive or negative), as follows Digit produced Sign of the input Advice to be given number 0,3,7,9You should protect life Positive 2,5,8 Share your wealth, donate generously 1,4,6 Build harmony, bring people together 0,3,7,9Speak honestly Negative 2,5,8 Praise others' successes 1,4,6 Lend your hand to those who are in need Table 2. Advice to be given based on the digit produced

Explanation / Answer

#include <stdio.h>

int multiply2digit(int num){

int rem,n'=1;

while(1){

while(num!=0){ //Loop to find individual digit of number num and multiply them

rem=num%10;

num=num/10;

n'=n'*rem;

}

if(n'/10==0) //check whether ir is single digit or not

break;

else //repeat it until single digit n' is not found

num=n';

return n';

  

}

}

void printAdvice(int num,int sign){

if(sign==1){ //Check condition for positive values

if(num==0||num==3||num==7||num==9)

printf("You should protect life");

else if(num==2||num==5||num==8)

printf("Share your wealth,donate generously");

else

printf("Build harmony,bring people together");

}

else if(sign==0){ //check condition for negative values

if(num==0||num==3||num==7||num==9)

printf("Speak Honestly");

else if(num==2||num==5||num==8)

printf("Praise other's success");

else

printf("Lend your hand in those who need it");

}

  

}

int main() {

int num,sign=0,digit; //Assume sign to be negative initially (0->negative sign, 1 ->positive sign)

printf("Enter a non-zero integer");

scanf("%d",num);

if(num>=0) //Change sign if it is positive

sign=1;

  

digit=multiply2digit(abs(num));

printAdvice(digit,sign);

return 0;

}