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");
}