Hi everyone, I'm new to c-programming. I'm currently trying to write a program using function call but end up in a mess. Can anyone help me please? I've been trying this for a few days.
I wanted to write a program with "factorial" and "power"

I'm refering to this example.

#include <stdio.h>


int power(int num, int raised_by);  <--- Function Prototype


main()
{
   int n, y, answer;
   int i;

   printf("Enter the number --> ");
   scanf("%d", &n);

   printf("Enter power to raise this number to --> ");
   scanf("%d", &y);


   answer = power(n, y);  <--- Function Call


   /* Print the answer */
   printf("%d raised to the power %d is %d\n", n, y, answer);

   return(0);

   int power(int num, int raised_by)
{
   int i, answer;

   /* Calculate the answer using for loop */           <--- Function
   answer = 1;
   for (i=0; i<raised_by; i++) {
      answer *= num;
   }

   /* Return the answer to the caller */
   return(answer);
}

here's a template i've been using but still cant figure it out how to start with

#include <stdio.h>
#include <conio.h>

void display_menu(void);
void func_1(void);
void func_2(void);
void func_3(void);
void func_4(void);

main()
{
    char uc;
    char buffer[10];


    while(1) {      /* endless loop */
        display_menu();
        fgets(buffer, sizeof(buffer), stdin);
        sscanf(buffer, "%c", &uc);

        switch (uc) {

        case '1':
            func_1();
            break;

        case '2':
            func_2();
            break;

        case '3':
            func_3();
            break;

        case '4':
            func_4();
            break;

        case 'q':
        case 'Q':
            return 0;

        }
    }

}

void display_menu(void)
{
    printf("\n");
    printf("    Main Menu\n");
    printf("    ---------\n\n");

    printf(" 1) - Week 11 - Demonstrate Bit operators\n");
    printf(" 2) - Week 12 - Bit operators using functions\n");
    printf(" 3) - Week 13 - Output number to display\n");
    printf(" 4) - Week 14 - Calculator\n");
    printf(" Q) - Quit\n");
    printf("\n");
    printf("      Enter your selection --> ");


}

void func_1(void)
{
    printf("this is func_1\n");
}

void func_2(void)
{
    printf("this is func_2\n");
}

void func_3(void)
{
    printf("this is func_3\n");
}

void func_4(void)
{
    printf("this is func_4\n");
}

Recommended Answers

All 7 Replies

Please use the code tags next time you post code. If the code you have works correctly so far (when the user enters '1', the program prints "this is func_1") then it looks like you just need to put some logic into the functions you've made there. That being said, just plan out what needs to happen to make each of those functions do what they're supposed to and code it.

Please use the code tags next time you post code. If the code you have works correctly so far (when the user enters '1', the program prints "this is func_1") then it looks like you just need to put some logic into the functions you've made there. That being said, just plan out what needs to happen to make each of those functions do what they're supposed to and code it.

Thx bluefox :-) I'll try right now.

/*
     Program to calculate the power
     of any number without using header file
     'a' raise to the power 'n'





*/



#include<iostream.h>

#include<conio.h>

long int power(int , int);

void main()
{
  int a,n;
  long int ans=1,choice;


clrscr();


do
 {
  cout<<"Enter the value of a : ";
  cin>>a;
  cout<<"\nEnter the value of n : ";
  cin>>n;
  if(a>0&&n>0)
   {
     ans=power(a,n);
   }



 cout<<"\n\nResult : "<<ans;
   cout<<"\nMore?(0 for EXIT) : ";
   cin>>choice;

 }while(choice!=0);


getch();
}



long int power(int a,int n)
{


long int pow;
int i;
pow=a*a;

for(i=2;i<n;i++)
  {
  pow=pow*a;
  }



return(pow);
}

prashant_s, What were you doing? Trying to throw some ugly pieces of code?

start quote:

/*
     Program to calculate the power
     of any number without using header file
     'a' raise to the power 'n'





*/



#include<iostream.h>

#include<conio.h>

long int power(int , int);

void main()
{
  int a,n;
  long int ans=1,choice;


clrscr();


do
 {
  cout<<"Enter the value of a : ";
  cin>>a;
  cout<<"\nEnter the value of n : ";
  cin>>n;
  if(a>0&&n>0)
   {
     ans=power(a,n);
   }



 cout<<"\n\nResult : "<<ans;
   cout<<"\nMore?(0 for EXIT) : ";
   cin>>choice;

 }while(choice!=0);


getch();
}



long int power(int a,int n)
{


long int pow;
int i;
pow=a*a;

for(i=2;i<n;i++)
  {
  pow=pow*a;
  }



return(pow);
}

end quote.

Thx mate but i'mworking on c not c++. Anyway, appreciate your help :-)

recursion is your friend

long factorial(int n)
{
  if((n==0)||(n==1))
    return(1);
  else
    return(factorial(n-1));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.