Jump to content

what will the code be like (simple math program)

- - - - -

  • Please log in to reply
6 replies to this topic

#1
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
i need help with a program i am writing a in c++ and Java .a program that when i input a figure like 2345 it will sum the figure up by adding 2+3+4+5 can any one help some one give me some code
int a = 0
int i = atoi(argv[1]); 
while (i) { 
    a += i % 10; 
    i /= 10; 
}
printf("%d\n", a);
but it does not run correctly.

Edited by Alexander, 10 November 2010 - 03:35 PM.
(cleanup, code tags)


#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Nothing seems wrong, except that you're missing semicolon after inicialization of a.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
it does not display the output. after i enter the value it will not display the result.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You mean is closes really really fast? Or it doesn't display anything at all? What IDE, compiler are you using? And how do you run your program (via IDE, cmd/terminal)?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
If the program is closing really fast and your using the header file <iostream>, just throw in cin.get(); before you return the main() function. cin.get(); prompts for keyboard input, there are alot of other ways to make sure your program doesn't close but thats the simplest way.

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
For C, put this at the end of your program (where you want to pause):
getc(stdin);

Latinamne loqueris?

#7
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

There doesn't seem any problem with the code: make sure that you are giving command line parameter to your program when you run it. If you want to prompt user to input a number you can use


char str[33];

scanf("%s", str);


I hope this helps!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users