# C++ Program to find the sum of the digits

• (2 Pages)
• 1
• 2

## 18 Replies - 14060 Views - Last Post: 03 May 2009 - 01:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=102872&amp;s=ac723bbd806f89104ccfa4795da4bda5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wince

Reputation: 0
• Posts: 46
• Joined: 29-April 09

# C++ Program to find the sum of the digits

Posted 01 May 2009 - 08:59 PM

```#include <iostream.h>
#include <stdlib.h>
void main ()
{
int i , a ,b []; int sum= 0;
cin>>i;
for (int a=0;a<i;a++)
{
cin>> b[a];
sum=sum+ b [a]; }
cout<<sum;
}
```

The Question is :
Write a C++ program to find the sum of the digits of an integer given as input = 6532

Correct Me If i am wrong thanks

This post has been edited by wince: 01 May 2009 - 09:05 PM

Is This A Good Question/Topic? 0

## Replies To: C++ Program to find the sum of the digits

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: C++ Program to find the sum of the digits

Posted 01 May 2009 - 09:00 PM

There's a few things wrong, syntax wise:

you declare 'a' twice
cout is << not >>
you don't declare a size for the array 'b'

### #3 wince

Reputation: 0
• Posts: 46
• Joined: 29-April 09

## Re: C++ Program to find the sum of the digits

Posted 01 May 2009 - 09:04 PM

hello
i don really understand what u mean ...
```you declare 'a' twice
cout is << not >>
```

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: C++ Program to find the sum of the digits

Posted 01 May 2009 - 09:18 PM

Quote

you declare 'a' twice

first time: int i , a ,b [];
second time: for (int a=0;a<i;a++)

Quote

cout is << not >>

This should be self explanatory

Quote

you don't declare a size for the array 'b'

arrays' sizes must be declared at compile time or dynamically allocated with malloc() or new

### #5 wince

Reputation: 0
• Posts: 46
• Joined: 29-April 09

## Re: C++ Program to find the sum of the digits

Posted 01 May 2009 - 09:22 PM

u mean i just need put int i ,a , b ; int sum = 0; thats all ?
no need for ( int a = 0 ; a < i ; a ++)?

correct me if i am wrong .... newbie =)

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: C++ Program to find the sum of the digits

Posted 01 May 2009 - 09:26 PM

What i mean is this:

in C++ (and most every language i know of), you only declare a variable once. You did this in the top line int i ,a , b ;

Thus, when you use 'a' in the for loop, you just need to set its initial value, no need for the identifier again:

```for(a = 0; a < i; a++)//not 'int a', just 'a'
{
}

```

### #7 wince

Reputation: 0
• Posts: 46
• Joined: 29-April 09

## Re: C++ Program to find the sum of the digits

Posted 01 May 2009 - 09:29 PM

```for(a = 0; a < i; a++)//not 'int a', just 'a'
{
}

```

THANKS KYA =) .. GOOD JOB MEN thanks u much .. i am understood

### #8 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 01:05 AM

wince, on 1 May, 2009 - 08:29 PM, said:

```for(a = 0; a < i; a++)//not 'int a', just 'a'
{
}

```

Except this is a deprecated coding style.
Coding a block of variables at the head of the code went out in the 1980s.

Current coding style is to declare variables as close to the first use of the variables as practicable.

So if you want to look like you learned to code (or have updated your coding skills) in the 21st century drop the first declaration and keep this one
```for(int a = 0; a < i; a++) {
}

```

### #9 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 06:34 AM

Unless he's using C.

Which, based off of the iostream use I'm guessing not; just saying

This post has been edited by KYA: 02 May 2009 - 06:34 AM

### #10 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 07:07 AM

janotte, on 2 May, 2009 - 12:05 AM, said:

Except this is a deprecated coding style.
Coding a block of variables at the head of the code went out in the 1980s.

So if you want to look like you learned to code (or have updated your coding skills) in the 21st century drop the first declaration and keep this one

You can't say that!
Poor KYA you've BLASTED him, he's not old, not out of date.

It's the new coding style that should be deprecated.

Look at the state of coders these days.

Oh (reminiscing), I remember when a coder was a
coder ... (indistinct mumbles) ... opcode ...
(indistinct mumbles) ...

### #11 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 07:17 AM

I would have normally recommended to keep the scope as small as possible, but I generally try to work with whatever is presented, running with the notion that the person coded that for a specific reason and/or purpose.

### #12 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 07:22 AM

KYA, on 2 May, 2009 - 05:34 AM, said:

Unless he's using C.

Which, based off of the iostream use I'm guessing not; just saying

True in practice (but not for good reasons).

Certainly the old 'declare at the top' style is commonly used in new C code but moving the declarations close to first use is the most commonly recommended style for new C code nowadays.

The problem is so many of the reference sources were written in the 1980s or written by people stuck in 1980s style ( some teachers tend to be slow to change in such things ) so students/learners follow these old examples and perpetuate this deprecated style.

'Declare at the top' just makes code that extra bit harder to follow and that's never good. Unless your teacher or boss requires it, don't do it.

### #13 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 07:22 AM

Quote

I would have normally recommended to keep the scope as small as possible, but I generally try to work with whatever is presented, running with the notion that the person coded that for a specific reason and/or purpose.

Thats right.

This post has been edited by #define: 02 May 2009 - 07:25 AM

### #14 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 07:24 AM

#define, on 2 May, 2009 - 06:07 AM, said:

janotte, on 2 May, 2009 - 12:05 AM, said:

Except this is a deprecated coding style.
Coding a block of variables at the head of the code went out in the 1980s.

So if you want to look like you learned to code (or have updated your coding skills) in the 21st century drop the first declaration and keep this one

You can't say that!
Poor KYA you've BLASTED him, he's not old, not out of date.

It's the new coding style that should be deprecated.

Look at the state of coders these days.

Oh (reminiscing), I remember when a coder was a
coder ... (indistinct mumbles) ... opcode ...
(indistinct mumbles) ...

Wow if I came across like that then I offer huge apologies.

I was just having a pedantic discussion about coding styles.

If it came across as worse than that then that's my bad!

Apologies for any offence KYA

### #15 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: C++ Program to find the sum of the digits

Posted 02 May 2009 - 07:30 AM

janotte, on 2 May, 2009 - 06:24 AM, said:

Wow if I came across like that then I offer huge apologies.

I was just having a pedantic discussion about coding styles.

If it came across as worse than that then that's my bad!

Apologies for any offence KYA

OK #define

Quote

You've been a very naughty boy.