C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Question about functions in C Posted by TimCereja on 17 Feb 2010 at 11:36 PM
I've written this program in C that asks for an integer input and outputs the numbers in reverse order. I need to cut the loop out of it into a separate function that is called upon, but I'm having trouble getting it to work. Any suggestions about how to make a function out of a part of the code?

#include <stdio.h>
int main( void )
{

int num, mod, rev = 0;

printf("\n");
printf("Enter an Integer Between 1 - 10,000: ");
scanf("%d", &num);
printf("\n");

while( num > 0)
{
mod = num %10;
rev = (rev * 10) +mod;
num = num / 10;
}

printf(" The Reverse of the Integer is: %d\n", rev);

return 0;
}

Report
Re: Question about functions in C Posted by anthrax11 on 18 Feb 2010 at 3:21 AM
#include <stdio.h>

int reverse(int num)
{
  int mod, rev = 0;
  while( num > 0)
  {
    mod = num %10;
    rev = (rev * 10) +mod;
    num = num / 10;
  }
  return rev;
}

int main( void )
{
  int num;

  printf("\n");
  printf("Enter an Integer Between 1 - 10,000: ");
  scanf("%d", &num);
  printf("\n");

  printf(" The Reverse of the Integer is: %d\n", reverse(num));

  return 0;
}
Report
Re: Question about functions in C Posted by Ching718 on 24 Feb 2010 at 6:44 PM
Can u help me about my C++ running program,here is my Proble.
Write a Program that outputs the balance of an account after each succeding year.The input is the initial balance,the interest rate,the first year,and the last year.Assume the deposit is made on January 1 and the interest for the pastyear is calculated and paid once each year on January 1.Next modify the program so that it shows three different balances for different ways of calculating the interest;simple interest, compounded semiannually,and compounded quarterly.Finally,modify the Program so that it allows the user to repeat this calculation as often as desired.



Report
Re: Question about functions in C Posted by Ching718 on 24 Feb 2010 at 6:45 PM
Can u help me about my C++ running program,here is my Proble.
Write a Program that outputs the balance of an account after each succeding year.The input is the initial balance,the interest rate,the first year,and the last year.Assume the deposit is made on January 1 and the interest for the pastyear is calculated and paid once each year on January 1.Next modify the program so that it shows three different balances for different ways of calculating the interest;simple interest, compounded semiannually,and compounded quarterly.Finally,modify the Program so that it allows the user to repeat this calculation as often as desired.






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.