C++ Program to find the sum of the digits

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 7171 Views - Last Post: 03 May 2009 - 01:21 AM Rate Topic: -----

#1 wince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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'
Was This Post Helpful? 0
  • +
  • -

#3 wince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 >>
you don't declare a size for the array 'b' \\about this i dun understand 

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 wince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 =)
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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'
{
}


Was This Post Helpful? 0
  • +
  • -

#7 wince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ^^
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 02 May 2009 - 01:05 AM

View Postwince, 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++) {
}


Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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

Was This Post Helpful? 1
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

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

Posted 02 May 2009 - 07:07 AM

View Postjanotte, 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) ...
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 02 May 2009 - 07:22 AM

View PostKYA, 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.
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • 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

Was This Post Helpful? 0
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 02 May 2009 - 07:24 AM

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

View Postjanotte, 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
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

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

Posted 02 May 2009 - 07:30 AM

View Postjanotte, 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.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2