Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I was asked to create a program that will get numbers from the user.
The numbers should always be positive and disregards the negative sign.
The numbers will then be converted to digits.
I need to print it in the right order as it looked like the number entered by the user.
I should also get the sum of the digits and tell if it is divisible by 3.
If the input of the user is 0 the program should end.

So here's my code:
C++
#include <stdio.h>
#include <stdlib.h>
int main () {
	int input, individual, reverse=0; // declarations
	
do {	// loop infinitley except when input is 0
    printf ("Please Enter a number: "); // Statement to enter a number
	scanf ("%d", &amp;input); // scan input
    
	if (input!=0) { // to check if input is 0 or not if yes end program
	 int sum=0, i=1; // initial value of sum and i as counter
	 
     printf ("The Digits are "); // print the introductory text
    
     	while (input!=0) { // check if input is 0 or not
     	input = abs (input); // numbers should be positive so i use absolute
		individual= input%10; //gets modulo of input  
		sum += individual; // adds the digits
		input/=10; ++i; // divides digits and update counter
 		printf ("%d ", individual); // prints individual digit BUT SHOULD BE IN PROPER ORDER
        }
       
		printf ("\nThe Sum of the Digit(s) is %d\n", sum);// prints the sum
		
		if (sum % 3 == 0) { // checks if sum is divisible by 3
         printf ("This Number is divisible by 3!\n"); // prints
         }
         printf ("\n"); // break line
    }
    	
	else if (input == 0) { // ends the program if input is 0
      return 0;
      system ("pause");
    }
         
} while (1);        // infinite loop
        	
 	system ("pause");
}


Sample run:
Please Enter a number : 123
The Digits are 3 2 1
The sum of the digits is 6
this number is divisible by 3!

Please Enter a Number

What I wanted it to output is :
Please Enter a number : 123
The Digits are 123
The sum of the digits is 6
this number is divisible by 3!

Please Enter a Number
Posted
Updated 7-Nov-15 5:21am
v2
Comments
PIEBALDconsult 7-Nov-15 11:22am    
Did you have a question?

1 solution

First off, you aren't converting them to digits - you are extracting individual digits from it, but you are getting them from the least significant digit position first - so it naturally prints them in the reverse order.

What I would suggest you do is try to do this in stages.
1) Read the input from the user.
2) Extract the digits into individual variables (an array would be a good idea)
3) Print the digits from the array.
3) Sum the digits from the array, and print that
4) Print if the sum divisible by three.

At the moment, you are trying to run when you need to walk first: get the "basic" version right, and then look at making it more efficient.

But this is your homework - so I'll give you no code! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900