473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Displaying sum of digits without loops

Ok so here is my issue. I need to display the sum of digits in a year without using loops. For example if the input is 1986 then the sum is 24.

I know that with loops I would do this:

int YearSum(0);
int x(1);
for (x = 1; x <= 4; ++x)
{
YearSum = YearSum + Year % 10;
Year = Year / 10;
}

But what about without loops? I've tried Year % 10 + Year / 10 which gets me 204... I'm lost without loops...
Oct 4 '11 #1
4 8075
Rabbit
12,516 Expert Mod 8TB
To get the first digit, it would be the year integer division by 1000. The second digit would be the year integer division by 100 mod 10. And so forth.
Oct 4 '11 #2
weaknessforcats
9,208 Expert Mod 8TB
OK. Set Result to year. Result is 1966.
Now Calculate Thousands = Result / 1000 gets the 1. Your first dight.
Now calculate Result = Result - Thousands*1000
which is 1966 - 1000 leaves you 966.
Now calculate Hundreds = Result / 100 gets the 9. Your second digit
Now calculate Result = Result - Hundreds *100
etc...

You could also << to a stringstream and then >> to a string. Now you would have string[3] == '1' so the int value would be string[3] - 48 to get from ASCII to decimal. string[0] would be '9', etc...

There's probably a recursive function solution where each recursion returns one of the values. You would recurse down until you reached 4 levels and then return summing the values as you come up.

You might also ask someone else for the answer and just write it down. No loop there. :)
Oct 4 '11 #3
donbock
2,426 Expert 2GB
I'm aware of three ways to implement repetitive actions such as this: iteration (looping); recursion; and tediously sequential (loop unrolling). All of these have been mentioned already.
Oct 4 '11 #4
whodgson
542 512MB
weaknessforcats last suggestion is possibly the most effective solution but you could also do this:
Expand|Select|Wrap|Line Numbers
  1. int a,b,c,d,sum;
  2. cout<<"\nenter the year (with spaces)\n";
  3. cin>>a>>b>>c>>d;
  4. cout<<"the year was "<<a<<b<<c<<d;
  5. sum=a+b+c+d;
  6. cout<<"\nthe sum of year digits: "<<sum<<endl;
Oct 6 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steven | last post by:
Hi there, I have a list of values that I am displaying in a table (Not using a loop) I want to be able to put a graphic next to the item with the hightest price. I have all of the prices in...
3
by: ofer h | last post by:
hello all i have got a span inside a td of a table. i am trying to clip the string that exceeds the length of the td and then put three dots ("...") after it so it will show that the text was...
3
by: SQL | last post by:
The trick is to use a pivot table Check out the code here http://sqlservercode.blogspot.com/2005/09/fast-date-ranges-without-loops-in-sql.html
22
by: Saurabh Saxena | last post by:
can we write the program to write no 1 to n without using switch,do,while,for,goto,if and conditional operator where n will be input by user.
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
2
by: p.mc | last post by:
Hi all, I'm using asp.net to display data from a SQL Server database, I've managed to display the data using dataGrids but now need to have more control over the display of the data. I'd like...
4
by: jkuruvil | last post by:
I need to print a report of a bunch of records that falls between certain dates. Some records have dates and some don't. I did a UNION query to look for everything that falls between these two dates...
8
by: ranjitkumar | last post by:
Hi, If any one know the answer for the following questions Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100
7
by: abhinuke | last post by:
Been brushing up my C,C++ for my new venture in Graduate Studies for this fall.I am doing basic programs in which I am trying this one right now. A 5-digit positive integer is entered through the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.