Question

C program--write a program to find whether the given number of three digits is an integer...

C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371.
Output:

Enter a number, or * to quit : 432

4**3 + 3**3 + 2**3 is not = 432
Enter a number, or * to quit : 371

3**3 + 7**3 + 1**3 is = 371
Enter a number, or * to quit : *

Please modify my code but not using atoi...

#include <stdio.h>
int main()
{
    int number, oriNumber, rem, result = 0;

    printf("Enter a number, or * to quit: ");
    scanf("%d", &number);

    oriNumber = number;

    while (oriNumber != 0)
    {
        rem = oriNumber%10;
        result += rem*rem*rem;
        oriNumber /= 10;
    }

    if(result == number)
        printf("%d**3 + %d**3 + %d**3 is not = %d ",number);
    else
        printf("%d**3 + %d**3 + %d**3 is = %d.",number);

    return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main() {
    int number, oriNumber, rem, result = 0, d1, d2, d3;

    printf("Enter a number, or * to quit: ");
    while (scanf("%d", &number) == 1) {
        oriNumber = number;
        result = 0;
        while (oriNumber != 0) {
            rem = oriNumber % 10;
            result += rem * rem * rem;
            oriNumber /= 10;
        }

        oriNumber = number;
        d1 = oriNumber / 100;
        oriNumber %= 100;
        d2 = oriNumber / 10;
        oriNumber %= 10;
        d3 = oriNumber;

        if (result == number)
            printf("%d**3 + %d**3 + %d**3 is = %d\n", d1, d2, d3, number);
        else
            printf("%d**3 + %d**3 + %d**3 is not = %d\n", d1, d2, d3, number);
        printf("Enter a number, or * to quit: ");
    }
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C program--write a program to find whether the given number of three digits is an integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong...

    Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

  • In the Source Folder (src) of this project create a new C source file called "gcd.c"....

    In the Source Folder (src) of this project create a new C source file called "gcd.c". Once again, copy the contents of the "main.c" file above into the "gcd.c" source file. Modify this program to NOT ask the user to input the second Positive Integer, if the user has entered a program terminating value for the "first" input Postitive Integer. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { //============================== setbuf(stdout, NULL); // turns standard output buffering off int...

  • Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int...

    Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

  • Write a C program which calculates how many digits a number has and prints each digit...

    Write a C program which calculates how many digits a number has and prints each digit in reserve order with space in between(no space after the last digit). The number > 0 and has no more than 5 digits. (optional] It is better to write two functions. One is designed for printing number in reverse order and the other is for counting the number of digits. Given that the value of number (which is an int variable) is 12345, the...

  • Question:Write a C program to input an integer k. Compute 10^k and store the result in...

    Question:Write a C program to input an integer k. Compute 10^k and store the result in a double variable. Display the result on the standard output using printf. Implementation ° The program is named lab4c . c. Use the given template lab4c .c (BELOW ) and fill in your code. ° Assume that the input integer k is small enough so that 10^k can be stored in a double variable named my_double. ° Display on the standard output the result...

  • Write a program that asks the user to type an even number or 111 to stop....

    Write a program that asks the user to type an even number or 111 to stop. When the user types an even number, display the message “Great Work”, and then ask for input again. When the user types an odd number, display the message “Try again” and ask the user to enter again. When the user enters the sentinel value of 111 the program ends I've attempted this but it went horribly wrong. we are only suppose to use while,...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...

    Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Draw the flowchart and write a Java code to find ALL Armstrong number in the range of 0 and 999. You should write your code in two ways: (Yes as if you are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT