i'm a beginner in programming.
the code does get compiled but prints out values not understandable.
when i put those values inside the outer "for" by declaring extra integers, and put them into use, the code seems to properly work. What is the problem with the code below? Thanks!
#include <stdio.h>
int main( int argc, char** argv ) {
int num1, num2, i, j, k;
printf( "Enter the two Integers: " );
scanf( "%d%d", &num1, &num2 );
for ( i = (num1 < num2) ? num1 : num2; i <= (num1 > num2) ?num1 : num2; ; i++ ) {
for ( j = 1; j <= 9; j++ ) {
printf( "%d x %d = %-2d", i, j, i*j );
}
printf( "\n" );
}
return EXIT_SUCCESS;
}
You have a problem with operator precedence. The ternary operator has very low precedence, higher only than the assignment operator, compound assignment operators, and comma operator. Thus, your loop condition
i<=(num1>num2)?num1:num2
is equivalent to
(i<=(num1>num2))?num1:num2
. That expression will always evaluate to the value of either num1 or num2, so unless one of those is zero, your loop will never terminate. This also explains why you observed different behavior when you precomputed the ternary expressions.
You want this:
for(i = ((num1 < num2) ? num1 : num2);
i <= ((num1 > num2) ? num1 : num2); i++) {
But actually, I'd recommend the precomputation in the first place. The code is slightly longer, but much clearer.
For starters there is a typo in the for statement
for(i=(num1<num2)?num1:num2;i<=(num1>num2)?num1:num2;; i++)
^^^^
There is a redundant semicolon.
According to the C Standard the conditional operator is defined the following way
conditional-expression:
logical-OR-expression ? expression : conditional-expression
Relative to this expression
i<=(num1>num2)?num1:num2
then it is interpreted like
( i<=(num1>num2) ) ? num1 : num2
It is evident that you mean instead
i <= (num1 > num2 ? num1 : num2 )
Nevertheless it would be better to use one if statement before the loops than using the conditional operators in the loop.
Take into account that in any case the first loop is unsafe because the user can enter a value equal to INT_MAX. In this case the program will have undefined behavior. It is better and safe to use do-while loop.
Also for the result of multiplication it is better to use the type long long int.
The program can look the following way
#include <stdio.h>
int main(void)
{
int num1 = 0, num2 = 0;
printf( "Enter two Integers: " );
scanf( "%d%d", &num1, &num2 );
if ( num2 < num1 )
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}
int i = num1;
do
{
for ( int j = 1; j < 10; j++ )
{
printf( "%d x %d = %-2lld\n", i, j, ( long long int )i * j );
}
printf("\n");
} while ( i++ != num2 );
return 0;
}
Its output might look like
Enter two Integers: 1 5
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
Your weird results are due to operator precedence, the ternary operator is executed after comparing i with (num1>num2). So when you write
i<=(num1>num2)?num1:num2;
You're actually writing something like
(i<=(num1>num2))?num1:num2;
So the condition relies on the value of num1 or num2. Just add parenthesis around your ternary operator and you're good.
In i<=(num1>num2)?num1:num2, <= has higher precedence than ? :. So this expression is equivalent to:
(i <= (num1 > num2)) ? num1 : num2;
Thus, the expression always evaluates to one of num1 or num2. If they are both non-zero, then the loop continues forever.
What you actually want is:
i <= (num1 > num2 ? num1 : num2)
The etrtra ; is the problem. It means do nothing. The i++ as the 4th statement is ignored.
Anyway, complex expressions inside the for statement make code more difficult to read (by humans) same times with the negative performance effect.
Related
I have an assignment that requires me to make a quiz which generates random math questions. I'm fine with everything but i'm struggling to find a way to randomly choose between the mathematical operators "+" and "-".
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int choice = 0;
int lives = 0;
int question = 1;
int a;
int b;
int answer = 0;
int ans = 0;
int correct = 0;
printf("\n Welcome to Maths Tester Pro.");
printf("\n Please Select a difficulty:");
printf("\n 1) Easy");
printf("\n 2) Medium");
printf("\n 3) Hard \n");
scanf("%d%*c",&choice);
switch(choice)
{
case 1:
printf("You have selected Easy mode!");
lives = lives+3;
while ((lives !=0)&&(question !=6)){
if(question !=5){
//Ask Question
printf("\nQuestion %d of 5. You have %d lives remaining", question, lives);
srand(time(NULL));
a = (rand() % (10 - 1 + 1)) + 1; //make the sign random
b = (rand() % (10 - 1 + 1)) + 1;
printf("\n%d + %d = ",a ,b);
scanf("%d", &answer);
ans = a+b;
//If answer is correct
if((a+b) == answer){
printf("Correct!\n");
correct = correct + 1;
}
//If answer is incorrect
else{
printf("Incorrect! The correct answer was %d\n",ans);
lives=lives-1;
}
question = question + 1;
}
In my code I have it written as ans=a+b but I want it to be able to randomly pick either "+" or "-".
The easiest way to go would be to change the sign of b. To do so, simply multiply it by either 1 (keeps positive sign) or -1 (makes it a negative):
b = b * ((rand() - (RAND_MAX / 2)) > 0 ? 1 : -1);
Upon execution, you will randomly get a + b or a + (-b).
Example print of the resulting operator:
printf("%d%s%d = %d\n", a, (b > 0 ? "+" : ""), b, a + b);
Note: as pointed in earlier comments, you may also want to randomize the seed in order to prevent you application to keep providing the "same" random numbers with:
/* Randomize seed (needed once). */
srand(time(NULL));
/* Then make your calls to `rand()`. */
...
I offer this as an example of how to cleanly generate and print the sort of problems that you seem to want.
This produces 20 'sum' and/or 'difference' equations. You can simply suppress printing the total in order to pose the question to the user.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand( time( NULL ) );
for( int i = 0; i < 20; i++ ) {
int a = (rand() % 10) + 1; // 1 <= a <= 10
int b = (rand() % 20) - 9; // -9 <= b <= 10
printf( "%d %c %d = %d\n", a, "+-"[b<0], abs( b ), a+b );
}
return 0;
}
1 + 3 = 4
1 - 6 = -5
4 + 10 = 14
8 + 2 = 10
10 + 0 = 10
4 + 4 = 8
8 + 10 = 18
8 + 9 = 17
8 - 2 = 6
8 - 4 = 4
2 + 1 = 3
8 - 5 = 3
2 - 6 = -4
4 + 9 = 13
6 + 6 = 12
5 + 0 = 5
3 + 4 = 7
1 + 0 = 1
9 - 5 = 4
8 + 8 = 16
The keen eyed reader will notice that even "10 + 0" is a reasonable problem. Zero is the identity operator(?) for addition (like 1 being the identity operator(?) for multiplication.)
You're free to adjust the ranges of the terms to suit your questions.
You can use rand() function, which is defined in stdlib. And if you want your program to give you different random numbers every time you run it, you need to put srand(time(NULL)) at the beginning, but for the time function, you need to include time.h library
I have these three codes, which do work, but they're not printing what I expected them to print. I don't think I properly understand the math/precedence here and was wondering if someone could help me comprehend.
CODE A
int a;
int b = 1;
for (a = 1; a < b + 4; b++, a = b * 2)
printf("%i\n", a);
I expected it to print out 4, 5. but it's 3, 9. I understand that's correct -- but why?
CODE B.
int a = 5;
int b = 0;
while (a > 3)
{
b += a;
--a;
}
printf("%i, %i\n", a, b);
Admittedly I struggled figuring out the math. It prints out 3, 9 --- but I don't get why.
CODE C.
int a;
int b;
for (a = 7, b = 2; b < a; a++)
b += a - 2;
printf("%d, %d\n", b, a);
This prints out 13, 9 but I got 11, 7.
Let's step through the first loop:
for (a = 1; a < b + 4; b++, a = b * 2)
printf("%i\n", a);
First, we execute the initialization statement, which gives us a=1 and b=1 (b was set earlier in the code).
We execute the test expression (a < b + 4, which is 1 < 1 + 4), which is true, so we continue
We execute the loop body. We haven't performed any operations on a yet, so a is still equal to 1 so our output is:
1
Now execute the update expression, b++, a = b * 2. This
increments b (giving us b=2), and then sets a = b * 2, so a = 4.
We execute the test expression, and 4 < 2 + 4, so we continue.
We execute the loop body, which gives us as output:
4
We execute the update expression. We increment b, giving us
b=3, and then set a = b * 2, giving us a = 6.
We execute the test expression, and 6 < 3 + 4, so we continue.
We execute the loop body, giving us as output:
6
We execute the update expression. We increment b, giving us
b=4, and then set a = b * 2, giving us a = 8.
We execute the test expression. 8 < 4 + 4 is false, so we exit
the loop.
You can walk through a similar process for the other loops.
What might be part of the source of confusion is that a for loop in c will execute until the first semicolon found in the source code if there is not a surrounding pair { ... } to delineate several lines of code. For code A, the stuff after the second semicolon in the for loop is executed on every iteration and the printf statement is executed on every iteration too. In the last code snippet, code C. The printf is only executed after all the iterations of the for loop have completed. The same is true of the while loop in code snippet B, the printf executes after the while terminates. The while loop use of {...} delimiting characters makes this more obvious to the reader while the for loops do not.
Of course you still need to work through the calculations themselves too-which are also fairly tricky.
A for loop consists of the following structure:
for ( init; condition; increment ) {
statement(s);
}
As for how this actually executes, it is exactly equivalent to the following:
init;
while(condition) {
statement(s);
increment;
}
So, if we have the following code (CODE A):
int a;
int b = 1;
for (a = 1; a < b + 4; b++, a = b * 2)
printf("%i\n", a);
That means:
init: a = 1
condition: a < b + 4
increment: b++, a = b * 2
statement(s): printf("%i\n", a);
We can translate that into a while loop by substituting:
int a;
int b=1;
a = 1;
while(a < b + 4) {
printf("%i\n", a);
b++, a = b * 2;
}
Now, we can trace the execution step by step to see what's happening.
First loop:
1. b=1
2. a=1
3. a < b + 4
1 < 1 + 4
1 < 5
true
4. Output: 1
5. b++
b = b + 1
= 1 + 1
= 2
6. a = b*2
= 2*2
= 4
Second loop:
1. a < b + 4
4 < 2 + 4
4 < 6
true
2. Output: 4
3. b++
b = b + 1
= 2 + 1
= 3
4. a = b*2
= 3*2
= 6
Third loop:
1. a < b + 4
6 < 3 + 4
6 < 7
true
2. Output: 6
3. b++
b = b + 1
= 3 + 1
= 4
4. a = b*2
= 4*2
= 8
Fourth loop:
1. a < b + 4
8 < 4 + 4
8 < 8
false
2. End
Looking at Code A:
int a;
int b = 1;
for (a = 1; a < b + 4; b++, a = b * 2)
printf("%i\n", a);
b is set to 1;
a is set to 1;
a (1) is less than b + 4 (5), so the loop executes the printf(), printing 1;
b is incremented to 2; a is set to 4 (b * 2);
a (4) is less than b + 4 (6), so the loop executes the printf(), printing 4;
b is incremented to 3; a is set to 6 (b * 2);
a (6) is less than b + 4 (7), so the loop executes the printf(), printing 6;
b is incremented to 4; a is set to 8 (b * 2);
a (8) is not less than b + 4 (8 too), so the loop terminates.
You can apply a similar technique to the other cases.
Looking at Code B:
int a = 5;
int b = 0;
while (a > 3)
{
b += a;
--a;
}
printf("%i, %i\n", a, b);
a is set to 5;
b is set to 0;
a (5) is greater than 3 so the loop body executes;
b (0) has a (5) added to it, so it becomes 5;
a is decremented, so it becomes 4;
a (4) is greater than 3 so the loop body executes;
b (5) has a (4) added to it, so it becomes 9;
a is decremented, so it becomes 3;
a (3) is not greater than 3 so the loop terminates;
The printf() statement prints a then b, so the result is 3, 9.
Looking at Code C:
int a;
int b;
for (a = 7, b = 2; b < a; a++)
b += a - 2;
printf("%d, %d\n", b, a);
a is set to 7;
b is set to 2;
b (2) is less than a (7), so the loop executes the assignment operator;
a - 2 is 5 so b is set to 7 (2 + 5);
a is incremented to 8;
b (7) is less than a (8), so the loop executes the assignment operator;
a - 2 is 6, so b is set to 13 (7 + 6);
a is incremented to 9;
b (13) is not less than a (9), so the loop terminates;
The printf() statement prints b then a, so the result is 13, 9.
Link to CodeChef problem MAXSC
Attempted solution:
#include<stdio.h>
int main()
{
long long int n, t, k, i, j, max[701], a[701][701], sum, flag;
scanf( "%lld", &t );
for( k = 0 ; k < t ; k++ )
{
scanf( "%lld", &n );
for( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= n ; j++ )
{
scanf( "%lld", &a[i][j] );
if( j == 1)
max[i] = a[i][1];
if( a[i][j] > max[i] )
max[i] = a[i][j];
}
}
sum = 0, flag = 0;
for( i = 1 ; i <= n-1 ; i++ )
{
if( max[i] < max[i+1])
sum = sum + max[i];
else
{
flag = 1;
break;
}
}
if(flag == 1)
printf("-1\n");
else
{
sum = sum + max[n-1];
printf("%lld\n", sum );
}
}
}
Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead.
Constraint:
Code should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1.
Does this constraint mean we have to choose max element from each line?
If you look at example given:
Example Input:
1
3
1 2 3
4 5 6
7 8 9
Output:
18
Explanation
Example case 1: To maximize the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
If you notice they have mentioned "maximize".
Though my code finds the max, it isn't being accepted.
why is this code not being accepted?
Code's logic is flawed. When max[i] < max[i+1] is false, it sets flag = 1; instead of considering other elements from a[i].
Does this constraint mean we have to choose max element from each line?
No. The goal is a maximal sum, not a sum of maximums.
// if( max[i] < max[i+1])
if(max[i] < max[i+1])
sum = sum + max[i];
else {
flag = 1;
break;
}
The solution lies in tying other elements. Even if that fails, perhaps a prior selection should be changed. Recursion may be employed or other analysis. I think it would make sense to first sort each row of data to avoid this code taking n*n run-time. It should be trivial to code a n*n solution (trying every combination).
As this is homework, leave to OP to develop the solution.
1
3
6 10 12
4 5 7
8 9 10
-1
but output should be 6+7+10=23.
question is given Ei should be strictly greater than Ei-1
1
3
1 2 3
4 5 6
7 8 9
Output is 3+6+9=18 means 3<6 and 6<9 which satisfy the above problem
3
6 10 12
4 5 7
8 9 10
Output is 6+7+10=23 means 12<7 which is false and 6<7 which is true and 7<10 also true so sum=23.
3
8 9 10
11 12 13
10 5 9
Output is -1 because 10<13 is true but 13 is not less than 10 or 5 or 9 which is false hence output is -1.
I am trying to understand how recursion works in the factorial function. What should I print, so that I can see what actually happens during each recursion call?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
long factorial(long number) {
if(number <= 1) {
return 1;
} else {
return number * factorial(number - 1);
}
}
int main() {
int i;
for(i = 0; i <= 10; i++) {
printf("%2d ! = %ld\n", i, factorial(i));
}
return 0;
}
The following code
#include <stdio.h>
#include <stdlib.h>
long factorial (int level, long number){
int result;
printf("[%03d] Calculating: %d\n", level, number);
if(number <= 1)
{
result = 1;
}
else
{
result = number * factorial(level+1, number - 1);
}
printf("[%03d] Returning: %d\n", level, result);
return result;
}
int main(){
int i;
for(i = 0; i <= 10; i++)
{
printf("%2d ! = %ld\n", i, factorial(0, i));
}
return 0;
}
gives this output
[000] Calculating: 10
[001] Calculating: 9
[002] Calculating: 8
[003] Calculating: 7
[004] Calculating: 6
[005] Calculating: 5
[006] Calculating: 4
[007] Calculating: 3
[008] Calculating: 2
[009] Calculating: 1
[009] Returning: 1
[008] Returning: 2
[007] Returning: 6
[006] Returning: 24
[005] Returning: 120
[004] Returning: 720
[003] Returning: 5040
[002] Returning: 40320
[001] Returning: 362880
[000] Returning: 3628800
10 ! = 3628800
Where [XXX] is the current level of recursion.
Try this
long factorial (long number){
if(number <= 1){
return 1;
}else{
return number * factorial(number - 1);
}
}
Assume we pass 4 as first parameter, then function evaluates to:
long factorial (long number /*==4*/){
if(4 <= 1){//false
return 1;
}else{
return 4 * factorial(4 - 1);
}
}
Note return in else can't finish, because it calls another function - factorial (4-1).
So 4 must be multiplied by the result of factorial (3). So it goes and executes factorial(3).
Now we get
long factorial (long number /*==3*/){
if(3 <= 1){//false
return 1;
}else{
return 3 * factorial(3 - 1);
}
}
But again the return here can't finish, since it calls factorial for 3-1=2.
Now one needs to evaluate factorial (2), result of which will be multiplied by 3 and passed to previous return. Again:
long factorial (long number /*==2*/){
if(2 <= 1){//false
return 1;
}else{
return 2 * factorial(2 - 1);
}
}
We have similar problem. But now when executing factorial (2-1) the function will immediately return 1, hence above line will return 2.
This result will be plugged to the result of previous return, giving 2*3; this result - 6 - will be plugged in previous return and yield 6*4, which is the final result 24.
For something ridiculous, factor out the body of factorial:
long factorial_body(long number) {
if(number <= 1) {
return 1;
} else {
return number * factorial(number - 1);
}
}
Then, rewrite factorial to call factorial_body, printing some stuff around the call.
long factorial (long number) {
static char output[4096];
static int level;
static int index;
int saved_index;
long result;
switch (level++) {
case 1: index += snprintf(output + index, sizeof(output) - index, "= ");
default: index += snprintf(output + index, sizeof(output) - index,
"%ld x ", number + 1);
case 0: snprintf(output + index, sizeof(output) - index,
"factorial(%ld)\n", number);
}
saved_index = index;
printf("%s", output);
result = factorial_body(number);
index = saved_index;
snprintf(output + index, sizeof(output) - index,
--level ? "%ld\n" : "= %ld\n", result);
printf("%s", output);
return result;
}
factorial(10) will generate the following output:
factorial(10)
= 10 x factorial(9)
= 10 x 9 x factorial(8)
= 10 x 9 x 8 x factorial(7)
= 10 x 9 x 8 x 7 x factorial(6)
= 10 x 9 x 8 x 7 x 6 x factorial(5)
= 10 x 9 x 8 x 7 x 6 x 5 x factorial(4)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x factorial(3)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x factorial(2)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x factorial(1)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 6
= 10 x 9 x 8 x 7 x 6 x 5 x 24
= 10 x 9 x 8 x 7 x 6 x 120
= 10 x 9 x 8 x 7 x 720
= 10 x 9 x 8 x 5040
= 10 x 9 x 40320
= 10 x 362880
= 3628800
I am dubious if the above output will help enhance your understanding of recursion. But, working to understand how the printing code works probably would lead to a better understanding of recursion.
Instead of print statements add whatever you would print to an array or something and then print them out in the order that they were inserted (fifo) after the recursive function is finished.
This should give you a trace of the execution of factorial, with indentation:
long factorial(long number, int depth){
printf("%*s enter, number = %d\n", 4*depth, "", number);
long result = 1;
if (number > 1) result = number * factorial(number-1, depth+1);
printf("%*s exit, result = %d\n", 4*depth, "", result);
return result;
}
Here is the task I was given:
Write a function fact_calc that takes a string output argument and an integer input argument n and returns a string showing the calculation of n!. For example, if the value supplied for n were 6, the string returned would be
“6! x 6 x 5 x 4 x 3 x 2 x 1 x = 720”. Write a program that repeatedly prompts the user for an integer between 0 and 9, calls fact_calc and outputs the resulting string in a box of asterisks of the right size to surround the result. If the user inputs an invalid value, the program should display an error message and reprompt for valid input. Input of the sentinel -1 should cause the input loop to exit.
Here is my code:
#include <stdio.h>
/*Prototypes*/
void fact_calc(char [], int);
int main(void)
{
char calc[99];
int num1;
do{
printf("Enter an integer between 0 and 9 or -1 to quit: ");
scanf("%d", &num1);
fact_calc(calc, num1);
}while( num1 != -1 );
return (0);
} //end main
/*Place function definitions below*/
void fact_calc(char calc[], int num1)
{
if(num1 == 0)
char calc[] = "0! = 0 = 0";
else if(num1 == 1)
char calc[] = "1! = 1 = 0"
else if(num1 == 2)
char calc[99] = "2! = 2 x 1 = 2"
else if(num1 == 3)
char calc[99] = "3! = 3 x 2 x 1 = 6"
else if(num1 == 4)
char calc[99] = "4! = 4 x 3 x 2 x 1 = 24"
else if(num1 == 5)
char calc[99] = "5! = 5 x 4 x 3 x 2 x 1 = 120"
else if(num1 == 6)
char calc[99] = "6! = 6 x 5 x 4 x 3 x 2 x 1 = 720"
else if(num1 == 7)
char calc[99] = "7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040"
else if(num1 == 8)
char calc[99] = "8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320"
else if(num1 == 9)
char calc[99] = "9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880"
else
printf("Invalid Entry");
}
I am getting the error:
expected
expression
ch...
^
I still have to print out the string but I can't figure out what the error means or what I am doing wrong. Thanks in advance for the help!
char calc[] = "1! = 1 = 0"
Don't you think you should be ending this line with a ;
char calc[] = "1! = 1 = 0";
Similar fix needs to be done for the rest of the lines
Your program is doing something strange. I doubt it does what you expect.
You are declaring variables inside the body of if statements. These variables will only be in scope for the body, and you don't do anything with them.
Your program can be made to work. I suggest simply making fact_calc() return literal strings rather than try to assign to variables.
/*Place function definitions below*/
char const *fact_calc(char calc[], int num1)
{
if(num1 == 0)
return "0! = 0 = 0";
else if(num1 == 1)
return "1! = 1 = 0";
else if(num1 == 2)
return "2! = 2 x 1 = 2";
else if(num1 == 3)
return "3! = 3 x 2 x 1 = 6";
else if(num1 == 4)
return "4! = 4 x 3 x 2 x 1 = 24";
else if(num1 == 5)
return "5! = 5 x 4 x 3 x 2 x 1 = 120";
else if(num1 == 6)
return "6! = 6 x 5 x 4 x 3 x 2 x 1 = 720";
else if(num1 == 7)
return "7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040";
else if(num1 == 8)
return "8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320";
else if(num1 == 9)
return "9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880";
/* Invalid Entry */
return NULL;
}
Then your main function should declare a variable of type char const * to store the pointer returned by this function, and should check that the pointer is not set to NULL (and print "Invalid Entry" if the pointer was set to NULL).
You cannot assign strings (char arrays) in C. You should use strcpy() instead.
E.g.
strcpy(calc, "2! = 2 x 1 = 2");
Note that you should use strcpy() only if you are 100% certain calc is large enough to store the entire string. If not, use strncpy() instead. Example:
strncpy(calc, "2! = 2 x 1 = 2", 99);
calc[98] = '\0';
// a better way would be to pass 99 to your function, or to declare a macro
Finally, note that both 0! and 1! should be 1, not 0.