This is a program to find the sum of the digits of a number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> void main() { using namespace std; int num, rem, sum=0; //Declaring variables cout<<"Enter a number :"<<endl; cin>>num; //Loop to calculate the sum of the digits of the given number. while(num!=0) { rem=num%10; num=num/10; sum=sum+rem; } cout<<"Sum of the digits is "<<sum<<endl; cin.get(); } |

Program tested in Visual C++

The following two tabs change content below.

#### Vlad

#### Latest posts by Vlad (see all)

- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013

Hi:

I have a homework problem using floating decimal and I’m just learning C++, this is my second class and I feel lost. I will paste the question and my code. Any help you can give me will be appreciated.

Thank You!

Lucy likes to jog in the morning.

As she jogs, she counts the number of strides, she makes during the first minute and then again during the last minute of her jogging.

She then averages these two numbers together and calls this the average the number of strides she makes during a minute.

Write a program that asks for the number of strides made during the first minute and the number of strides made during the last minute (each an integer) and then asks for the total number of hours and minutes that Lucy jogs. (each an integer)

Assume Lucy’s stride length is 2.5 feet. Using the fact that there are 5280 feet in a mile, have your program display the average number of strides Lucy makes in a minute (a float) and the distance Lucy jogged in miles (a float)

Below is a sample run….your code should work for any input I choose .

What was the number of strides you made during the first minute?

128

What was the number of strides you made during the last minute?

123

How many hours did you jog?

1

How many minutes did you jog?

15

Your average number of strides per minute is: 125.5

The distance you jogged was: 4.46 miles

HINT:

The magic code to print a float to 2 places is :

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

MY CODE:

#include

using namespace std;

int main()

{

int number_of_feet, feet_per_minutes, feet_per_miles, miles_hour;

float numMinutes, numMiles

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

cout <> numFeet;

cout <> numFeet;

cout <> “numAvfeet;

cout<> numHours;

cout <> numMinutes;

cout << “How many jogged miles per hour:";

cout<< numMiles / numHours << '\n';

return 0;

}

Hey, i am working on the program, will post it as soon as possible.

@sundayy

There you go buddy, I’ve made the program.

http://www.mycoding.net/?p=199

But i did not use your hint or code.

Let me know how if this ones good.

how can i write a program in c++ that do this:

1. Asks the user to enter two numbers.

2. Prints their:

a) Sum

b) Product

c) Difference

d) Average

e) Remainder

i need help please !!

This really has helped me out alot and to further on this program would anyone know how to get the multiple of the sum of the digits? If anyone has any tips or hints that would be highly helpful also with command line arguments as well within this same kind of program?