how to generate number pattern in triangular form [duplicate] - c

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern

Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}

Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}

Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.

Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.

Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}

For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}

#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

Related

arrange the vector by showing the even values ​first and then the odd ones

Insert in a vector B, from its beginning, the even numbers of vector A and, from its end, the
odd numbers of vector A (this item must be solved with only a single structure of
repetition);
i don't know the conditions i have tu put in do while loop
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int i, j, A[25], B[25], testeNumeroIgual = 0, aux;
srand(time(NULL));
i = 0;
do {
A[i] = rand() % 40;
testeNumeroIgual = 0;
for (j = 0; j < i; j++) {
if (A[j] == A[i]) {
testeNumeroIgual = 1;
}
}
if (testeNumeroIgual == 0)
i++;
} while(i < 25);
i = 0;
do {
if (A[i] % 2 == 0) {
B[i] = A[i];
}
else {
B[25 - i] = A[i];
}
i++;
} while(i < 25);
for (i = 0; i < 25; i++) {
printf("%d\n", B[i]);
}
return 0;
}
Generate the random number into a specific loop to isolate the algorithm.
Then use two indexes tail and head to put it on the right place.
Increase head and decrease tail when you insert.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 25
int main()
{
int i, A[SIZE], B[SIZE], tail, head ;
srand(time(NULL));
/* generate random number for A */
for (i = 0; i < SIZE; i++) {
A[i] = rand() % 40;
}
tail = SIZE - 1;
head = 0;
for (i = 0; i < SIZE; i++) {
if (A[i] % 2) {
B[tail--] = A[i];
} else {
B[head++] = A[i];
}
}
printf("A: \n");
for (i = 0; i < SIZE; i++) {
printf("%d %d\n",i, A[i]);
}
printf("\nB: \n");
for (i = 0; i < SIZE; i++) {
printf("%i %d\n", i, B[i]);
}
return 0;
}

C programming with functions

I have got some more problems with the code. This program ask the user to specify the nr of throws then it throws 3 dices and add these 3 dices to sum.
Then another function sorts the sum form the smallest to the largest with a bubble sorting algorithm.
the first two functions seems to work but the program does not print out the result of the 3rd sorting function.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
//This function ask the user for the amout of throws
int numberofthrows() {
int throws
printf("Type in the number of throws");
scanf("%d", &throws);
return throws;
}
//This function makes the random throws of 3 dices with regard to the number of throws
int filler(int thrownr, int dice1[MAX], int dice2[MAX], int dice3[MAX], int sum[MAX]) {
int i, nr;
srand(time(NULL));
for(i = 0; i <= thrownr; i++) {
nr = rand()%6;
dice1[i] = nr + 1;
nr = rand()%6;
dice2[i] = nr + 1;
nr = rand()%6;
dice3[i] = nr + 1;
sum[i] = dice1[i] + dice2[i] + dice3[i];
}
int j;
for(j = 0; j <= thrownr; j++) {
printf("%d ", dice1[j]);
printf("%d ", dice2[j]);
printf("%d ", dice3[j]);
printf("%d\n", sum[j]);
}
}
//This function sorts the result in form the sum array
int sorter(int thrownr, int sum[MAX], int sortsum[MAX]) {
int tmp, i, j, k, m;
for(i = 0; i <= thrownr; i++) {
sortsum[i] = sum[i];
}
for(m = 0; m <= 10; m++) {
for(j = 0; j <= thrownr; i++) {
if (sortsum[j] > sortsum[j+1]) {
tmp = sortsum[j];
sortsum[j] = sortsum[j+1];
sortsum[j+1] = tmp;
}
}
}
for(k = 0; k <= thrownr; k++) {
printf("%d\n", sortsum[k]);
printf("%d\n", sum[k]);
}
}
int main(void) {
srand(time(NULL));
int dice1[MAX];
int dice2[MAX];
int dice3[MAX];
int sum[MAX];
int sortsum[MAX];
int numberofthrows2;
numberofthrows2 = numberofthrows();
filler(numberofthrows2, dice1, dice2, dice3, sum);
sorter(numberofthrows2, sum, sortsum);
return 0;
}
The code for sorting is a bit wrong. Change
for(m = 0; m <= 10; m++)
To
for(m = 0; m <= thrownr-1; m++)
And
for(j = 0; j <= thrownr; i++)
To
for(j = 0; j < thrownr-m-1; i++)
To fix it. Also, call srand once at the start of main. Don't call it more than once in a program or you might get the same "random" numbers everytime you run your program.

Is there something different with C in Hackerrank(getting different output)?

I've been trying to do the Love-Letter Mystery Challenge on Hackerrank.
Here are the rules: https://www.hackerrank.com/challenges/the-love-letter-mystery
And here's my solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 1000
int check_palindrome(char *A)
{
int i = 0, j;
while(A[i])
i++;
i -= 1;
for(j = 0; j <= i; j++)
{
if(A[j] != A[i - j])
return 0;
}
return 1;
}
int love_letter(char *A)
{
int i = 0;
int j;
int times;
while(A[i])
i++;
i -= 1;
if(i == 0)
return 0;
if(check_palindrome(A))
return 0;
for(j = i; j >= 0; j--)
{
while(A[j] != 'a')
{
if(check_palindrome(A))
return times;
else
{
A[j] -= 1;
times += 1;
}
}
}
return times;
}
int main() {
int t, i;
char a[MAX];
scanf("%d", &t);
for(i = 0; i < t; i++)
{
scanf("%s", a);
printf("%d\n", love_letter(a));
}
return 0;
}
While testing in on my computer, I get the right output. But, when I try to run the code on Hackerrank, it tell's that my program always gives an output of:
0
0
0
0
That's wrong of course, and it fails the testcase. But why is that? Is there something different about C or something? Or is it just a problem with the site? Or with my code?
At a minimum, you seem to have forgotten to initialize the variable "times".
In terms of the actual algorithm, keep in mind that to make the letters match, you can decrement either (or both) of them. I don't think you handle all cases properly.

Regarding a C Program of star pattern

if i give an input of 13, output should be like:
*
**
***
****
***
i've reached only this far:
#include <stdio.h>
int main()
{
int i, j, n = 13;
for(i = 1 ; i <= n ; i++)
{
for ( j = 1; j <= i ; j++)
{
printf("*");
}
t = ((i * (i + 1)) / 2);
printf("\n");
}
printf("%d",t);
return 0;
}
stars should be increasing by 1 in every line, but program will stop when total printed stars reach my inputted number. i am not sure how to start and where to end. i am very new to "for" loops.
You can do something like this
int n,i=1,j,coun=0;
scanf("%d",&n);
while(1)
{
if(i+coun<=n)
{
for(j=0;j<i;j++)
printf("*");
printf("\n");
coun+=i;
}
else
{
for(j=0;j<n-coun;j++)
printf("*");
printf("\n");
break;
}
i++;
}
#include <stdio.h>
int main(){
int i, j, k, n = 13;
for(k = j = i = 1 ; i <= n ; i++, j++){
putchar('*');
if(j==k){
putchar('\n');
j = 0;
++k;
}
}
return 0;
}

How do I generate number pattern in triangular form

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

Resources