Advanced Number Pattern - c

Given a Number N, Print the following pattern.
Input Format
The input contains a number N
Constraints
1 < N < 100
Output Format
The required pattern
for input 5 is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
and for input 3 is
1
2 5
3 4 6
this is the code i have tried .. but the results are not the same
#include<stdio.h>
void pattern(int n)
{
for(int i=1; i<=n; i++)
{
int k = i;
for(int j=1; j<=i; j++)
{
printf("%d ",k);
k = n - j + k;
}
printf("\n");
}
}
int main()
{
int n = 5;
pattern(n);
return 0;
}
this is the result of the above code
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
How should I modify the above code to get the Expected Output?

This is an interesting problem, and not an easy one. I'm not going to write a program to solve it (in part because I'm too lazy), but I can describe how I would solve it.
You already have an outer loop for(int i=1; i<=n; i++) which counts down the rows, and an inner loop for(int j=1; j<=i; j++) which counts across the columns. Those are both fine.
Inside the inner loop I would test if(j % 2 == 1). If j % 2 is 1 we're in an odd-numbered column, and we want to count down the column. But if j % 2 is 0, we're in an even column, and we have to do it the other way.
First I would have a variable which is the number that's supposed to be at the top of the column (1, 9, 10, 14, or 15 in the n=5 case). I'd have to compute that number two different ways, one for the "odd" columns and one for the "even".
And then I'd use that number as a base to count down the odd columns, and up the evens. Specifically: I'd add i to it in the odd columns, but subtract i in the even columns. But actually that's not quite right, because i is not 1 at the top of columns other than 1, so what I'd actually have to add or subtract would be some function of i and j. But I think you can work this out.

Here are my three cents.:)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
unsigned int value = j % 2 == 0
? i + 1 + j * n - j * ( j + 1 ) / 2
: ( j + 1 ) * n - j * ( j + 1 ) / 2 - i + j;
printf( "%2u ", value );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look the following way
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 9
1
2 17
3 16 18
4 15 19 30
5 14 20 29 31
6 13 21 28 32 39
7 12 22 27 33 38 40
8 11 23 26 34 37 41 44
9 10 24 25 35 36 42 43 45
Enter a non-negative number no greater than 100 (0 - exit): 8
1
2 15
3 14 16
4 13 17 26
5 12 18 25 27
6 11 19 24 28 33
7 10 20 23 29 32 34
8 9 21 22 30 31 35 36
Enter a non-negative number no greater than 100 (0 - exit): 7
1
2 13
3 12 14
4 11 15 22
5 10 16 21 23
6 9 17 20 24 27
7 8 18 19 25 26 28
Enter a non-negative number no greater than 100 (0 - exit): 6
1
2 11
3 10 12
4 9 13 18
5 8 14 17 19
6 7 15 16 20 21
Enter a non-negative number no greater than 100 (0 - exit): 5
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
Enter a non-negative number no greater than 100 (0 - exit): 4
1
2 7
3 6 8
4 5 9 10
Enter a non-negative number no greater than 100 (0 - exit): 3
1
2 5
3 4 6
Enter a non-negative number no greater than 100 (0 - exit): 2
1
2 3
Enter a non-negative number no greater than 100 (0 - exit): 1
1
Enter a non-negative number no greater than 100 (0 - exit): 0
Or using the recursive approach of calculating the output value the program can look the following way.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j < i + 1; j++ )
{
printf( "%2u ", value );
value += j % 2 == 0 ? 2 * ( n - i ) - 1 : 2 * ( i - j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
For example its output for the entered number equal to 10 looks like
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 0

Related

Partition a Array

Given a randomly ordered array (arr) of n elements, function partitionArray(int arr[], int n, int x) partition the elements into two subsets such that elements <= x are in left subset and elements > x are in the right subset.
The first line of the test case will contain two numbers n (number of elements in a list ) and x (number to use for partition) separated by space.
The next line will contain N space-separated integers.
I am getting the wrong output for certain cases from the following Function.
Here's my code:
void partitionArray(int arr[], int n, int x)
{
int i, j, temp;
i = 0;
j = n-1;
while (i < j)
{
while (arr[i] <=x)
i++;
while (arr[j] > x)
j--;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
For the cases I am getting the right output is:
10 6
28 26 25 5 6 7 24 29 6 10
For the cases I am not getting the right output is:
10 17
28 26 25 11 16 12 24 29 6 10
The output I am getting in this:
10
6
12
11
25
16
24
29
26
28
Expected Output:
10
6
12
11
16
25
24
29
26
28
10 6
28 26 25 11 5 7 24 29 6 10
The output I am getting in this:
6
25
5
11
26
7
24
29
28
10
Expected Output:
6
5
25
11
26
7
24
29
28
10
Can Anyone help me this
Below change will do:
if(i < j){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
When swapping when the value of j is i+1 and arr[i]<x and arr[j]>x, after i++ and j-- from while loops, the value of j is i-1 in your code. Hence checking i<j before swapping is important.
Suppose input is
2 5
1 10
Your output will be
10 1
And the index has to be checked as index may run out of the size of the array.
while (i<n && arr[i]<=x)
i++;
while (j>=0 && arr[j]>x)
j--;
Example inputs:
5 7
5 3 2 4 1
5 3
7 6 9 5 6

Eratosthenes prime numbers

I have created a program to search for prime numbers. It works without problems until the entered number is smaller than 52, when it is bigger output prints out some blank (0) numbers and I don't know why. Also other numbers have blank output.
My code is:
#include <stdio.h> //Prime numbers
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
int c[100], n, a[50], d, e, b = 1;
void sort() {
for (int i = 1; i < n; i++) {
if (c[i] > 1) {
a[b] = c[i];
printf("%d %d %d\n", a[1], b, i);
b++;
e = 2;
d = 0;
while (d <= n) {
d = c[i] * e;
c[d - 1] = 0;
e++;
}
}
}
}
int main() {
printf("Enter number as an limit:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
c[i] = i + 1;
}
sort();
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 1; i < b; i++) {
printf("%d ", a[i]);
}
return 0;
}
Here is output for 25:
Enter number as an limit:
25
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
But for 83 is:
Enter number as an limit:
83
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
2 10 28
2 11 30
2 12 36
2 13 40
2 14 42
2 15 46
2 16 52
0 17 58
0 18 60
0 19 66
0 20 70
0 21 72
0 22 78
0 23 82
Prime numbers between 1 and 83 are:
0 3 5 7 11 0 17 19 23 29 31 37 0 43 47 53 0 61 67 71 73 79 83
Blank spots always spots after 17th prime number. And always the blank numbers are the same. Can you help me please what is the problem?
The loop setting entries in c for multiples of c[i] runs too far: you should compute the next d before comparing against n:
for (d = c[i] * 2; d <= n; d += c[i]) {
c[d - 1] = 0;
}
As a matter of fact you could start at d = c[i] * c[i] because all lower multiples have already been seen during the previous iterations of the outer loop.
Also note that it is confusing to store i + 1 into c[i]: the code would be simpler with an array of booleans holding 1 for prime numbers and 0 for composite.
Here is a modified version:
#include <stdio.h>
int main() {
unsigned char c[101];
int a[50];
int n, b = 0;
printf("Enter number as a limit:\n");
if (scanf("%d", &n) != 1 || n < 0 || n > 100) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < n; i++) {
c[i] = 1;
}
for (int i = 2; i < n; i++) {
if (c[i] != 0) {
a[b] = i;
//printf("%d %d %d\n", a[0], b, i);
b++;
for (int d = i * i; d <= n; d += i) {
c[d] = 0;
}
}
}
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 0; i < b; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
chqrlie$ ./sieve4780
Enter number as a limit:
25
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
chqrlie$ ./sieve4780
Enter number as a limit:
83
Prime numbers between 1 and 83 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Your problem seems to be caused by the fact that you have declared an array with size 50, but in fact it goes further than that: imagine you want to use Eratosthenes' procedure to find the first 10,000 prime numbers. Does this mean that you need to declare an array of size 10,000 first (or even bigger), risking to blow up your memory?
No: best thing to do is to work with collections where you don't need to set the maximum size at declaration time, like a linked list, a vector, ..., like that you can make your list grow as much as you like during runtime.

Write a basic half pyramid pattern program

Have tried few basic pattern
trying to get pattern
1
2 4
3 6 12
4 8 16 32
SO far trying to find the proper sequence, my idea is that need another variable lets say num, and need to create a sequence for num to print num eventually
#include <stdio.h>
int main()
{
int rows = 0 , i, j , num,num2;
do{
printf("please enter the number of rows: ");
scanf("%d",&rows);
}while(rows <=2 );
printf("printing a half pyramid of %d rows", rows);
printf("\n");
for( i = 1; i <=rows; ++i) {
for (j = 1; j <= i; ++j ) {
printf("%d ", );
}
printf("\n");
}
return 0;
}
Not being able to figure out a sequence
The code you were given literally contains all the parts necessary. All that remains for you is to fill out this line inside the nested loop:
printf("%d ", ‹what goes here?›);
To find the answer you need to find how the value relates to the current row and column (give by i and j, respectively).
You don’t need an additional variable num (to be clear, you can create one, but it’s not necessary to solve this problem).
We, beginners, should help each other.:)
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter the height of a pyramid (0 - exit): " );
unsigned int n;
if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
int width = 0;
unsigned int tmp = n * n;
do { ++width; } while ( tmp /= Base );
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j++ <= i; )
{
printf( "%*u ", width, value * j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter the height of a pyramid (0 - exit): 1
1
Enter the height of a pyramid (0 - exit): 2
1
2 4
Enter the height of a pyramid (0 - exit): 3
1
2 4
3 6 9
Enter the height of a pyramid (0 - exit): 4
1
2 4
3 6 9
4 8 12 16
Enter the height of a pyramid (0 - exit): 5
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Enter the height of a pyramid (0 - exit): 6
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
Enter the height of a pyramid (0 - exit): 7
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
Enter the height of a pyramid (0 - exit): 8
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
Enter the height of a pyramid (0 - exit): 9
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
Enter the height of a pyramid (0 - exit): 0
The loops in the program can also look like
for ( unsigned int i = 0; i++ < n; )
{
unsigned int value = i;
for ( unsigned int j = 0; j < i; j++ )
{
printf( "%*u ", width, value );
value += i;
}
putchar( '\n' );
}
or without introducing the intermediate variable value like
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j++ <= i; )
{
printf( "%*u ", width, j * ( i + 1 ) );
}
putchar( '\n' );
}
You yourself can add a check to the program that n * n is not greater than UINT_MAX.
Edit: As you changed the displayed values in the pattern then the program can look for example the following way
#include <stdio.h>
#include <math.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter the height of a pyramid (0 - exit): " );
unsigned int n;
if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
int width = 0;
unsigned long long int tmp = n * ( long long unsigned )pow( 2, ( n - 1 ) );
do { ++width; } while ( tmp /= Base );
putchar( '\n' );
for ( unsigned int i = 0; i++ < n; )
{
unsigned int value = i;
for ( unsigned int j = 0; j < i; j++ )
{
printf( "%*u ", width, value );
value *= 2;
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter the height of a pyramid (0 - exit): 10
1
2 4
3 6 12
4 8 16 32
5 10 20 40 80
6 12 24 48 96 192
7 14 28 56 112 224 448
8 16 32 64 128 256 512 1024
9 18 36 72 144 288 576 1152 2304
10 20 40 80 160 320 640 1280 2560 5120
Enter the height of a pyramid (0 - exit): 0
Tricky Pattern. Here, is a logic for that pattern with implementation.
'n' is the number of rows.
#include <stdio.h>
int main(void) {
int n = 4;
for(int i=1; i<=n; i++) {
int k=i;
printf("%d%s",k," ");
for(int j=1; j<i; j++) {
k = k*2;
printf("%d%s",k," ");
}
printf("\n");
}
return 0;
}

Program to code zigzag matrix vertically

I am writing a dynamic code that print n x n matrix in a zigzag pattern. Please help me with the code to get the output stated below:
The Code that I've tried so far with help of Rizier123 is in horizontal zigzag pattern:
#include <stdio.h>
int main() {
int rows, columns;
int rowCount, columnCount, count = 0;
printf("Please enter rows and columns:\n>");
scanf("%d %d", &rows, &columns);
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 1; columnCount <= columns; columnCount++) {
if(count % 2 == 0)
printf("%4d " , (columnCount+(rowCount*columns)));
else
printf("%4d " , ((rowCount+1)*columns)-columnCount+1);
}
count++;
printf("\n");
}
return 0;
}
Input:
5 5
Output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
I want the same zigzag pattern output but vertically..
EDIT
Expected output:
1 10 11 20 21 30
2 9 12 19 22 29
3 8 13 18 23 28
4 7 14 17 24 27
5 6 15 16 25 26
This should work for you:
#include <stdio.h>
int main() {
int rows, columns;
int rowCount, columnCount;
printf("Please enter rows and columns:\n>");
scanf("%d %d", &rows, &columns);
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 0; columnCount < columns; columnCount++) {
if(columnCount % 2 == 0)
printf("%4d " , rows*(columnCount)+rowCount+1);
else
printf("%4d " , (rows*(columnCount+1))-rowCount);
}
printf("\n");
}
return 0;
}
Input:
5 5
Output:
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25

Issue with printf precision control

I need to print out a multiplication table that looks like this in C:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 9 12 15 18 21 24 27 30
4 16 20 24 28 32 36 40
5 25 30 35 40 45 50
6 36 42 48 54 60
7 49 56 63 70
8 64 72 80
9 81 90
10 100
My loop to print the numbers in the correct format is a bit tedious right now:
printf(" 1 2 3 4 5 6 7 8 9 10\n");
for(i=1; i<=10; i++)
{
printf("%4d", i);
for (j=i; j<=10; j++)
{
result = i*j;
if (i == 2 && j == 2)
{
printf("%8d", result);
}
else if (i == 3 && j == 3)
{
printf("%12d", result);
}
else if (i == 4 && j == 4)
{
printf("%16d", result);
}
else if (i == 5 && j == 5)
{
printf("%20d", result);
}
else if (i == 6 && j == 6)
{
printf("%24d", result);
}
else if (i == 7 && j == 7)
{
printf("%28d", result);
}
else if (i == 8 && j == 8)
{
printf("%32d", result);
}
else if (i == 9 && j == 9)
{
printf("%36d", result);
}
else if (i == 10 && j == 10)
{
printf("%40d", result);
}
else
{
printf("%4d", result);
}
}
printf("\n");
}
I was thinking there has to be a way to make this easier, to somehow concat an int variable into the precision of the number, like this:
if (i == j)
{
printf("%(4 * i)d", result);
}
else
{
printf("%4d", result);
}
This code obviously won't work, but is there a way I can achieve something like this so I can avoid all the if/else statements in my current loop?
This may not be exactly what you want but it should help you:
#include <stdio.h>
int main() {
int i, j, result;
printf(" 1 2 3 4 5 6 7 8 9 10\n");
for(i=1; i<=10; i++) {
printf("%3d %*s", i, i * 4, " ");
for (j=i; j<=10; j++) {
printf("%3d ", i * j);
}
printf("\n");
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 9 12 15 18 21 24 27 30
4 16 20 24 28 32 36 40
5 25 30 35 40 45 50
6 36 42 48 54 60
7 49 56 63 70
8 64 72 80
9 81 90
10 100
Here is code that implements almost what you have as the desired output:
#include <stdio.h>
int main(void)
{
printf("%4s", " ");
for (int i = 1; i <= 10; i++)
printf("%4d", i);
putchar('\n');
for (int i = 1; i <= 10; i++)
{
printf("%-*d", 4 * i, i);
for (int j = i; j <= 10; j++)
printf("%4d", i * j);
putchar('\n');
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 9 12 15 18 21 24 27 30
4 16 20 24 28 32 36 40
5 25 30 35 40 45 50
6 36 42 48 54 60
7 49 56 63 70
8 64 72 80
9 81 90
10 100
The difference is in the space at the start of the lines. Your desired output has 2 spaces for the row number, followed by 2 spaces for the 1 in the row labelled 1, followed by 4 spaces for each other entry. Mimicking that exactly is a little fiddly — doable, but fiddly:
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 10; i++)
printf("%4d", i);
putchar('\n');
for (int i = 1; i <= 10; i++)
{
printf("%-*d", 4 * i - ((i == 1) ? 2 : 4), i);
for (int j = i; j <= 10; j++)
printf("%*d", (j == 1) ? 2 : 4, i * j);
putchar('\n');
}
return 0;
}
The conditional expressions are not very elegant.

Resources