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

I am only given one hour to code this. [NOTE : NO arrays, pointers, recursion an

ID: 3887661 • Letter: I

Question



I am only given one hour to code this.
[NOTE : NO arrays, pointers, recursion and strings should be used in this code.]
1) How do I go about doing this is under an hour? Please include some thought processes.
2) What's the best way to savage the maximum amount of marks if I don't complete this in under an hour?

Exercise 2: Happy Number 50 marks] Problem Statement For a positive integer S, if we sum up the squares of all the digits in S, we get another (possibly different) integer S1. If again, we sum up the squares of all the digits in S, we get yet another integer S2. We can repeat this process as many times as we want to get more integers. It has been proven that the integers generated in this way always eventually reach one of the 10 numbers 0, 1, 4, 16, 20, 37, 42, 58, 89, or 145. Particularly, a positive integer S is said to be happy if one of the integers generated this way is 1. For example, starting with 7 gives the sequence 17,49, 97, 130, 10, 1l, so 7 is a happy number. In this exercise, you are to write a program to compute and compare the number of happy numbers in two given ranges. For example, given two ranges [1, 10] and [2, 11], your program should be able to compute that there are 3 happy numbers (1, 7 and 10) in the first range and 2 (7 and 10) in the second. It should also be able to tell that there are more happy numbers in the first range than the second. Your program should read in four integers, which represent the lower bounds and upper bounds of the two ranges (both inclusive), compute the numbers of happy numbers in each range, and then print messages stating the numbers of happy numbers as well as which range has more happy numbers. You may assume that the input is valid (i.e., the integers are all positive and the lower bounds are no bigger than the upper bounds). Write on the skeleton file happy.c given to you. You need to include one function: * int computeHappyNumbers(int lower, int upper) which takes in two integers lower and upper, and returns the number of happy numbers in the range [lower, upper] You may define additional functions as needed. Check sample runs for input and output format.

Explanation / Answer

1.) If u want to do this in an hour, first think about how to achieve the logic for happy number with single integer. After achieving this you can improve the logic for a range then u can improve the achieved logic to second range as well.

2.) I feel the good way is atleast achieving of whether given number is happy number or not. Better way is to find the number of happy numbers in given range and finally the best way is to achieve the logic for given question.

Please understand the below logic for more info :

#include<stdio.h>

#include <stdbool.h>

int digitSquareSum(int n) {

int squareSum = 0;

while (n){

squareSum += (n % 10) * (n % 10);

n /= 10;

}

return squareSum;

}

// method return true if n is Happy number

bool isHappynumber(int n) {

int slow, fast;

// initialize slow and fast by n

slow = fast = n;

do

{

// move slow number by one iteration

slow = digitSquareSum(slow);

// move fast number by two iteration

fast = digitSquareSum(digitSquareSum(fast));

}

while (slow != fast);

// if both number meet at 1, then return true

return (slow == 1);

}

// main code to test above methods

int main() {

int range1_1, range1_2, range2_1, range2_2;

printf("Enter 1st range: ");

scanf("%d %d",&range1_1,&range1_2);

printf(" Enter 2st range: ");

scanf("%d %d",&range2_1,&range2_2);

int range1count = 0;

int range2count = 0;

int firstLimit = 0;

int secondLimit = 0;

for(int j=0;j<2;j++){

if(j==0){

firstLimit = range1_1;

secondLimit = range1_2;

}else {

firstLimit = range2_1;

secondLimit = range2_2;

}

for(int i=firstLimit;i<=secondLimit;i++){

int n = i;

if (isHappynumber(n)){

if(j==0){

range1count = range1count + 1;

}else{

range2count = range2count + 1;

}

}

}   

}

printf(" The numbers of happy numbers in the two ranges are: %d %d",range1count,range2count);

if(range1count==range2count){

printf(" The numbers of happy numbers in the two ranges are the same.");

}else if(range1count>range2count){

printf(" There are more happy numbers in the first range");

}else {

printf(" There are more happy numbers in the second range");

}

}

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