Quantcast
Channel: Codeforwin
Viewing all 132 articles
Browse latest View live

Even number pattern 2 in C

$
0
0
Write a C program to print the given even number pattern using loop. How to print the given even number pattern using for loop in C programming. Logic to print the given number pattern in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming , Loop

Logic to print the given even number pattern

To get the logic of the pattern have a close look on to the pattern. You can observe following things about the pattern.
  1. The pattern consists of total N rows (where N is the total number of rows to be printed).
  2. To make things little easier let's divide the pattern in two internal parts.
  3. Loop formation to print the first part of the pattern will be for(j=2; j<=i*2; i+=2).
  4. Loop formation to print the second part of the pattern will be for(j=(i-1)*2; j>=2; j-=2).
Let's combine the logic of both parts in single program.

Program to print the given even number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
// Prints first part of the pattern
for(j=2; j<=i*2; j+=2)
{
printf("%d", j);
}

// Prints second part of the pattern
for(j=(i-1)*2; j>=2; j-=2)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
2
242
24642
2468642
2468108642


Screenshot

Number pattern in C


Once you get the logic of above pattern you can easily print the second pattern. You just need to add extra space before printing the numbers.

Program to print the above pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
// Prints space
for(j=i; j<N; j++)
{
printf("");
}

// Prints first part of the pattern
for(j=2; j<=i*2; j+=2)
{
printf("%d", j);
}

// Prints second part of the pattern
for(j=(i-1)*2; j>=2; j-=2)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Screenshot

Number pattern in C


Happy coding ;)



Number pattern 40 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given number pattern using loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

On first look the pattern may seem little difficult to print out. Lets make things little easier and dissect the given pattern in two internal parts. Between these two patterns spaces are printed in decreasing order i.e first row contains 8, second row contains 6 spaces and so on last row contains 0 spaces. Loop formation to print spaces will be for(j=i*2; j<N*2; j++).
Loop to print the above two pattern will be similar and simple. It has also been discussed in one of my earlier post. Lets combine everything in single program.

Program to print the given pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
// Prints first part of pattern
for(j=1; j<=i; j++)
{
printf("%d", j);
}

// Prints spaces between two parts
for(j=i*2; j<N*2; j++)
{
printf("");
}

// Prints second part of the pattern
for(j=i; j>=1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5

1 1
12 21
123 321
1234 4321
1234554321


Screenshot

Number pattern program in C


Happy coding ;)


Number pattern 41 in C

$
0
0
Write a C program to print the given number pattern using for loop. How to print the given triangular number pattern using loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

To get the logic of the pattern have a close look to the pattern for a minute. You might notice following things about the pattern.
  1. It contains N rows (where N is the total number of rows to be printed).
  2. Each row contains exactly i columns (where i is the current row number).
  3. For all odd rows it contains numbers in increasing order from 1 to i. Hence the inner loop formation to print the odd rows will be for(j=1; j<=i; j++).
  4. For all even rows it contains numbers in decreasing order from i to 1. The inner loop formation to print the even rows will be for(j=i; j>=1; j--).
Based on the above observations lets code down the solution of the pattern.

Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
if(i & 1)
{
// Prints numbers for odd row
for(j=1; j<=i; j++)
{
printf("%d", j);
}
}
else
{
// Prints numbers for even row
for(j=i; j>=1; j--)
{
printf("%d", j);
}
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
21
123
4321
12345


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 42 in C

$
0
0
Write a C program to print the given number pattern using for loop. How to print the given number pattern using loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

The above pattern might seem confusing at first go. But trust me solving this will definitely boost your logical and reasoning thinking ability. Take your time and observe the logic of the pattern. If its getting difficult to solve the pattern let me help you to solve the pattern. You can easily notice following things about the pattern.
  1. It contains total N rows (where N is the total number of rows to be printed).
  2. Each row contains total i number of columns (where i is the current row number).
  3. Now when you look to the number of each row they are in special format. Number of each row starts with the current row number i.e. i. And they continue to increment in special format. The differences between columns are 4, 3, 2, 1. i.e. If the first term of column is 5 then next will be 5+4=9, 9+3=12, 12+2=14 and so on...
Let's code down the solution.

Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, diff, value, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
diff = N-1; // Initialize difference to total rows - 1
value = i; // Initialize value to the current row number
for(j=1; j<=i; j++)
{
printf("%-3d", value);

value += diff; // Computes the next value to be printed
diff--; // Decrements the difference
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
2  6
3  7  10
4  8  11 13
5  9  12 14 15


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 43 in C

$
0
0
Write a C program to print the given number pattern using for loop. How to print the given number pattern using loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

Logic to print the given pattern might look complicated at first sight. Take your time and think about any special pattern about the given number series. You might notice following interesting things about the pattern.
  1. It contains total N rows (where N is the total number of rows to be printed).
  2. Each row contains exactly i columns (where i is the current row number).
  3. If you notice about the integers printed in each column. They are in increasing order with increasing difference. The difference between first two integers is 1 followed by a difference of 2, 3, 4, 5 and so on.
Based on the above observations lets code down solution of the given pattern.

Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, diff, value, N;

printf("Enter rows: ");
scanf("%d", &N);

diff = 1;
value = 1;

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
printf("%-3d", value);

value += diff; // Computes the next value to be printed
diff++; // Increments the difference
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
2  4
7  11 16
22 29 37 46
56 67 79 92 106


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 44 in C

$
0
0
Write a C program to print the given triangular number pattern using for loop. How to print the given number pattern using loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

To get the logic of the pattern given above have a close look to the pattern for a minute. You can easily note following points about the pattern.
  1. The pattern contains N rows (where N is the total number of rows to be printed).
  2. Each row contains exactly i columns (where i is the current row number).
  3. Now, talking about the numbers printed. They are printed in a special order i.e. for each odd row numbers are printed in ascending order and for even rows they are printed in descending order.
  4. For odd row first column value start at total_numbers_printed + 1 (where total_numbers_printed is the total numbers printed till current row, column).
  5. For even row first column value start at total_numbers_printed + i.
Based on the above logic lets write down the code to print the pattern.

Program to print the given pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, count, value, N;

printf("Enter rows: ");
scanf("%d", &N);

count = 0;

for(i=1; i<=N; i++)
{
// Starting value of column based on even or odd row.
value = (i & 1) ? (count + 1) : (count + i);

for(j=1; j<=i; j++)
{
printf("%-3d", value);

// Increment the value for odd rows
if(i & 1)
value++;
else
value--;

count++;
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
3  2
4  5  6
10 9  8  7
11 12 13 14 15


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 45 in C

$
0
0
Write a C program to print the given triangular number pattern using for loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

The given pattern is very much similar to one of previous explained pattern with little trick. To get the logic of this pattern take a moment and think. If still its difficult to get the logic. Read the below observations about the pattern.
  1. It contains total N rows (where N is the total number of rows to be printed).
  2. Each row contains total i columns (where i is the current row number).
  3. For each row less than N / 2 current row number i.e. i gets printed.
  4. For each row greater than N / 2 a value N - i + 1 gets printed.
Lets, write down code for the given pattern.

Program to print the given pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(i <= (N/2))
{
printf("%d", i);
}
else
{
printf("%d", (N - i + 1));
}
}

printf("\n");
}

return 0;
}


Output
Enter rows: 5
1
22
333
2222
11111


Screenshot

Number pattern in C


Happy coding ;)


C program to count number of digits in an integer

$
0
0
Write a C program to read an integer from user and count total number of digits the integer contains. How to find total number of digits in a given integer using loop in C programming. Logic to count number of digits in a given integer using C program.

Example:
Input num: 35419
Output digits: 5

Required knowledge

Basic C programming, Loop

Logic to count number of digits in an integer

There are plenty of ways through which you can count the number of digits in a given integer. Here we will be looking two of the methods through which we can find the number of digits in an integer.
First is the easiest method to count number of digits in an integer.
  1. Initialize a variable count to 0.
  2. Increment count by 1 if num > 0 (where num is the number whose digits count is to be found).
  3. Remove the last digit from the number as it isn't needed anymore. Hence divide the number by 10 i.e num = num / 10.
  4. If number is greater than 0 then repeat steps 2-4.


Program to count number of digits in an integer


/**
* C program to count number of digits in an integer
*/

#include <stdio.h>

int main()
{
long long num;
int count = 0;

printf("Enter any number: ");
scanf("%lld", &num);

while(num != 0)
{
count++;
num /= 10; // num = num / 10
}

printf("Total digits: %d", count);

return 0;
}


Now, lets take a look to the second method of finding total number of digits in a given integer using mathematical approach. This method is more simpler and shorter to use. Before using this method you must have a good knowledge of logarithms.


Program to find number of digits in an integer


/**
* C program to count number of digits in an integer
*/

#include <stdio.h>
#include <math.h>

int main()
{
long long num;
int count = 0;

printf("Enter any number: ");
scanf("%lld", &num);

count = log10(num) + 1;

printf("Total digits: %d", count);

return 0;
}


Output
Enter any number: 123456789
Total digits: 9


Happy coding ;)



Number pattern 46 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern series using for loop in C programming. Logic to print the given number pattern using C program.

Example:
Input any number: 22464
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

Printing these type of patterns are very simple. Logic to print the pattern is very simple, and is described below.
  1. Print the value of num (where num is the number entered by user).
  2. Trim the last digit of num by dividing it from 10 i.e. num = num / 10.
  3. Repeat step 1-3 till the number is not equal to 0.


Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int num;

printf("Enter any number: ");
scanf("%d", &num);

while(num != 0)
{
printf("%d\n", num);
num = num / 10;
}

return 0;
}


Output
Enter any number: 12345
12345
1234
123
12
1


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 47 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern series using for loop in C programming. Logic to print the given number pattern using C program.

Example:
Input any number: 24165
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

The given number pattern is similar to the previous number pattern. Also logic of this pattern would be very much similar to the previous pattern.
Before I discuss the logic of this program you must know how to find first digits of any number and how to count digits in a given number. Logic to print the given pattern is.
  1. Print the value of num (where num is the number entered by user).
  2. Remove the first digit of the number, as it isn't needed anymore.
  3. Repeat steps 1-3 till the number is greater than 0.


Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>
#include <math.h>

int main()
{
int num, firstDigit, digits, placeValue;

printf("Enter any number: ");
scanf("%d", &num);

while(num > 0)
{
printf("%d\n", num);

digits = (int) log10(num); //Gets total number of digits
placeValue = (int) ceil(pow(10, digits)); //Gets the place value of first digit
firstDigit = (int)(num / placeValue); //Gets the first digit

num = num - (placeValue * firstDigit); //Remove the first digit from number
}

return 0;
}


Output
Enter any number: 24165
24165
4165
165
65
5


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 48 in C

$
0
0
Write a C program to print the given triangular number pattern using for loop. How to print the given number pattern series using for loop in C programming. Logic to print the given number pattern in C program.

Example:
Input N:
Output:

Required knowledge

Basic C programming, Loop

Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
for(j=i; j<=(i*i); j += i)
{
printf("%-3d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
2  4
3  6  9
4  8  12 16
5  10 15 20 25


Screenshot

Number pattern in C


Happy coding ;)


C program to print plus star pattern

$
0
0
Write a C program to print plus star pattern series using for loop. How to print plus star pattern series using loop in C programming. Logic to print plus star pattern in C program.

Example:
Input N: 5


Required knowledge

Basic C programming, If else, Loop

Logic to print plus star pattern

Before you write or think about logic of the pattern. Take a close look about the pattern and identify some noticeable things about it. Here are some.
  1. The pattern consists of N * 2 - 1 rows (where N is the value enter by user).
  2. When you look to the center horizontal plus line i.e. +++++++++ this line. It also consists of N * 2 - 1 columns.
  3. For every other row, single plus symbol gets printed after N - 1 blank spaces, for this case it is 4.
Based on the above observation we can easily code down the solution.

Program to print plus star pattern series


/**
* C program to print the plus star pattern series
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Run an outer loop from 1 to N*2-1
for(i=1; i<=(N * 2 - 1); i++)
{
// For the center horizontal plus
if(i == N)
{
for(j=1; j<=(N * 2 - 1); j++)
{
printf("+");
}
}
else
{
// For spaces before single plus sign
for(j=1; j<=N-1; j++)
{
printf("");
}
printf("+");
}

printf("\n");
}

return 0;
}


Output
Enter N: 5

+
+
+
+
+++++++++
+
+
+
+


Snapshot

Plus star pattern in C


Happy coding ;)


C program to print X star pattern

$
0
0
Write a C program to print X star pattern series using loop. How to print the X star pattern series using for loop in C programming. Logic to print X using stars in C program.

Example:
Input N: 5
Output:

Required knowledge:

Basic C programming, If else, Loop

Logic to print X star pattern

Logic to print the X star pattern can really be complex and confusing if you haven't gone through two previous star pattern programs.
C program to print hollow pyramid star pattern.
C program to print hollow inverted pyramid star pattern.

If you bisect the pattern horizontally you will get two pattern.

X star pattern is a combination of both of these patterns. Where the first one is similar to the hollow inverted pyramid and second one to hollow pyramid star pattern. Hence if you haven't gone through these two posts I recommend you to go through them before continuing. As I won't be getting in detail about these two patterns here.

Program to print X star pattern


/**
* C program to print X star pattern series
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Print first upper hollow inverted pyramid
for(i=N; i>=1; i--)
{
//Prints trailing spaces
for(j=i; j<N; j++)
{
printf("");
}

//Prints upper hollow triangular part
for(j=1; j<=(2*i-1); j++)
{
if(j==1 || j==(2*i-1))
{
printf("*");
}
else
{
printf("");
}
}

printf("\n");
}

// Prints the lower half hollow pyramid
for(i=2; i<=N; i++)
{
//Prints trailing spaces
for(j=i; j<N; j++)
{
printf("");
}

//Prints lower hollow triangular part
for(j=1; j<=(2*i-1); j++)
{
if(j==1 || j==(2*i-1))
{
printf("*");
}
else
{
printf("");
}
}

printf("\n");
}

return 0;
}


Output
Enter N: 5

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


Snapshot

X star pattern in C


Happy coding ;)


Number pattern 50 in C

$
0
0
Write a C program to print the given number pattern series using loop. How to print the given number pattern series using for loop in C programming. Logic to print the given number pattern series using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print number pattern 1

The above number pattern is a result of combination of two patterns together. Where the two parts individually look as

The above two patterns are explained in one of my previous number pattern post. Please go through the link to get the detailed explanation about these two patterns individually as combination of these two yields the final pattern.

For getting the final resultant pattern we need two separate loop that will print the first and second half of the pattern individually. For print the first upper half of the pattern here goes the logic.
  1. It consists of N rows (where N is the total number of rows to be printed). Hence the loop formation to iterate through rows will be for(i=1; i<=N; i++).
  2. Each row contains exactly i * 2 - 1 columns (where i is the current row number). Loop formation to iterate through each column will be for(j=1; j<=(i * 2 - 1); j++). For each column the current value of j is printed.
Once you coded that, you need to code another loop to print the second lower half of the pattern. Logic to print the other half of the pattern is.
  1. The second half pattern consists of N - 1 rows. Since the pattern goes in descending order hence the loop formation to iterate through rows will also go in descending order for(i=N-1; i>=1; i--).
  2. Here each row exactly contains i * 2 - 1 columns. Hence, the loop formation for iterating over columns is for(j=1; j<=(i * 2 - 1); j++). Inside the inner loop print the value of j.
Lets, write it down in a C program.

Program to print number pattern 1


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Iterate through upper half triangle of the pattern
for(i=1; i<=N; i++)
{
for(j=1; j<=(i * 2 - 1); j++)
{
printf("%d", j);
}

printf("\n");
}

// Iterate through lower half triangle of the pattern
for(i=N-1; i>=1; i--)
{
for(j=1; j<=(i * 2 - 1); j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
123
12345
1234567
123456789
1234567
12345
123
1


Screenshot 1

Number pattern in C


Logic to print the number pattern 2

Once you printed the above pattern you can easily print the second number pattern. It is exactly similar to the first pattern we just printed. The only thing we need to add here is the trailing spaces. To print trailing spaces you need the following loop formation for(j=(i * 2); j<(N * 2); j++).

Program to print the given number pattern 1


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Iterate through upper half triangle of the pattern
for(i=1; i<=N; i++)
{
// Print trailing spaces
for(j=(i * 2); j<(N * 2); j++)
{
printf("");
}

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

printf("\n");
}

// Iterate through lower half triangle of the pattern
for(i=N-1; i>=1; i--)
{
// Print trailing spaces
for(j=(i * 2); j<(N * 2); j++)
{
printf("");
}

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

printf("\n");
}

return 0;
}


Screenshot

Number pattern in C


Happy coding ;)


Number pattern 49 in C

$
0
0
Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

These types of patterns are basically a combination of two or more number patterns. Here if you look carefully then you can easily notice that the given pattern is a combination of two patterns. Hence we can divide the pattern in two parts to make our task easier. Now if you are following my previous posts, you might have noticed that these two patterns are explained in my previous post - Ascending order number pattern and descending order number pattern.
I recommend you to go though these two post before you get into this pattern.

Printing these two parts are really simple. Logic to print the first part.
  1. First upper part of the patter consists of N rows (where N is the total number of rows to be printed). Since the pattern is in ascending order hence the outer loop formation to iterate through rows will also be in ascending order i.e. for(i=1; i<=N; i++).
  2. Here each row contains exactly i columns (where i is the current row number). Inner loop formation to iterate through each columns is for(j=1; j<=i; j++). Print the current value of j inside this loop, to get the first part of the pattern.

Logic to print the second part of the pattern.
  1. Second lower part of the pattern consists of N - 1 rows. Run an outer loop to iterate through rows. Since the pattern is in descending order hence we need to run the loop in descending order. Therefore the outer loop formation will be for(i=N-1; j>=1; i--).
  2. Each row contains exactly i number of columns. Hence the inner loop formation to print columns will be for(j=1; j<=i; j++). To get the desired pattern print the current value of j inside this loop.
Lets, now combine the logic of both the parts in a single program.

Program to print the given number pattern 1


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Prints upper part of the pattern
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
printf("%d", j);
}

printf("\n");
}

// Print lower part of the pattern
for(i=N-1; i>=1; i--)
{
for(j=1; j<=i; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
12
123
1234
12345
1234
123
12
1


Screenshot

Number pattern in C


Logic to print the given number pattern 2

This pattern is exactly similar to the above pattern we just need to add spaces before the number gets printed. Here I am not getting into detailed explanation about the logic of this pattern. As we only need to add the code to print spaces.

Program to print the given number pattern 2


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Prints upper part of the pattern
for(i=1; i<=N; i++)
{
// Prints trailing spaces
for(j=N-1; j>=i; j--)
{
printf("");
}

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

printf("\n");
}

// Prints lower part of the pattern
for(i=N-1; i>=1; i--)
{
// Prints trailing spaces
for(j=i; j<N; j++)
{
printf("");
}

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

printf("\n");
}

return 0;
}


Screenshot 2

Number pattern program in C


If you really love programming, try these interesting pattern of your own, based on the logic of above pattern. You just need to play with the spaces.
And many more...

Happy coding ;)



Triangle number pattern using 0, 1 in C - 1

$
0
0
Write a C program to print the given triangle number pattern using 0, 1. How to print triangle number pattern with 0, 1 using for loop in C programming. Logic to print the given triangular number pattern using 0, 1 in C programming.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, If else, Loop, Even odd logic

Logic to print the given number pattern

To print these type of patterns, the main thing you need to get is the loop formation to iterate over rows and columns. Before I discuss the logic to print the given number pattern. Have a close eye to the pattern. Below is the logic to print the given number pattern.
  1. The pattern consists of total N number of rows (where N is the total number of rows to be printed). Therefore the loop formation to iterate through rows will be for(i=1; i<=N; i++).
  2. Here each row contains exactly i number of columns (where i is the current row number). Hence the inner loop formation to iterate through each columns will be for(j=1; j<=i; j++).
  3. Once you are done with the loop formation to iterate through rows and columns. You need to print the 0's and 1's. Notice that here for each odd columns 1 gets printed and for every even columns 0 gets printed. Hence you need to check a simple condition if(j % 2 == 1) before printing 1s or 0s.
Lets, now code it down.

Program to print the given number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
// For every odd column print 1
if(j % 2 == 1)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}

return 0;

}


Output
Enter N: 5
1
10
101
1010
10101


Instead of using if else you can also print the pattern using a simple but tricky method. Below is a tricky approach to print the given number pattern without using if else. Below program uses bitwise operator to check even odd, learn how to check even odd using bitwise operator.



/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

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

printf("\n");
}

return 0;
}


Screenshot

Triangle number pattern in C


NOTE: You can also get the below pattern with the same logic What you need to do is. Swap the two print() statements. Replace the printf("1"); with printf("0"); and vice versa.


Happy coding ;)


Triangle number pattern using 0, 1 in C - 2

$
0
0
Write a C program to print the given triangle number pattern using 0, 1. How to print the given triangle number pattern with 0, 1 using for loop in C programming. Logic to print the given triangle number pattern with 0, 1 in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, If else, Loop, Even odd logic

Logic to print the given triangle number pattern

  1. The pattern consists of N rows (where N is the total number of rows to be printed). Hence the outer loop formation to iterate through rows will be for(i=1; i<=N; i++).
  2. Each row contains exactly i number of columns (where i is the current row number) i.e. first row contains 1 column, 2 row contains 2 column and so on.
    Hence the inner loop formation to iterate through columns will be for(j=1; j<=i; j++).
  3. Once you are done with the loop semantics, you just need to print the number. If you notice the numbers are in special order. For each odd rows 1 gets printed and for each even rows 0 gets printed. Now to implement this we need to check a simple condition if(i % 2 == 1) before printing 1s or 0s.
Lets, now implement this in C program.

Program to print given triangle number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
// Print 1s for every odd rows
if(i % 2 == 1)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
00
111
0000
11111


You can also print this pattern without using if else. Below is a tricky approach to print the given number pattern without using if else. It uses bitwise operator to check even odd.



/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

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

printf("\n");
}

return 0;
}


Screenshot

Triangle number pattern in c


NOTE: You can also get the below pattern with the same logic What you need to do is. Swap the two print() statements. Replace the printf("1"); with printf("0"); and vice versa.


Happy coding ;)


Triangle number pattern using 0, 1 in C - 3

$
0
0
Write a C program to print the given triangle number pattern using 0, 1. How to print the given triangle number pattern with 0, 1 using for loop in C programming. Logic to print the given triangle number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, If else, Loop, Even odd logic

Logic to print the given triangle number pattern

  1. The pattern consists of N rows (where N is the total number of rows to be printed). Hence the outer loop formation to iterate through rows will be for(i=1; i<=N; i++).
  2. Here each row contains exactly i number of columns (where i is the current row number). Loop formation to iterate through columns will be for(j=1; j<=i; j++).

  3. Once you are done with the loop formation. Have a closer look to the pattern. If you view the pattern as you can easily print the final pattern. Where for each odd integers 1 gets printed and for each even integers 0 gets printed. Therefore to get such pattern we need to check a simple condition if(k % 2 == 1) before printing 1s or 0s (where k represents the current integer.
Lets, code down the solution in C program.

Program to print the given triangle number pattern


/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

printf("Enter N: ");
scanf("%d", &N);

// k represents the current integer
k = 1;

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
// Print 1 if current integer k is odd
if(k % 2 == 1)
{
printf("1");
}
else
{
printf("0");
}

k++;
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
01
010
1010
10101


You can also print the given number pattern without using if else. Below is a simple but tricky program to print the same number pattern. It uses bitwise operator to check even odd.



/**
* C program to print the given number pattern
*/

#include <stdio.h>

int main()
{
int i, j, k, N;

printf("Enter N: ");
scanf("%d", &N);

// k represents the current integer
k = 1;

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
printf("%d", (k & 1));

k++;
}

printf("\n");
}

return 0;
}


Screenshot

Triangle number pattern in C


NOTE: You can also obtain the reverse of the given pattern You just need to swap the two printf() statements. Means just replace the printf("1"); with printf("0");.


Happy coding ;)


Triangle number pattern using 0,1 in C - 4

$
0
0
Write a C program to print the given triangle number pattern using 0, 1. How to print the given triangle number pattern with 0, 1 using for loop in C programming. Logic to print the given triangle number pattern using C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, If else, Loop

Logic to print the given number pattern

If you are going through my previous number pattern posts, then I hope that logic of this wouldn't be difficult. If still its difficult for you to get the logic. Then, read it below else continue to the program.
  1. The pattern consists of N rows (where N is the number of rows to be printed). Outer loop formation to iterate through the rows will be for(i=1; i<=N; i++).
  2. Each row contains exactly i columns (where i is the current row number). Hence the loop formation to iterate though individual columns will be for(j=1; j<=i; j++).
  3. Now comes the logic to print 0 or 1. You can see that 1 only gets printed for first and last column or first and last row otherwise 0 gets printed. Therefore you must check a condition that if(i==1 || i==N || j==1 || j==i) then print 1 otherwise print 0.


Program to print the given number pattern


/**
* C program to print triangle 0, 1 number pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(i==1 || i==N || j==1 || j==i)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}

return 0;
}


Output
Enter N: 5
1
11
101
1001
11111


Screenshot

Number pattern program in C


Happy coding ;)


C program to count frequency of digits in an integer

$
0
0
Write a C program to count frequency of digits in an integer. How to find frequency of digits in a given number using loop in C programming. Logic to find total occurrences of each digits in a given number in C program.

Example:
Input num: 116540
Output:
Frequency of 0 = 1
Frequency of 1 = 2
Frequency of 2 = 0
Frequency of 3 = 0
Frequency of 4 = 1
Frequency of 5 = 1
Frequency of 6 = 1
Frequency of 7 = 0
Frequency of 8 = 0
Frequency of 9 = 0

Required knowledge

Basic C programming, Loop, Array

Logic to find frequency of digits in a number

Before I begin to explain the logic of counting frequency of digits in a given integer. You must have a good understanding of below three concepts -
How to find last digit of any number in C
How to reverse numbers in C
How to store and retrieve elements from an array in C

Here goes the step-by-step detailed explantion about how you gonna find the frequency of digits in an integer.
  1. First of all we need a storage where we can store frequencies of each digits. For that we will be using an integer array of size 10 call it as freq[10]. We have used an array of size 10 because decimal has base 10. There are only 10 digits that makes up any decimal number.
  2. Next, we need to initialize every element of the array with 0. Assuming that every digit has occurred 0 times.

  3. Now comes the main logic. Find the last digit of the given number. For that we need to perform modular division by 10 i.e. lastDigit = num % 10 (where num is the number whose frequency of digits is to be found).
  4. Increment the freq[ lastDigit ]++ by one.
  5. Now remove the last digit from the num as it isn't needed anymore. Perform num = num / 10.
  6. Repeat steps 3-5 till num != 0. Finally you will be left with an array freq having the frequency of each digits in that number.
Lets, now implement this on code.

Program to count frequency of digits in an integer


/**
* C program to count frequency of digits in a given number
*/

#include <stdio.h>
#define BASE 10

int main()
{
long long num, n;
int i, lastDigit;
int freq[BASE];

printf("Enter any number: ");
scanf("%lld", &num);

// Initializes frequency array with 0
for(i=0; i<BASE; i++)
{
freq[i] = 0;
}

n = num; //Copies the value of num to n

while(n != 0)
{
// Gets the last digit
lastDigit = n % 10;

// Increments the frequency array
freq[lastDigit]++;

// Removes the last digit from n
n /= 10;
}

printf("Frequency of each digit in %lld is: \n", num);
for(i=0; i<BASE; i++)
{
printf("Frequency of %d = %d\n", i, freq[i]);
}

return 0;
}


Do not confuse with n /= 10. It is same as n = n / 10.


Output
Enter any number: 11203458760011
Frequency of each digit in 11203458760011 is:
Frequency of 0 = 3
Frequency of 1 = 4
Frequency of 2 = 1
Frequency of 3 = 1
Frequency of 4 = 1
Frequency of 5 = 1
Frequency of 6 = 1
Frequency of 7 = 1
Frequency of 8 = 1
Frequency of 9 = 0

Happy coding ;)


Viewing all 132 articles
Browse latest View live