English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have to print fibbonacci series using for loop,and also take a integer and print its factorial using for loop

2007-01-29 05:20:19 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

You just need to understand how the fib. series and factorials work...

If you have a basic understanding of the C language the rest will fall in place.

The pattern for the fib numbers is to start with: 0,1 then add the last two numbers together to get the next number
(the third number will be 1 + 0 = 1) : 0,1,1
(the fourth number will be 1 + 1 = 2) : 0,1,1,2
(the fifth number will be 2 + 1 = 3) : 0,1,1,2,3
etc...

Factorials are a multiplication of all the numbers up to the current number... and are denoted with an !
4 factorial: 4! : 4 * 3 * 2 * 1
5 factorial: 5! : 5 * 4 * 3 * 2 * 1

etc...

2007-01-29 05:33:10 · answer #1 · answered by Anonymous · 0 0

I won't give you code but I will give you an Idea of how to do it. Take 2 integer variables and set them to 1. Add them together and put that in another variable. Print the first number to the screen then make variable 1 = to variable 2. And make variable 2 = to variable 3 you may need some extra variables to do the swap. Then repeat.

2007-01-29 05:33:08 · answer #2 · answered by The Master 5 · 0 0

Fibbonacci sequence:

int x = 1, y = 1;

int total_numbers = TOTAL_NUMBER_TO_PRINT; // how many you want to print out.

printf("%d %d ", x, y); // first 2 numbers

for (int nexttotal = 0; total_numbers > 2; total_numbers--)
{
nexttotal = x + y;
printf("%d ", nexttotal);
x = y;
y = nexttotal;
}

factorial:

long i = NUMBER_TO_BE_FACTORIALED; // i.e. 64

long total = 1;

for (; i > 1; i--)
total = total * i;

printf("%ld", total);

2007-01-29 05:41:33 · answer #3 · answered by lei514 1 · 0 0

n! help


#include
#include
#include
#include
using namespace std;



int fact(int n)
{

int i;
long int x=1;
for (i=1;i<=n;i++){
x=x*i;

}
return x; }



int main() {

int a;

while (1) {

printf("\n Enter a number ");
scanf("%d",&a);

printf("\n %10d! = %5ld \n",a,fact(a));

system("pause");

}
return 0;

}

2007-01-29 05:35:45 · answer #4 · answered by iyiogrenci 6 · 0 0

fedest.com, questions and answers