Thread: a really really simple problem...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Question a really really simple problem...

    i really need a big help..
    i just started learning C++..
    the problem is this..

    i need to write a code in which
    the user inputs two numbers (3 and 5, for example..)
    and it goes as follows:
    243

    which is..
    3 x 3 x 3 x 3 x 3...
    notice how 3(the first number) was multiplied 5(second number) times.
    this needs to be done using two functions..
    and it needs to include #include<stdio.h>

    int main(void) and
    int multiply(int a)

    can somebody please help me out with this?
    Last edited by geshupenst; 11-12-2008 at 11:58 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So have you done either of these?
    - reading user input
    - for loops

    If you have, you're not completely stuck.

    Also, stdio.h is C, not C++.
    So you need to clarify which you're actually using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Your actually talking about the "base - exponent" feature of integers.
    As a small hint, try to think how you would calculate that. You know it will use mutliplication, but the trick is deciding what to multiply against what, and how many times.
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    wow thanks a lot
    that helped alot
    took me some time, but got it done ha!

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int power(int, int);
    
    int main()
    {
    	int x, y, z;
    
    
    	printf("Please enter two numbers : \n");
    	scanf("&#37;d",&x);
    	scanf("%d",&y);
    	z = power(x,y);
    	
    	printf("%d power %d : %d\n", x, y, z);
    }
    
    int power(int a, int b)
    {
    	 if (b==1)
    	 return a;
    	
    	 else
    	 return (a * power(a, b-1));
    }
    oh... C.. right. hehe
    this is how i got it done..
    how is it?
    Last edited by geshupenst; 11-12-2008 at 05:25 PM. Reason: Not C++.. sorry 'bout that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM