Sunteți pe pagina 1din 24

*

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

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.

Pattern program in C
1. #include <stdio.h>
2.
3. int main()
4. {
5. int row, c, n, s;
6.
7. printf("Enter the number of rows in pyramid of stars you wish to
see\n");
8. scanf("%d", &n);
9.
10. s = n;
11.
12. for (row = 1; row <= n; row++) // Loop to print rows
13. {
14. for (c = 1; c < s; c++) // Loop to print spaces in a row
15. printf(" ");
16.
17. s--;
18.
19. for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in
a row
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. return 0;
26. }

C pattern programs
Pattern:
*
*A*
*A*A*
*A*A*A*

C pattern program of stars and alphabets:

1. #include<stdio.h>
2.
3. int main()
4. {
5. int n, c, k, space, count = 1;
6.
7. printf("Enter number of rows\n");
8. scanf("%d", &n);
9.
10. space = n;
11.
12. for (c = 1; c <= n; c++)
13. {
14. for (k = 1; k < space; k++)
15. printf(" ");
16.
17. for (k = 1; k <= c; k++)
18. {
19. printf("*");
20.
21. if (c > 1 && count < c)
22. {
23. printf("A");
24. count++;
25. }
26. }
27.
28. printf("\n");
29. space--;
30. count = 1;
31. }
32. return 0;
33. }

Pattern:
1
232
34543
4567654
567898765

C program:

1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, row, num = 1, space;
6.
7. scanf("%d", &n);
8.
9. space = n - 1;
10.
11. for (row = 1; row <= n; row++)
12. {
13. num = row;
14.
15. for (c = 1; c <= space; c++)
16. printf(" ");
17.
18. space--;
19.
20. for (c = 1; c <= row; c++)
21. {
22. printf("%d", num);
23. num++;
24. }
25.
26. num = num - 2;
27.
28. for (c = 1 ; c < row; c++)
29. {
30. printf("%d", num);
31. num--;
32. }
33.
34. printf("\n");
35. }
36.
37. return 0;
38. }
Example 2: Program to print half pyramid a using
numbers
1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Source Code

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i<=rows; ++i)


{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}

Example 3: Program to print half pyramid using alphabets


A
B B

C C C

D D D D

E E E E E

Source Code

#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';

printf("Enter the uppercase character you want to print in last row: ");
scanf("%c",&input);

for(i=1; i <= (input-'A'+1); ++i)


{
for(j=1;j<=i;++j)
{
printf("%c", alphabet);
}
++alphabet;

printf("\n");
}
return 0;
}

Programs to print inverted half pyramid using * and


numbers
Example 4: Inverted half pyramid using *
* * * * *

* * * *

* * *

* *

Source Code

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=rows; i>=1; --i)


{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}

return 0;
}

Example 5: Inverted half pyramid using numbers


1 2 3 4 5
1 2 3 4

1 2 3

1 2

Source Code

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=rows; i>=1; --i)


{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}

return 0;
}

Programs to display pyramid and inverted pyramid


using * and digits

Example 6: Program to print full pyramid using *


*
* * *

* * * * *

* * * * * * *

* * * * * * * * *

Source Code

#include <stdio.h>
int main()
{
int i, space, rows, k=0;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i<=rows; ++i, k=0)


{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}

while(k != 2*i-1)
{
printf("* ");
++k;
}

printf("\n");
}

return 0;
}
Example 7: Program to print pyramid using numbers

2 3 2

3 4 5 4 3

4 5 6 7 6 5 4

5 6 7 8 9 8 7 6 5

Source Code

#include <stdio.h>
int main()
{
int i, space, rows, k=0, count = 0, count1 = 0;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i<=rows; ++i)


{
for(space=1; space <= rows-i; ++space)
{
printf(" ");
++count;
}

while(k != 2*i-1)
{
if (count <= rows-1)
{
printf("%d ", i+k);
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1 = count = k = 0;

printf("\n");
}
return 0;
}

Example 8: Inverted full pyramid using *

* * * * * * * * *

* * * * * * *

* * * * *

* * *

Source Code

#include<stdio.h>
int main()
{
int rows, i, j, space;

printf("Enter number of rows: ");


scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(space=0; space < rows-i; ++space)
printf(" ");

for(j=i; j <= 2*i-1; ++j)


printf("* ");

for(j=0; j < i-1; ++j)


printf("* ");

printf("\n");
}

return 0;
}

Example 9: Print Pascal's triangle

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

Source Code

#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=0; i<rows; i++)


{
for(space=1; space <= rows-i; space++)
printf(" ");

for(j=0; j <= i; j++)


{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;

printf("%4d", coef);
}
printf("\n");
}

return 0;
}

Example 10: Print Floyd's Triangle.


1

2 3

4 5 6

7 8 9 10
Source Code

#include <stdio.h>
int main()
{
int rows, i, j, number= 1;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i <= rows; i++)


{
for(j=1; j <= i; ++j)
{
printf("%d ", number);
++number;
}

printf("\n");
}

return 0;
}

Diamond pattern in C language: This code prints diamond pattern of stars. The diamond
shape is as follows:

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

C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, k, space = 1;
6.
7. printf("Enter number of rows\n");
8. scanf("%d", &n);
9.
10. space = n - 1;
11.
12. for (k = 1; k <= n; k++)
13. {
14. for (c = 1; c <= space; c++)
15. printf(" ");
16.
17. space--;
18.
19. for (c = 1; c <= 2*k-1; c++)
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. space = 1;
26.
27. for (k = 1; k <= n - 1; k++)
28. {
29. for (c = 1; c <= space; c++)
30. printf(" ");
31.
32. space++;
33.
34. for (c = 1 ; c <= 2*(n-k)-1; c++)
35. printf("*");
36.
37. printf("\n");
38. }
39.
40. return 0;
41. }

C program to print diamond using recursion


1. #include <stdio.h>
2.
3. void print (int);
4.
5. int main () {
6. int rows;
7.
8. scanf("%d", &rows);
9.
10. print(rows);
11.
12. return 0;
13. }
14.
15. void print (int r) {
16. int c, space;
17. static int stars = -1;
18.
19. if (r <= 0)
20. return;
21.
22. space = r - 1;
23. stars += 2;
24.
25. for (c = 0; c < space; c++)
26. printf(" ");
27.
28. for (c = 0; c < stars; c++)
29. printf("*");
30.
31. printf("\n");
32.
33. print(--r);
34.
35. space = r + 1;
36. stars -= 2;
37.
38. for (c = 0; c < space; c++)
39. printf(" ");
40.
41. for (c = 0; c < stars; c++)
42. printf("*");
43.
44. printf("\n");
45. }
Arrays are structures that hold multiple variables of the same
data type. The first element in the array is numbered 0, so the
last element is 1 less than the size of the array. An array is also
known as a subscripted variable. Before using an array its type
and dimension must be declared.
1. Array Declaration
Like other variables an array needs to be declared so that the
compiler will know what kind of an array and how large an
array we want.
int marks[30] ;
Here, int specifies the type of the variable, just as it does with
ordinary variables and the word marks specifies the name of
the variable. The [30] however is new. The number 30 tells how
many elements of the type int will be in our array. This number
is often called the "dimension" of the array. The bracket ( [ ]
) tells the compiler that we are dealing with an array.
Let us now see how to initialize an array while declaring it.
Following are a few examples that demonstrate this.
int num[6] = { 2, 4, 12, 5, 45, 5 } ;
int n[] = { 2, 4, 12, 5, 45, 5 } ;
float press[] = { 12.3, 34.2 -23.4, -11.3 } ;
2. Accessing Elements of an Array
Once an array is declared, let us see how individual elements in
the array can be referred. This is done with subscript, the
number in the brackets following the array name. This number
specifies the element’s position in the array. All the array
elements are numbered, starting with 0. Thus, marks [2] is not
the second element of the array, but the third.
int valueOfThirdElement = marks[2];
3. Entering Data into an Array
Here is the section of code that places data into an array:
for(i = 0;i <= 29;i++)
{
printf("\nEnter marks ");
scanf("%d", &marks[i]);
}
The for loop causes the process of asking for and receiving a
student’s marks from the user to be repeated 30 times. The
first time through the loop, i has a value 0, so
the scanf() function will cause the value typed to be stored in
the array element marks[0], the first element of the array. This
process will be repeated until ibecomes 29. This is last time
through the loop, which is a good thing, because there is no
array element like marks[30].
In scanf() function, we have used the "address of" operator (&)
on the element marks[i] of the array. In so doing, we are
passing the address of this particular array element to
the scanf() function, rather than its value; which is
what scanf()requires.
4. Reading Data from an Array
The balance of the program reads the data back out of the
array and uses it to calculate the average. The for loop is much
the same, but now the body of the loop causes each student’s
marks to be added to a running total stored in a variable called
sum. When all the marks have been added up, the result is
divided by 30, the number of students, to get the average.
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ;
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
5. Advantages of using an Array
 Similar data types can be grouped together under one
name.
for ex.- int a=1;int b=2;int c=3;int d=4;int e=5; could be
represented as- int X[5]={1,2,3,4,5};
 It allows random accessing of elements using indices which
saves a huge time.
 It can be used to implement other data structures like-
stack,queues etc.
 We can represent 2-D matrices using array.
6. Disadvantages of using an Array
 It is mandatory to determine the size of array to store the
elements in it.
 As array elements are stored at consecutive memory
locations,so insertion and deletion of an element is
difficult/time consuming.
 There is a certain chance of memory wastage/shortage.
 Array size is static in nature so size of array cannot be
altered.
7. Multidimensional arrays
The simplest form of multidimensional array is the two-
dimensional array. A two-dimensional array is, in essence, a list
of one-dimensional arrays. To declare a two-dimensional
integer array of size [x][y], you would write something as
follows −
type arrayName [x][y];
Multidimensional arrays may be initialized by specifying
bracketed values for each row. Following is an array with 3
rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are
optional. The following initialization is equivalent to the
previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
8. Example
8.1 Simple Array
Let us try to write a program to find average marks obtained by
a
class of 30 students in a test.
#include<stdio.h>
main()
{
int avg, i, sum=0;
int marks[30] ; /*array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}
8.2 Two-Dimensional Array
#include <stdio.h>
main () {

/* an array with 5 rows and 2 columns*/


int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;

/* output each array element's value */


for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}

return 0;
}

*******************************************************
Statement - Copy element of one array into another

*******************************************************/

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main()
{
int arr1[30], arr2[30], i, num;
clrscr();

printf("\nEnter no of elements :");


scanf("%d", &num);

//Accepting values into Array


printf("\nEnter the values :");
for (i = 0; i < num; i++)
{
scanf("%d", &arr1[i]);
}

//Copying data from array 'a' to array 'b'


for (i = 0; i < num; i++)
{
arr2[i] = arr1[i];
}

//Printing of all elements of array


printf("The copied array is :");
for (i = 0; i < num; i++)
{
printf("\narr2[%d] = %d", i, arr2[i]);
}

getch();
}

S-ar putea să vă placă și