You are on page 1of 30

/* Fibonacci Series c language */ #include<stdio.

h> main() { int n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-\n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf("%d\n",next); } return 0; } Armstorng #include<stdio.h> #include<conio.h> main() { int r; long number = 0, c, sum = 0, temp; printf("Enter the maximum range upto which you want to find armstrong numbers "); scanf("%ld",&number); printf("Following armstrong numbers are found from 1 to %ld\n",number);

for( c = 1 ; c <= number ; c++ ) { temp = c; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( c == sum ) printf("%ld\n", c); sum = 0; } getch(); return 0; Prime number #include<stdio.h> main() { int n, i = 3, count, c; printf("Enter the number of prime numbers required\n"); scanf("%d",&n); if ( n >= 1 ) { printf("First %d prime numbers are :\n",n); printf("2\n"); } for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n",i); count++;

} i++; } return 0; } palindrome or not\ #include<stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0; } Matrices #include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = { for ( d { for ( { sum } 0 ; c < m ; c++ ) = 0 ; d < q ; d++ ) k = 0 ; k < p ; k++ ) = sum + first[c][k]*second[k][d];

multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n");

} } return 0; } #include<stdio.h> main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } return 0; }

* *** ***** *******

*********

We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

C programming code
#include<stdio.h> main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } return 0; }

Output: Consider the pattern * ** *** **** ***** to print above pattern see the code below:
#include<stdio.h> main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("*"); printf("\n"); } return 0; }

For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:

Floyd triangle Pascal triangle

Comments
48 comments posted

i need a code for this pattern.


12345 1234 123 12 1 Posted by Guest on Fri, 26/08/2011 - 12:35

number pattern c code


#include<stdio.h> main() { int n, c, k, space; scanf("%d", &n); space = 0; for ( k = n ; k >= 1 ; k-- ) { for ( c = 1 ; c <= space ; c++ ) printf(" ");

space++; for ( c = 1 ; c <= k ; c++) printf("%d", c); printf("\n"); } return 0; } Posted by adminPs on Fri, 26/08/2011 - 17:06

thanks for the patterns it


thanks for the patterns it really helped me a lot.
Posted by Guest on Tue, 23/08/2011 - 22:04

i need code of this pattern


A B C D E F G H A B C D E F G A B C D E F A B C D E A B C D A B C A B A Posted by Guest on Sat, 20/08/2011 - 18:07

c program for character pattern


#include<stdio.h> main() { char ch = 'A'; int n, c, k, space = 0; scanf("%d", &n); for ( k = n ; k >= 1 ; k-- ) { for ( c = 1 ; c <= space ; c++) printf(" "); space++; for ( c = 1 ; c <= k ; c++ ) { printf("%c ", ch); ch++; } printf("\n"); ch = 'A'; } return 0; } Posted by adminPs on Sat, 20/08/2011 - 21:29

c language
diamond pattern of stars
Posted by Guest on Sat, 20/08/2011 - 14:51

diamond pattern
Diamond pattern
Posted by adminPs on Fri, 26/08/2011 - 09:48

please help give me source code


1 01 010 1010 10101
Posted by Guest on Fri, 19/08/2011 - 17:38

c program to print pattern


#include<stdio.h> main() { int n, c, k, num = 1; scanf("%d", &n); for ( c = 1 ; c <= n ; c++ ) { for ( k = 1 ; k <= c ; k++ ) { printf("%d", num); if ( num == 0 ) num = 1; else num = 0;

} printf("\n"); } return 0; } Posted by adminPs on Sat, 20/08/2011 - 08:43

can you write code for this


p pr pro prog progr progra program
Posted by Guest on Thu, 18/08/2011 - 21:20

pattern for string


Just input the string and press enter, corresponding pattern will be printed.
#include<stdio.h> #include<string.h> main() { char string[100]; int c, k, length; printf("Enter a string\n");

gets(string); length = strlen(string); for ( c = 0 ; c < length ; c++ ) { for( k = 0 ; k <= c ; k++ ) { printf("%c", string[k]); } printf("\n"); } return 0; } Posted by adminPs on Thu, 18/08/2011 - 21:59

c programming code
please get me the code of the c program to get the output as follows: 1 121 12321 1234321 123454321
Posted by Guest on Thu, 18/08/2011 - 20:40

c program to print number pattern


#include<stdio.h> main()

{ int n, c, k, x = 1; scanf("%d", &n); for ( c = 1 ; c <= n ; c++ ) { for ( k = 1 ; k <= c ; k++ ) { printf("%d", x); x++; } x--; for ( k = 1 ; k <= c - 1 ; k++ ) { x--; printf("%d", x); } printf("\n"); x = 1; } return 0; } Posted by adminPs on Thu, 18/08/2011 - 22:10

c++ code to print pattern


1 1 12 21 12321 Posted by Guest on Wed, 17/08/2011 - 19:15

c++ code to print pattern


#include<iostream> using namespace std; main() { int n, k, c, space, x, num = 1; cin >> n; x = n; for ( k = 1 ; k <= n ; k++ ) { for ( c = 1 ; c <= k ; c++ ) { cout << num; num++; } num--; for ( c = 1 ; c <= 2*x - 3 ; c++ ) cout << " "; x--; if ( k != n ) { for ( c = 1 ; c <= k { cout << num; num--; } } else { num--;

; c++ )

for ( c = 1 ; c <= k - 1 ; c++ ) { cout << num; num--; } } printf("\n"); num = 1; } return 0; } Posted by adminPs on Thu, 18/08/2011 - 17:14

may i get its source code


1 232 34543 4567654 567898765 Posted by Guest on Tue, 16/08/2011 - 21:34

c code for number pattern


#include<stdio.h> main() { int n, c, d, num = 1, space; scanf("%d",&n);

space = n - 1; for ( d = 1 ; d <= n ; d++ ) { num = d; for ( c = 1 ; c <= space ; c++ ) printf(" "); space--; for ( c = 1 ; c <= d ; c++ ) { printf("%d", num); num++; } num--; num--; for ( c = 1 ; c < d ; c++) { printf("%d", num); num--; } printf("\n"); } return 0; } Posted by adminPs on Thu, 18/08/2011 - 17:17

how to print this pattern


******* ***S*** **SSS** *SSSSS*

where S represents Space


******* *** *** ** * ** *

Posted by Guest on Sat, 13/08/2011 - 17:53

c code for star pattern


#include<stdio.h> main() { int n, c, k, space, r; printf("Enter number of rows\n"); scanf("%d",&n); space = 1; r = n-1; for( c = 1 ; c <= 2*n - 1 ; c++ ) printf("*"); printf("\n"); for ( k = 2 ; k <= n ; k++ ) { for( c = 1 ; c <= r ; c++ ) printf("*"); for ( c = 1 ; c <= space ; c++ ) printf(" ");

space = 2*k-1; for( c = 1 ; c <= r ; c++ ) printf("*"); r--; printf("\n"); } return 0; } Posted by adminPs on Sat, 13/08/2011 - 19:17

Doubt
In the program, instead of the statement space=2*k-1; , can we go for the statement, space+=2; ?
Posted by Guest on Mon, 22/08/2011 - 09:47

sure
You can use but don't forget to initialize variable space to one.
Posted by adminPs on Mon, 22/08/2011 - 13:01

code
guys i need the code for this
* **

*** **** ***** Posted by Guest on Sat, 13/08/2011 - 17:15

star pattern source code


#include<stdio.h> main() { int n, c, k, space; printf("Enter number of rows\n"); scanf("%d",&n); space = n; for ( k = 1 ; k <= n ; k++ ) { for ( c = 1 ; c < space ; c++ ) printf(" "); space--; for( c = 1 ; c <= k ; c++ ) printf("*"); printf("\n"); } return 0; } Posted by adminPs on Sat, 13/08/2011 - 17:29

i need a program to draw the below patern


1 22 333 4444 55555
Posted by Guest on Fri, 12/08/2011 - 18:48

number pattern source code


#include<stdio.h> main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("%d", c); printf("\n"); } return 0; } Posted by adminPs on Fri, 12/08/2011 - 20:13

need help

i need program to print the following pattern :: 1 23 456 7 8 9 10


Posted by Guest on Thu, 11/08/2011 - 20:14

Floyd's triangle
This pattern is Floyd's triangle see Floyd's Triangle code.
Posted by adminPs on Fri, 12/08/2011 - 07:31

need a code in c to print below pattern


i want a code to print stars like below * ** *** **** *** ** *
Posted by Guest on Tue, 09/08/2011 - 21:46

stars pattern using c programming


#include<stdio.h>

main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++) { for ( k = 1 ; k <= c ; k++ ) printf("*"); printf("\n"); } for ( c = n - 2 ; c >= 0 ; c-- ) { for ( k = c ; k >= 0 ; k-- ) printf("*"); printf("\n"); } return 0; } Posted by adminPs on Tue, 09/08/2011 - 22:38

GET ME THE CODE OF A PROGRAM


GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:
* *A* *A*A*

*A*A*A* Posted by Guest on Tue, 09/08/2011 - 17:21

code to print pattern of stars and numbers


#include<stdio.h> main() { int n, c, k, space, count = 1; printf("Enter number of rows\n"); scanf("%d",&n); space = n; for ( c = 1 ; c <= n ; c++) { for( k = 1 ; k < space ; k++) printf(" "); for ( k = 1 ; k <= c ; k++) { printf("*"); if ( c > 1 && count < c) { printf("A"); count++; } } printf("\n"); space--; count = 1; } return 0; }

Posted by adminPs on Tue, 09/08/2011 - 21:46

codes for pyramid of stars.


i need this pattern ***** *** ** *
Posted by Guest on Mon, 08/08/2011 - 13:18

code for pattern


#include<stdio.h> main() { int n, c, k, temp; printf("Enter number of rows\n"); scanf("%d",&n); temp = n; for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= temp ; k++ ) printf("* "); temp--; printf("\n"); } return 0;

} Posted by adminPs on Mon, 08/08/2011 - 15:32

Please post the code for this


* ** *** **** Please post the code to print the following shape as soon as possible.
Posted by Guest on Mon, 08/08/2011 - 13:01

code
#include<stdio.h> main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("* "); printf("\n"); } return 0; }

Posted by adminPs on Mon, 08/08/2011 - 15:29

please help
i need c program to print no in this format 1 12 123 1234
Posted by Guest on Sat, 06/08/2011 - 13:59

source code
#include<stdio.h> main() { int number = 1, n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) { printf("%d ", number); number++; } number = 1; printf("\n"); }

return 0; } Posted by adminPs on Sun, 07/08/2011 - 11:32

Thanks giving and suggestion


Thanks for the pyramid program!! Please help me in the same problem. Can't we directly print k directly instead of number(in 2nd inner for loop)? I have tried it but output gives garbage values!!why does this happen? Please help me!
Posted by Guest on Mon, 08/08/2011 - 22:02

number pattern code


You are right there is no need to use variable number you can use k directly as in code below:
#include<stdio.h> main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("%d ", k);

printf("\n"); } return 0; } Posted by adminPs on Tue, 09/08/2011 - 11:19

i need a code in C to print pyramid of stars


i need a code in C to print stars like this
* * * * * * * * * *

please help?
Posted by Guest on Fri, 05/08/2011 - 11:53

c code to print pattern


#include<stdio.h> main() { int n, c, k = 2, j; printf("Enter number of rows\n"); scanf("%d",&n); for ( j = 1 ; j <= n ; j++ ) { for ( c = 1 ; c <= 2*n-k ; c++) printf(" ");

k = k + 2; for ( c = 1 ; c <= j ; c++) printf("* "); printf("\n"); } getch(); return 0; }

You might also like