Like Tree1Likes
  • 1 Post By std10093

Help plz in making power function?

This is a discussion on Help plz in making power function? within the C Programming forums, part of the General Programming Boards category; Im getting some troubles in making a program that uses function to display results power of entered number with entered ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Thumbs down Help plz in making power function?

    Im getting some troubles in making a program that uses function to display results power of entered number with entered power and results are passed back to main(). See my code, there is something wrong, how can I make output?

    Code:
    #include<stdio.h>
    
    int power(int a,int b)
    {
     int i,j;
     for(i=1; i<b; i++)
     {
     a=a*a;
     }
     j=a;
     return j;
    }
    
    void main()
    {
       int n,p;
       printf("\nEnter number: ");
       scanf("%d", &n);
       printf("\nEnter Power: ");
       scanf("%d", &p);
    
       power(n,p);
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    I got it......I got it....



    Code:
    #include<stdio.h>
    #include<conio.h>
    int power(int a,int b);
    
    void main()
    {
       clrscr();
       int n,p;
       printf("\nEnter number: ");
       scanf("%d", &n);
       printf("\nEnter Power: ");
       scanf("%d", &p);
    
       power(n,p);
    
       getch();
    
    
    }
    
    int power(int a,int b)
    {
     int i,c=a;
     for(i=1; i<b; i++)
     {
     a=a*c;
     }
     printf("\n%d",a);
    }

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Really? You have main returning void and you are sure ok?

    Have been to that position too. I posted something and then I solved it by myself.. So, that proves that I didn't try enough by myself, thus I posted too easily. We should post, if we really need to! But this happens. You found the solution, but most of the users read "I got it.." and leave the thread. It would be better if you wrote something like "It works, any suggestions though? "

    So replace void main()
    to
    Code:
    int main(void)
    Also have the main retuning zero before exiting, like this
    Code:
    getch();
    return 0;
    }
    that way you inform the rest of the world that everything went ok

    Also, as I think you have already told, but to make sure everybody can see that, conio.h is not standard, thus I would suggest everybody never use it. For more, click here.

    Also, since function power does not seem to really return nothing, change the prototype of the function to this
    Code:
    void power(...);
    Don't forget to change the prototype of the function before and after main.

    Also try to input as number 2 and as power zero. Are you getting the correct result? My guess is no.

    Why? Because of the terminate condition in your for loop inside the body of function power. Maybe you should modify the power function to this
    Code:
    void power(int a,int b)
    {
        int i,c=a;
        if(b == 0)
        {
            printf("\n1");
            /* Even though the function's return */
            /* value is void, we always can return (terminate) */
            /* the function, but we simply don't return something. */
            return; 
        }
        for(i=1; i<b; i++)
        {
            a=a*c;
        }
        printf("\n%d",a);
    }
    So, I suppose this code wasn't ok. But, I have to admit you really did a good try
    shansajid likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. power function
    By yjn in forum C Programming
    Replies: 5
    Last Post: 03-16-2012, 08:38 AM
  2. Power Function through Recursive
    By neo_2010 in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2009, 08:45 PM
  3. Th power function
    By wiz23 in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2005, 05:56 AM
  4. Recursive Power function
    By MethodMan in forum C Programming
    Replies: 12
    Last Post: 12-08-2002, 01:13 PM
  5. power function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-06-2001, 12:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21