Pattern Printing – 16

 
 


#include <stdio.h>

void main()
{
    int numRows = 10;
    int row, col;
    int count = 1;
    for (row = 1; row <= numRows; row++)
    {
        count = row;
        for (col = 1; col <= row; col++)
        {

            int remainder = (count) % 2;
            count++;
            printf("%d ", remainder);

        }
        printf("\n");

    }

}

Pattern Printing 15

Figure 1

To understand and compute above type of pattern we follow the following steps:

  1. We will divide the pattern into two parts:
  2. Now the two parts of the pattern are solved in the following way:- As you can see, the upper part seems like combination of three different patterns that we have studied (marked with three different colors)

    - Solving these three parts individually and analyzing it, we can see that the outer loop in all the three patterns is same.

    
    // n in the user input
    
    // for blue region
    for (i = n; i >= 1; i--)
    {
        for (j = n; j >= i; j--)
            printf("%d", j);
        printf("\n");
    }
    /* -----------------------------------*/
    // for brown region
    for (i = n; i >= 1; i--)
    {
        // loop to print blank spaces
        for (j = n - i; j >= 1; j--)
        	printf(" ");
    
        // loop to print pattern
        for (j = 1; j < (i * 2) - 1; j++)
            printf("%d", i);
        printf("\n");
    }
    /* -----------------------------------*/
    // for red region
    for (i = n; i >= 1; i--)
    {
       // loop to print blank spaces
       for (j = i-2; j >= 0; j--)
    	 printf(" ");
       
       // loop to print pattern
       for (j = i + 1; j <= n; j++)
             printf("%d", j);
       printf("\n");
    }
    
    
    

    Now w.r.t this common part, we can combine all the patterns (removing blank spaces loop mentioned in the program) to form upper part of our pattern.

    - Same way lower part is solved.

    - Combining both upper and lower part, we come up with final program:

    
    #include<stdio.h>
    int main()
    {
        int i, j, k, n;
        
        printf("Enter n: ");
        scanf("%d", &n); // input value i.e. 3, 4
        printf("\n");
    
        // to print upper half
        for (i = n; i >= 1; i--)
        {
            // to print blue region
            for (j = n; j >= i; j--)
                printf("%d", j);
    
            // to print brown region
            for (j = 1; j < (i * 2) - 1; j++)
                printf("%d", i);
    
            // to print red region
            for (j = i + 1; j <= n; j++)
                printf("%d", j);
    
            printf("\n");
        }
    
        // to print lower half
        for (i = 2; i <= n; i++)
        {
            // to print red region
            for (j = n; j >= i; j--)
                printf("%d", j);
    
            // to print brown region
            for (j = 1; j < (i * 2) - 1; j++)
                printf("%d", i);
    
            // to print blue region
            for (j = i + 1; j <= n; j++)
                printf("%d", j);
    
            printf("\n");
        }
        return 0;
    }
    
    /*
    Output:
    
    Enter n: 3
    
    33333
    32223
    32123
    32223
    33333
    
    */
    

Printing Pattern -14

Untitled


/*
Logic:
Firstly we take input of max value and store it in n.
For the case shown in figure n=3

We divide the program in 3 parts ie upper half , centre * and lower half

For upper half:
for each row row_num starts with 0 till n-1:
-Number of starting spaces:2*(row_num)
-print stars: *
-Number of centre spaces : 4*(n-row_num)-1
-print stars: *
-new line

//for centre *
-number of spaces:2*n
-print *
-newline

Similar to upper half lower half is printed.

We have used i for row_num
-n rows in upper half
-print *
-n rows in lower half

*/



#include<stdio.h>
int main(){
int n,i,j;
printf("Enter maximum value\n");
scanf("%d",&n);

//for upper half

for(i=0;i<n;i++){
//print starting space
for(j=0;j<2*i;j++){
printf(" ");
}

//print stars
printf("*");

// print centre spaces
for(j=0;j<4*(n-i)-1;j++)
printf(" ");

//print stars
printf("*");

printf("\n");
}

//for centre *
for(j=0;j<2*n;j++){
printf(" ");
}

printf("*");

printf("\n");

//for lower half
for(i=0;i<n;i++){
//print starting spaces
for(j=0;j<2*(n-i-1);j++){
printf(" ");
}

//print stars
printf("*");

//print centre spaces
for(j=0;j<4*(i+1)-1;j++)
printf(" ");

//print stars
printf("*");

printf("\n");
}

return 0;
}

 

C Practical and Assignment Programs – Reverse given Number

In this video we will understand how to reverse given number. We will also look into the algorithm . And then we will be writing program to reverse give numbers. In this video we will also learn how to effectively improve our coding skills using function. And also see the advantages of functions.  Now this program is written in C programming language but will work in C++. Fell free to provide you  suggestion.


#include<stdio.h>

int reverse(int number){
int ans=0;
while(number!=0){
int units=number%10;
ans=ans*10+units;
number=number/10;
}
return ans;
}

void main(){

int a[]={234,563,11,985,568,235,<wbr />456};
int size=7;
int i=0;
for(i=0;i<7;i++){
int rev;
rev=reverse(a[i]);
printf("Original number: %d  Reversed Number %d \n",a[i],rev);
}

}

Printing Pattern-12

Untitled


/*
Logic:
1.First we take input of number of rows(n).
2.For each row(row num starts from 0 till n-1):
-Number of "-" equals the row number.
-Number of "*" equals (n-row_num)
3. New Line.
*/

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

Printing Pattern – 11

Untitled


/*
Logic:
Firstly we take input of number of rows.
For each row (row num goes from 0 to n-1)
-print spaces (num of spaces = total num of rows minus row num minus 1)
-print numbers starting with row number decrementing upto 0
-print numbers starting with 1 incrementing upto row num
-newline

*/

#include<stdio.h>
int main(){
int n,i,j;
printf("Enter number of rows\n");
scanf("%d",&n);

for(i=0;i<n;i++){

//print spaces
for(j=0;j<n-i-1;j++)
printf(" ");

//print decrementing numbers
for(j=i;j>=0;j--)
printf("%d",j);

//print incrementing numbers
for(j=1;j<=i;j++)
printf("%d",j);

//newline
printf("\n");
}

return 0;
}

Pascal Triangle element

Untitled


#include<stdio.h>

//it calculates factorial of a given number

int factorial(int t){

int fact=1;

while(t>0){

fact*=t;

t--;

}

return fact;

}

&nbsp;

void main(){

printf("Enter the row number\n");

int r;

scanf("%d",&r);

printf("Enter the column number\n");

int c;

scanf("%d",&c);

r--;//since row number must start from 0

c--;//since column number must start from 0

//formula to find any any element from pascals triangle

//          nCr= n!/(r!*(n-r)!)

//where n=row number

//                      r=column number

int nCr=factorial(r)/(factorial(c)*factorial(r-c));

printf("Value of element in pascals triangle is %d \n",nCr);

}

Printing Pattern-9

Capture/*
Logic:
Firstly we take input of max value and store it in n.
For the case shown in figure n=4

We divide the program in 3 parts ie upper half , zero and lower half

For upper half:
for each row row_num starts with 0 till n-1:
-Number of starting spaces:2*(row_num)
-print number: n-row_num
-Number of centre spaces : 4*(n-row_num)-1
-print number: n-row_num
-new line

//for zero
-number of spaces:2*n
-print 0
-newline

Similar to upper half lower half is printed.

We have used i for row_num
-n rows in upper half
-print 0
-n rows in lower half

*/

#include<stdio.h>
int main(){
int n,i,j;
printf("Enter maximum value\n");
scanf("%d",&n);

//for upper half

for(i=0;i<n;i++){
//print starting space
for(j=0;j<2*i;j++){
printf(" ");
}

//print number
printf("%d",n-i);

// print centre spaces
for(j=0;j<4*(n-i)-1;j++)
printf(" ");

//print number
printf("%d",n-i);

printf("\n");
}

//for 0
for(j=0;j<2*n;j++){
printf(" ");
}

printf("0");

printf("\n");

//for lower half
for(i=0;i<n;i++){
//print starting spaces
for(j=0;j<2*(n-i-1);j++){
printf(" ");
}

//print number
printf("%d",i+1);

//print centre spaces
for(j=0;j<4*(i+1)-1;j++)
printf(" ");

//print number
printf("%d",i+1);

printf("\n");
}

return 0;
}

Printing Pattern – 8

Untitled/*
Logic:
1.Take input of number of rows.
2.create a count variable and initialize it to 1.
3.For each row
-Number of elements is equal to row number
the 1st row has 1 element 2nd has 2 and so on…
-for each element print the value of count.
-increment count.
4.new line
*/


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

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

Printing Pattern – 5

pattern   /* This pattern is similar to Pattern Printing 9 so I highly recommend you to watch it. Here is the link https://www.youtube.com/watch?v=Hpr5m_PE4As I will be using the same code */

 
 

// source code
#include
int main()
{
 int totalRows = 5;
 //colSpace used for printing Spaces & colStar for printing Star
 int row, colSpace, colStar;
 //Loop for the top triangle
 for (row = 1; row <= totalRows; row++)
 {
 //Loop for Space Printing
 for (colSpace = totalRows - row; colSpace >= 1; colSpace--)
 {
 printf(" ");
 }
 //Loop for Star printing

 for (colStar = 1; colStar <= 2 * row - 1; colStar++)
 {
 //Check if it is the first column or last column and print row number
 if (colStar == 1 || colStar == 2 * row - 1)
 printf("%d", row);
 else
 printf("*");
 }
 printf("\n");
 }
 //Loop for the bottom triangle
 for (row = totalRows - 1; row >= 1; row--)
 {
 //Loop for Space Printing
 for (colSpace = 1; colSpace <= totalRows - row; colSpace++)
 {
 printf(" ");
 }
 //Loop for Star printing
 for (colStar = 1; colStar <= 2 * row - 1; colStar++)
 {
 //Check if it is the first column or last column and print row number
 if (colStar == 1 || colStar == 2 * row - 1)
 printf("%d", row);
 else
 printf("*");
 }
 printf("\n");
 }
 return 0;

}
Follow

Get every new post delivered to your Inbox.