How can i print a pyramid using C? - c

I am using C in this case and its my first time creating multiples nested loops.
Let us consider this problem:
8
88
888
8888
88888
8888
888
88
8
I wrote this program :
#include <stdio.h>
int main() {
int n = 9;
int p = n + 1;
int q = p / 2;
for (int i = 1; i <= q; i++) {
for (int j = i; j <= q; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("8");
}
printf("\n");
}
for (int i = q + 1; i <= n; i++) {
for (int j = 1; j <= o; j++) {
printf(" ");
}
for (int i = o; i <= n; i++) {
printf("8");
}
printf("\n");
}
}
Instead of creating a pyramid, it is creating something that looks like this:
8
88
888
8888
88888
8888
888
88
8
What should i do to fix this?

int main() {
int n = 9;
int p = n + 1;
int q = p / 2;
for (int i = 1; i <= q; i++) {
for (int j = i; j <= q; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("8");
}
printf("\n");
}
for (int o = q + 1; o <= n; o++) {
for (int l = 1; l <= o - q + 1; l++) {
printf(" ");
}
for (int m = o; m <= n; m++) {
printf("8");
}
printf("\n");
}
}
https://godbolt.org/z/cPW14b5W1

You're using almost the entire lowercase alphabet as variable names... Who wants to keep track of all of that?
Two variables (one int and one string) suffice...
int main() {
int i;
char *out = " 88888";
i = 1; while( i < 5 ) printf( "%.5s\n", out + 0 + i++ );
i = 0; while( i < 5 ) printf( "%.5s\n", out + 5 - i++ );
return 0;
}
8
88
888
8888
88888
8888
888
88
8
Code should be clear & concise. Your readers will thank you...
EDIT
A comment by the OP below the OP suggests that this function should not be limited to 5 characters width.
Once the fundamental (short) code is written, it can be generalised.
int main() {
int i;
for( int n = 2; n <= 9; n++ ) {
char *out = malloc( n + n ); // don't need/use '\0'...
/* Omitting test for failure */
memset( out + 0, ' ', n );
memset( out + n, n + '0', n );
i = 1; while( i < n ) printf( "%.*s\n", n, out + 0 + i++ );
i = 0; while( i < n ) printf( "%.*s\n", n, out + n - i++ );
printf( "=======" ); getchar();
free( out );
}
return 0;
}

For the record,I have used this program and this program prints the number of stars according to user given input.
#include<stdio.h>
int main()
{
int n;
printf("Enter values: ");
scanf("\n%d",&n);
for(int i=1;i<=n;i++){
for(int j=i;j<n;j++){
printf(" ");
}
for(int k=1;k<=i;k++){
printf("*");
}
printf("\n");
}
for(int i=1;i<=n;i++){
for(int j=2;j<=i;j++){
printf(" ");
}
for(int k=i;k<=n;k++){
printf("*");
}
printf("\n");
}
}

Related

How to use strtok

Below is a solution to the codewars training.
https://www.codewars.com/kata/59f4a0acbee84576800000af/train/c
Test Crashed
Caught unexpected signal: SIGSEGV (11). Invalid memory access.
I get an error message like this, what's wrong?
I think it's probably part of the strtok function, but please tell me what's wrong.
#include <stdlib.h>
#include <string.h>
double pos_average(char *s, unsigned n)
{
char **matrix;
char *p;
unsigned int i, j, k;
unsigned int subst_len = ( strchr(s,',') - s ) / sizeof(char);
double count = 0;
matrix = (char**)calloc( n + 1 ,sizeof(char*) );
if(!matrix) exit(0);
for(i = 0; i < n; i++)
{
matrix[i] = (char*)calloc( subst_len + 1 ,sizeof(char) );
if(!matrix[i]) exit(0);
}
for(i = 0; i < n; i++)
{
if(i == 0){
p = strtok(s, " ");
strncpy(matrix[i], p, subst_len);
}
else{
p = strtok(NULL," ");
strncpy(matrix[i], p, subst_len);
}
}
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
for(k = 0; k < subst_len; k++)
{
if(matrix[i][k] == matrix[j][k]) count++;
}
}
}
for(i = 0; i < n; i++) free(matrix[i]);
free(matrix);
return (count / ( ( (double)n * ( (double)n - 1.0 ) ) / 2.0 ) ) * 100.0;
}

Why triple nested for loop doesn't work the 2nd time in C?

Edid: Thanks for the suggestions, but none of them work for the FOR loop error.
Why the triple nested for loop(line 82 - 98) works only the first time(correctly) and then it just spits crazy random numbers? The first time it works so good and then it does everything wrong. Thanks for the help!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
// 2 players, 5 zara, 3 puti(ako ne si gi haresa), moje da pazite:
//1 qift
// 2 qifta
// ful: 1*2 i 1*3
// 5 ednakvi: general
// 4 ednakvi
// 1*3
// 5 poredni
// shans
// izpulnqvase 8 puti i moje da se zapazva zara (samo pri 1 i 2 hvurlane)
int np, otg[5], otg10[5];
int check_pairs(int *otg10, int time){
int dicecopy[5];
memcpy(dicecopy, otg10, sizeof(int) * 5);
for(int k = 0; k < 5; k++){
printf("%d " , dicecopy[k]);
}
int pairs = 0, sum = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(dicecopy[i] == dicecopy[j])
pairs++;
}
}
// points
if(pairs == 1*2 || pairs == 2*2){
printf("GGGGGGG");
for(int k = 0; k < 5; k++){
sum += dicecopy[k];
}
} else {
return 0;
}
return sum;
}
int main(){
printf("How many players do you want: ");
scanf("%d" , &np);
int points[np];
for(int i = 0; i < np; i++){
points[i] = 0;
}
int dice[8][np][3][5];
srand(time(NULL));
for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
for(int c = 0; c < 3; c++){
for(int k = 0; k < 5; k++){
dice[i][j][c][k] = rand() % 6 + 1;
}
}
}
}
/* for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
for(int c = 0; c < 3; c++){
for(int k = 0; k < 5; k++){
printf("%d " , dice[i][j][c][k]);
}
putchar('\n');
}
putchar('\n');
}
putchar('\n\n');
}
*/
char anws = 'y';
for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
printf("\nPlayer: %d\nPoints: %d\n" , j + 1 , points[j]);
for(int k = 0; k < 3; k++){
for(int l = 0; l < 5; l++){
otg10[l] = dice[i][j][k][l];
printf("%d " , otg10[l]);
}
printf("\n\nDo you like it?\n");
scanf("%s" , &anws);
if(anws == 'y' || anws == 'Y'){
points[j] = check_pairs(otg10 , k);
break;
}
}
}
}
return 0;
}
The code is still not finished I have to do more work on the other check-ups. BTW this will represent the game called Generala, if you have any suggestions I'd like to hear them!

what is the better way to loop this problem?

#include <stdio.h>
int main()
{
int arr[9][9];
int i = 0, x = 10;
for (int i = 0, j = 0; j <= 8; j++) {
x++;
arr[i][j] = x;
}
for (int j = 8, i = 1; i <= 8; i++) {
x++;
arr[i][j] = x;
}
for (int i = 8, j = 7; j >= 0; j--) {
x++;
arr[i][j] = x;
}
for (int j = 0, i = 7; i >= 1; i--) {
x++;
arr[i][j] = x;
}
for (int i = 1, j = 1; j <= 7; j++) {
x++;
arr[i][j] = x;
}
for (int j = 7, i = 2; i <= 7; i++) {
x++;
arr[i][j] = x;
}
for (int i = 7, j = 6; j >= 1; j--) {
x++;
arr[i][j] = x;
}
for (int j = 1, i = 6; i >= 2; i--) {
x++;
arr[i][j] = x;
}
...
arr[4][4] = x + 1;
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
getch();
}
so I have this program, and I know you can loop it but how ? been sitting for an hour thinking and nothing came to my mind. By the way, the task is to append a matrix like on picture. Does anyone know to do it ? Maybe use some complex for loop
Here's one way to do it:
int arr[9][9] = {0};
int x = 0, i = 0, j = 0, vi = 0, vj = 1;
do {
++x;
arr[i][j] = x;
{
int ii = i+vi;
int jj = j+vj;
if (ii < 0 || ii >= 9 || jj < 0 || jj >= 9 || arr[ii][jj] != 0) {
if (vi != 0) {
vj = -vi;
vi = 0;
} else {
vi = vj;
vj = 0;
}
}
}
i = i+vi;
j = j+vj;
} while (arr[i][j] == 0);
Live on Coliru
Here's another way:
int arr[9][9] = {0};
int x = 0, i = 0, j = 0, vi = 0, vj = 1, lk = 8;
while (lk > 0) {
for (int k = 0; k < lk; ++k) {
++x;
arr[i][j] = x;
i += vi;
j += vj;
}
vi = vj;
vj = 0;
for (int k = 0; k < lk; ++k) {
++x;
arr[i][j] = x;
i += vi;
j += vj;
}
vj = -vi;
vi = 0;
if (vj > 0) {
++i;
++j;
lk -= 2;
}
}
arr[9/2][9/2] = x+1; // Only if odd dimensions
Live on Coliru
And here is yet another:
int arr[9][9] = {0};
int i = 0, lk = 8, x = 1;
while (lk > 0) {
for (int k = 0; k < lk; ++k) {
arr[i][i+k] = x + k;
arr[i+k][lk+i] = x + lk + k;
arr[lk+i][lk+i-k] = x + 2*lk + k;
arr[lk+i-k][i] = x + 3*lk + k;
}
x += 4*lk;
lk -= 2;
++i;
}
arr[9/2][9/2] = x; // Only if odd dimensions
Live on Coliru
Here is the "straight forward" option with for loops:
#include <stdio.h>
#define N 5
int main(void) {
int i,j,dim;
int matrix[N][N];
// init and print the matrix
for (i=0; i < N; i++)
{
for (j=0; j< N; j++)
{
matrix[i][j] = i*N + j;
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
// perform spiral print
for (dim = 0; dim < (N+1)/2; dim++)
{
// set initial i and go till the "last column"
i = dim;
for (j = dim; j < N - dim; j++)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
// bring back i and j to the proper coordinate
// and move down to the "last row"
j--;i++;
for (; i < N - dim; i++)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
// bring back i and j to the proper coordinate
// and move back to the "first column"
i--;j--;
for (; j >= dim; j--)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
// bring back i and j to the proper coordinate
// and move up to the "first row"
j++;i--;
for (; i > dim; i--)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
The output, as can be seen here is
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
0 1 2 3 4
9 14 19 24
23 22 21 20
15 10 5
6 7 8
13 18
17 16
11
12
==========================================================================
Looks like I misunderstood the question but the step from "printing" clockwise to "setting" clockwise is really small. Here is the setting flow:
#include <stdio.h>
#define N 5
int main(void) {
int i,j,dim, val = 1;
int matrix[N][N];
// perform spiral print
for (dim = 0; dim < (N+1)/2; dim++)
{
// set initial i and go till the "last column"
i = dim;
for (j = dim; j < N - dim; j++)
{
matrix[i][j] = val++;
}
// bring back i and j to the proper coordinate
// and move down to the "last row"
j--;i++;
for (; i < N - dim; i++)
{
matrix[i][j] = val++;
}
// bring back i and j to the proper coordinate
// and move back to the "first column"
i--;j--;
for (; j >= dim; j--)
{
matrix[i][j] = val++;
}
// bring back i and j to the proper coordinate
// and move up to the "first row"
j++;i--;
for (; i > dim; i--)
{
matrix[i][j] = val++;
}
}
// print the matrix
for (i=0; i < N; i++)
{
for (j=0; j< N; j++)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
The output as shown here is
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
My solution. Using an "object" struct strangeite_s (from strange ite-rator). It allows ease reusing on different arrays and probably could be even rescaled to support n-dimensional arrays.
The strangeite is initialized using _init function with specified dimensions sizes of an 2d array. Each time _loop is evaluated, the x and y positions are updated and the condition for loop end is checked (and returned). The _inc function should be called on each increment of the iterator.
#include <stdio.h>
#include <limits.h>
#include <stddef.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
struct strangeite_s {
size_t steplen[2];
size_t idx[2];
size_t cursteplen;
int direction;
};
void strangeite_init(struct strangeite_s *t, size_t max_x, size_t max_y)
{
assert(t != NULL);
t->steplen[0] = max_y;
t->steplen[1] = max_x;
memset(t->idx, 0, sizeof(t->idx));
t->direction = 0;
t->cursteplen = t->steplen[0];
}
bool strangeite_loop(const struct strangeite_s *t, size_t *x, size_t *y)
{
if (x) *x = t->idx[0];
if (y) *y = t->idx[1];
for (size_t i = 0; i < sizeof(t->steplen)/sizeof(t->steplen[0]); ++i) {
if (t->steplen[i] == 0) {
return false;
}
}
return true;
}
void strangeite_inc(struct strangeite_s *t)
{
if (t->cursteplen != 1) {
--t->cursteplen;
} else {
t->direction = ++t->direction % (2 * 2);
t->cursteplen = --t->steplen[t->direction % 2];
}
const size_t idx_to_change = t->direction % 2;
t->idx[idx_to_change] = t->idx[idx_to_change] + ( t->direction < 2 ? +1 : -1 );
}
int main()
{
int var[5][5];
struct strangeite_s i;
strangeite_init(&i, 5, 5);
int idx = 0;
for (size_t x, y; strangeite_loop(&i, &x, &y); strangeite_inc(&i)) {
var[y][x] = ++idx;
}
for (size_t i = 0; i < 5; ++i) {
for (size_t j = 0; j < 5; ++j) {
printf("%d ", var[i][j]);
}
printf("\n");
}
return 0;
}
Produces the following output:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Live version at onlinegdb.
It's possible to deterministically compute any one entry in the array A_{i,j} as a function only given the array size N, and i, j, in O(1), on-line, without any state, noticing the radius is a geometric progression. The space requirement is O(1) since one doesn't actually need the array to store the values; we can scan the array sequentially. However, like ray-tracing, it probably is slower, (up to a constant, it's still O(N^2).)
#include <stdio.h>
/* N: The size of the (simulated) array. */
#define N (16)
/* i, j: The array indices, as if, a[i][j]. */
static int a(const int i, const int j) {
/* (x,y) translation of (i,-j) to the centre, scaled up 2x for int math. */
const int x = 2 * i + 1 - N, y = -2 * j - 1 + N;
/* Geometric series and an offset +fiddling to get the directionality. */
return N*N - ((x < -y) ?
(-x > -y) ? (x+1)*(x+1) - (y+x)/2 - 1: y*y + (x+y)/2 :
(x > y) ? x*x + (x+y)/2 : (y+1)*y + (-x+y)/2);
}
int main(void) {
int i, j;
for(j = 0; j < N; j++) {
for(i = 0; i < N; i++) {
printf("%3d ", a(i, j));
}
printf("\n");
}
return 0;
}

remove duplicate number in array in c

I want my output print the array without any duplicate number
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUBBLE 5
int main()
{
int myArray[BUBBLE];
int i, j ,a , b, k;
int temp = 0;
int num;
int cunt, size;
cunt=0;
float floatType;
int integerType;
srand(time(NULL));
make the array randomlly
//Fill Array Randomlly
for (i = 0; i < BUBBLE; i ++)
{
num = rand() % BUBBLE + 1;
myArray[i] = num;
}
here my problem is not working
in the program tell me
Error value required as decrement operand
for (a = 0; i < BUBBLE; a++) {
for (b = a + 1; j < BUBBLE;) {
if (myArray[i] == myArray[i]) {
for (k = b; k < BUBBLE; k++) {
myArray[k] = myArray[k + 1];
}
BUBBLE --;
} else
b++;
}
}
Sort Array With Bobble Algorhim
here my sort
for(i = 0; i < BUBBLE; i++)
{
for (j = 0; j < BUBBLE-1; j++)
{
if (myArray[j] > myArray[j+1])
{
temp = myArray[j];
myArray[j] = myArray[j+1];
myArray[j+1] = temp;
cunt++;
}
}/*End inner for loop*/
}/*End outer for loop*/
the output
//Print Array After Sort
for (i = 0; i < BUBBLE; i++)
{
printf("%d\n",myArray[i]);
}
// Count For How Many Swap
printf("the numbeer of pases is %d \n" ,cunt);
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of int: %ld bytes\n",sizeof(integerType));
system("PAUSE");
return 0;
}/*End of main*/
You may not change integer constants. At the compile-time this code snippet
for (a = 0; i < BUBBLE; a++) {
for (b = a + 1; j < BUBBLE;) {
if (myArray[i] == myArray[i]) {
for (k = b; k < BUBBLE; k++) {
myArray[k] = myArray[k + 1];
}
BUBBLE --;
} else
b++;
}
}
actually looks like
for (a = 0; i < 5; a++) {
for (b = a + 1; j < 5;) {
if (myArray[i] == myArray[i]) {
for (k = b; k < 5; k++) {
myArray[k] = myArray[k + 1];
}
5--;
^^^
} else
b++;
}
}
That is the compiler substitutes the name BUBBLE for the integer constant 5.
Moreover it is unclear where for example the variables i and j are initialized. And this condition in the if statement
if (myArray[i] == myArray[i]) {
^^^ ^^^
does not make sense.
You should declare a variable that will keep the actual number of elements in the array during deleting duplicates because you can not change the size of an already initialized array.
"Removing" duplicates can look as it is shown in the demonstrative program
#include <stdio.h>
#define BUBBLE 5
int main(void)
{
int a[BUBBLE] = { 1, 2, 1, 3, 2 };
for ( size_t i = 0; i < BUBBLE; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
size_t n = 0;
for ( size_t i = 0; i < BUBBLE; i++ )
{
size_t j = 0;
while ( j < i && a[j] != a[i] ) ++j;
if ( j == i )
{
if ( n != i ) a[n] = a[i];
++n;
}
}
for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
Its output is
1 2 1 3 2
1 2 3
The variable n keeps the actual number of elements of the array after removing duplicates.

Explain Fibonacci code

#include<stdio.h>
#define max 2000
int arr1[max], arr2[max], arr3[max];
void fib();
int main()
{
int num, i, j, flag = 0;
for(i = 0; i<max; i++)
arr1[i] = arr2[i] = arr3[i] = 0;
arr2[max - 1] = 1;
printf("Enter the term : ");
scanf("%d", &num);
for(i = 0; i<num; i++)
{
fib();
if(i == num - 3)
break;
for(j = 0; j<max; j++)
arr1[j] = arr2[j];
for(j = 0; j<max; j++)
arr2[j] = arr3[j];
}
for(i = 0; i<max; i++)
{
if(flag || arr3[i])
{
flag = 1;
printf("%d", arr3[i]);
}
}
getch();
return 1;
}
void fib()
{
int i, temp;
for(i = 0; i<max; i++)
arr3[i] = arr1[i] + arr2[i];
for(i = max - 1; i>0; i--)
{
if(arr3[i]>9)
{
temp = arr3[i];
arr3[i] %= 10;
arr3[i - 1] += (temp / 10);
}
}
}
The above code generates the nth Fibonacci number. I am not able to understand how this works. Basically the Fibonacci number get stored in a very large array arr3[].
Please explain the logic involved in this code.
How does the fib() function work as well?
Here is a simple Fibonacci loop.
#include <stdio.h>
int main()
{
int term = 20, last2=0, last1=1, fib, i;
for (i=0; i<term; i++) {
fib = last2 + last1;
last2 = last1;
last1 = fib;
}
printf ("Term %d = %d\n", i, fib);
return 0;
}
Program output:
Term 20 = 10946
Although there is more than one idea as to where the sequence starts.
The example code in the original post is dealing with large numbers by storing 1 decimal digit per element in each of the arrays. It initializes arr[3] = arr2[] = arr1[] = 0, then arr2[] = 1. In the loop, fib() performs one instance of arr3[] = arr1[] + arr2[], handling the carries, then the loop does arr[1] = arr2[], arr2[] = arr3[]. If num < 3, the for loop exits on the loop condition i < num, if n >= 3, the loop exit when i == (num-3). (This could be avoided). The print loop skips leading zeroes in arr3[], setting flag once a non-zero value is found. The code needs some minor fixes. Here is a fixed example. Note that getch() may be _getch() in some environments (from conio.h). The second example below only uses two arrays. Fibonacci numbers starting from 0 are 0 1 1 2 3 5 8 ...
#include <conio.h>
#include <stdio.h>
#define max 2000
int arr1[max], arr2[max], arr3[max];
void fib();
int main()
{
int num, i, j;
for(i = 0; i<max; i++)
arr1[i] = arr2[i] = arr3[i] = 0;
arr1[max - 1] = 1;
printf("Enter the term : ");
scanf("%d", &num);
for(i = 0; i<num; i++)
{
fib();
for(j = 0; j<max; j++)
arr1[j] = arr2[j];
for(j = 0; j<max; j++)
arr2[j] = arr3[j];
}
for(i = 0; i < max-1; i++)
if(arr3[i])
break;
for( ; i < max; i++)
printf("%d", arr3[i]);
getch();
return 0;
}
void fib()
{
int i, temp;
for(i = 0; i<max; i++)
arr3[i] = arr1[i] + arr2[i];
for(i = max - 1; i>0; i--)
{
if(arr3[i]>9)
{
temp = arr3[i];
arr3[i] %= 10;
arr3[i - 1] += (temp / 10);
}
}
}
This example only uses two arrays, by alternating which array contains the sum (a1 += a0, a0 += a1). It uses Duff's device to enter the loop. Since the largest sum from digit + digit + carry is < 20, the carry loop in fib() was simplified.
#include <conio.h>
#include <stdio.h>
#define max 2000
void fib(unsigned char *a0, unsigned char *a1);
int main()
{
unsigned char a0[max], a1[max];
size_t i;
int n;
printf("Enter the term : ");
scanf("%d", &n);
for(i = 0; i < max; i++)
a0[i] = a1[i] = 0;
a0[max-1] = n & 1; /* if n even, a0=0=fib(0), a1=1=fib(-1) */
a1[max-1] = 1 - a0[max-1]; /* if n odd, a1=0=fib(0), a0=1=fib(-1) */
switch(n&1){
do{
fib(a0, a1);
case 1:
fib(a1, a0);
case 0:
continue;
}while(0 <= (n -= 2));
}
for(i = 0; i < max-1; i++)
if(a0[i])break;
for( ; i < max; i++)
printf("%d", a0[i]);
getch();
return 0;
}
void fib(unsigned char *a0, unsigned char *a1)
{
size_t i;
for(i = 0; i < max; i++)
a1[i] += a0[i];
for(i = max - 1; i > 0; i--){
if(a1[i] >= 10){
a1[i] -= 10;
a1[i-1] += 1;
}
}
}
Here's a much better implementation of the Fibonacci series
#include<iostream>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
return 0;
}

Resources