Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number.
I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c.
For example i type 10. It must show first 10 numbers along with their factorials (Making a table)
Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
printf("Factorial of %d js %d\n", n, fakt);
getch();
}
You probably want this:
Program:
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
{
fakt=fakt*i;
printf("Factorial of %d is %d\n", i, fakt);
}
getch();
}
Output:
Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
#include <conio.h>
#include <stdio.h>
void main()
{
int f=1,i,v;
clrscr();
printf("Enter the number :");
scanf("%d",&v);
for(i=1;i<=v;i++)
{
f=f*i;
printf("num =%d and fac=%d\n",i,f);
}
getch();
}
this code will work
That code already uses the for loop. The while loop equivalent is:
i = 1;
while (i <= n) {
fakt = fakt*i;
i++;
}
#include <stdio.h>
int factorial(int n)
{
int i,fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
return fakt;
}
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d", &n);
int i = 0;
for(i=1;i<=n;i++)
{
printf("Factorial for %d is %d\n",i,factorial(i));
}
return 0;
}
I think this will do the job just fine.
You can do a nested loop.
Run the parent loop from 1 to n,
and the nested loop will be your already working for loop.
You may want this:
#include <stdio.h>
int main()
{
int n, i, num, factorial;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
num = i;
factorial = 1;
while(num)
factorial *= num--;
printf("%d \t %d\n", i, factorial);
}
return 0;
}
Output:
Enter the number of terms: 10
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Use this fastest version of factorial using recursion with if ..else statement
#include<stdio.h>
int fact(int n);
void main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
fact(n);
}
int fact(int n)
{
int a;
if(n==0)
{
printf("The Factorial of 0 is 1\n");
return 1;
}
else
{
a=n*fact(n-1);
printf("The Factorial of %d is %d\n",n,a);
return a;
}
}
#include<stdio.h>
int main(int n){
int fact;
clrscr();
printf("Enter a number and type exit:\n");
scanf("%d",&n);
if(n!=0){
fact=n*main(n-1);
printf("Factorial of %d is %d\n",n,fact);
getch();
return fact;
}
else{
printf("Factorial of 0 is 1.\n");
getch();
return 1;
}
}
Related
I have 2 functions + my main.
One of them is a void function that "prints out instructions"
The other is the one that actually does what I want it to do.
For some reason when the play function is by itself in the main it works just fine, but as soon as I add the print instructions function, it breaks and I cannot figure out why it's doing that.
Functions:
int playGame();
void printInstructions();
`
int playGame()
{
int dice[100];
int diceAmount, j, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice[j] = rand() % 6 + 1;
sum += dice[j];
printf("Dice %d: %d\n",i+1,dice[j]);
}
printf("---------\nSum: %d", sum);
}
`
`
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
`
Whole thing:
`
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playGame();
void printInstructions();
int playGame()
{
int dice[100];
int diceAmount, j, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice[j] = rand() % 6 + 1;
sum += dice[j];
printf("Dice %d: %d\n",i+1,dice[j]);
}
printf("---------\nSum: %d", sum);
}
int main()
{
printInstructions();
playGame();
}
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
`
Without the printInstructions();
With the printIUnstruction();
Why is it breaking?
With the suggestions from UnholySheep & Martin James, I was able to get my code to work.
Here is the working code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playGame();
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
int playGame()
{
int dice;
int diceAmount, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice = rand() % 6 + 1;
sum += dice;
printf("Dice %d: %d\n",i+1,dice);
}
printf("---------\nSum: %d\n",sum);
}
int main()
{
printInstructions();
playGame();
}
Result:
I wrote a code in C to find the odd numbers from a given interval of min and max number. The function works well when it is inside the int main() but not well when outside the program as a function.
What's more is that it also prints the incremented number outside the max number given.
This is the code...
#include <stdio.h>
// My Function
int odd_numbers(int x, int y) {
for (int i = x; i <= y; ++i) {
if (i % 2 == 1) {
printf("%d\n",i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
printf("%d",odd_numbers(min_num,max_num));
}
and this is the output...
As you can see, it adds an 11 besides the 9...
How can I solve this? I've tried return 0; and it returns the value 0 but i only want to return no number except the odd numbers.
Here is the working code.
Notes
Change the return type of odd_numbers from int to void because you are not returning anything when the function is called.
Only call the function odd_numbers, no need to printf anything because odd_numbers already does the job.
#include <stdio.h>
// My Function
void odd_numbers(int x, int y) {
for (int i = x; i <= y; i++) {
if (i % 2 != 0) {
printf("\n%d",i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
odd_numbers(min_num,max_num);
}
Here is the modified code.
you have declare function return type int but return nothing. odd_numbers made to void type. no need to return anything
code:
#include <stdio.h>
// My Function
void odd_numbers(int x, int y)
{
int i = 0;
for (int i = x; i <= y; i++)
{
if (i % 2 != 0)
{
printf("%d\n", i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
odd_numbers(min_num, max_num);
return 0;
}
Can anyone tell me why is my output wrong "1* 3 = 1" whereas other multiplications is correct? Thanks!
#include <stdio.h>
int main()
{
int n,i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n",n);
while(i<= 12)
{
printf("\n%d*%d=%d", i, n, total);
i++;
total = i*n;
}
}
You are printing a value, total, before you assign any value to it. On the first iteration of the while loop, total doesn't contain any defined value.
Here's a way to fix it:
#include <stdio.h>
int main(void)
{
int n, i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n", n);
while (i <= 12)
{
total= i * n;
printf("\n%d*%d=%d", i, n, total);
i++;
}
}
Simply calculate total before you print it, not after.
I want to do this code that tells you the number of (n) integers that are bigger (or equal) than a (k) input.
So for example:
input:
4 15
12
6
15
24
output:
2
So the 4 is the number of integers the user is going to input and the 15 is the k number, now the output is the quantity of numbers that are bigger than k.
What I have of code is this:
#include<stdio.h>
int main()
{
int n, k, i;
int c, d;
scanf(" %d",&n);
scanf("%d", &k);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if (c[i]>k)
c[i]=d;
}
printf("%d", d);
return 0;
}
As you can see my code sucks, I don't know how to find the integers that are bigger than k and to print the quantity, not the numbers themselves. Any help is really appreaciated. Thanks.
Far less elegant solution, but one that keeps the value you need for some further use.. OldProgrammer did it much simpler and more pretty.
int main()
{
int num, s, i, cnt = 0;
printf("please input number of integers and int to compare with\n");
scanf("%d %d", &s, &num);
int arr[s];
for(i = 0; i < s; i++)
{
printf("Please input %d. number", i+1);
scanf("%d", &arr[i]);
}
for(i = 0; i < s; i++)
{
if(arr[i] >= num)
cnt++;
}
//at this point cnt holds the value you need
return 0;
}
Not sure why you are trying to reference c as an array. That is not needed. Try this:
int main()
{
int n, k, i, c;
int count = 0;
scanf(" %d",&n);
scanf("%d", &k);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if (c > k)
count++;
}
printf("%d", count);
return 0
}
Also, I would rename your variables to something more meaningful, such as numEntries, checkValue, etc.
Seeing the solution to this online on Stackoverflow https://stackoverflow.com/a/19200031/3185410 I tried to come up with another solution by setting max to -infinity and min to +infinity.
The code here by #haccks works pretty fine.
#include <stdio.h>
int main()
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num)
max = num;
else if (min > num)
min = num;
}
printf ("\n%d %d", max, min);
return 0;
}
Here's the one I came up with:
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i;
min=INT_MAX;
max=INT_MIN;
for (i=0;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}
What am I doing wrong with that?
If the users input 5 then 6 you have :
for 5 :
5>-inf => max = 5
for 6 :
6>5 => max = 6
but then min has become 5 and yet it is still INT_MAX.
If you put if instead of else if, you loose in performance but have the right output I think (I cannot test right now).
Set first input as min and max value. And then add logic you wrote. Here you had 2 lines of extra code but you don't have to sacrifice performance.
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i=0;
min=INT_MAX;
max=INT_MIN;
printf("Enter number %d : ",i+1);
scanf("%d",&num);
min=num;
max=num;
for (i=1;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}