how to make isosceles triangle with loop in C - c

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i, space, nilai, k = 0;
scanf("%d", &nilai);
for (i = 1; i <= nilai; ++i, k = 0) {
for (space = 1; space <= nilai - i; ++space) {
printf("%d", nilai);
}
while (k < 2 * i - 1) {
if (k == 0 || k == (2 * i) - 2 || i == nilai) printf("*");
else printf("%d", nilai);
k++;
}
printf("\n");
}
return 0;
}
how to make output like this i alrd try with my code in there but my output like this i hope someone give me explanation

replace the following line
printf("%d", nilai);
with
printf("%d", " ");
everywhere in your code.

Related

Iterating through code to add a new line on the nth term

How would I go about adding a new line on the 10th term on this zybooks question
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
printf("%d\t", n);
while (n > 1) {
if (n % 2 == 1) {
n = 3 * n + 1;
}
else {
n = n / 2;
}
printf("%d\t",n);
}
printf("\n");
return 0;
}
This solution uses a counter variable i which increments until 10 is reached. Once it is reached a newline character (\n) is printed.
#include <stdio.h>
int main(void)
{
int n, i = 0;
scanf("%d", &n);
printf("%d\t", n);
while (n > 1) {
i++;
(n % 2 == 1) ? (n = 3*n+1) : (n = n/2);
if (i == 10) {
i = 0;
printf("\n");
}
else {
printf("%d\t", n);
}
}
printf("\n");
return 0;
}

Optimize code to get following output in c

I had an interview last week. They asked me to write a code to print like this
input :5
0
101
21012
3210123
432101234
54321012345
i wrote the below code but he said i could optimize this more . i cant figure it out.
,
int main(){
int n,i,j,k,lim,num;
scanf("%d",&n);//getting input starting number of last row
lim=n;
int collen=n+2;//it denotes end of row
for(i=0;i<n+1;i++)
{
num=i;
k=0;
for(j=0;j<collen-1;j++){
if(j<lim)
printf(" ");
else if(num<0){
printf("%d",++k);
}
else{
printf("%d",num--);
}
}//j for
printf("\n");
collen++;
lim--;
}//i for
}// main end
I have different code at first attempt, I used flag to detect when num reaches for incrementing and decrementing, it was complex there was about 4 if inside second loop, so I optimized that code to the above one. He said can you optimize more? I have no idea to optimize it .
My question: can it be optimized? If it can be - please post the code
There are more for-loops than those comparing by <. for(i=0;i<n+1;i++) is much clearer written as for (i = 0; i <= n; i++).
If you initialize a value, in example int collen=n+2;, and later use it like collen-1, save the subtraction and initialize it adjusted.
Separate this complex inner loop with ifs into their own loops.
Use less variables.
Use more and consistent whitespace.
And now my solution, but as yours it can only handle inputs from 0 to 9:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n;
printf("input :");
if (scanf("%d", &n) != 1 || n < 0 || n > 9) {
printf("input not recognized or invalid\n");
return EXIT_FAILURE;
}
for (int i = 0; i <= n; ++i) {
printf("%*d", n - i + 1, i);
for (int j = i - 1; j >= 0; --j) {
printf("%d", j);
}
for (int j = 1; j <= i; ++j) {
printf("%d", j);
}
printf("\n");
}
return EXIT_SUCCESS;
}
This looks optimized to the brims:
int main( int argc, char **argv )
{
puts( "input :5");
puts( " 0");
puts( " 101");
puts( " 21012");
puts( " 3210123");
puts( " 432101234");
puts( "54321012345");
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Print a pyramid */
int
main(int argc, char **argv)
{
int size = argc > 1 ? strtol(argv[1],NULL,10) : 5;
if( size > 9 || size < 0) {
fprintf(stderr, "Invalid size\n");
return EXIT_FAILURE;
}
for(int line = 0; line <= size; line++) {
char template[]="9876543210123456789";
char *s = template + 9 - size;
template[10 + line] = '\0';
memset(s, ' ', size - line);
if(puts(s) == EOF) {
perror("puts");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

concatinating even and odd places in a string

I'm learning basics in coding. Can any one say what went wrong with my code
Prob:Given a string, S, of length N that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
char input[100], final[100];
int main()
{
int num, i, j;
char even[50], odd[50], space[] = " ";
scanf("%d", &num);
for (i = 0; i < num; i++)
{
int k = 0, p = 0;
scanf(" %[^\n]s", input);
for (j = 0; input[j] != '\0'; j++)
{
if (j % 2 == 0)
{
even[k] = input[j];
k++;
}
else
{
odd[p] = input[j];
p++;
}
}
strcat(final, even);
strcat(final, space);
strcat(final, odd);
}
printf("%s", final);
}

SPOJ shows wrong answer to the my solution to: http://www.spoj.com/BSCPROG/problems/SMPCPH1/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main() {
int n, m, i, j, k;
char a[100], b[100];
scanf("%d", &n);
if (n > 26 && n <= 1)
exit(0);
scanf("%s", a);
scanf("%d", &m);
if (m > 100 && m <= 1)
exit(0);
for (i = 0; i < m; i++) {
fgets(b, 100, stdin);
for (j = 0; b[j] != '\0'; j++) {
for (k = 0; k < n; k++) {
if (a[k] == b[j]) {
if (k == (n - 1)) {
b[j] = a[0];
break;
} else {
b[j] = a[k+1];
break;
}
}
}
}
printf("%s",b);
}
return 0;
}
I just have written C code for the problem: http://www.spoj.com/BSCPROG/problems/SMPCPH1/
With the given example my program gives desired result but spoj says it is wrong. How can I find mistakes?
You need 3 changes in your code:
Use || instead of && in your argument validation tests.
Scan and ignore the line feed after the number parsed into m, otherwise the next fgets() will read an empty line
Here is the corrected code:
if (n > 26 || n <= 1)
exit(0);
scanf("%s", a);
scanf("%d%*c", &m);
if (m > 100 || m <= 1)
exit(0);

Character patterns in C

I'm trying to get the following character pattern as follows by only using "for" or "while" loops but no "if" or "if-else" statements: (Mentioned in code block)
It's difficult for me to figure out a way to make the first character appear as blank space or represent it even as a null character. Below is my code:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch[6], j='\0';
int p,q,n,i;
printf("Enter a character\n");
for (n = 0; n < 5; n++)
{
scanf_s(" %c", &ch[n], 1);
}
printf("\n");
for (i = 4; i >= 0; i--)
{
for (p = 4; p >= i; p--)
{
printf("%c", ch[p+1]-1);// _-->A blank space
} // BA
printf("\n"); // CBA
} // DCBA
// EDCBA
_getch();
return 0;
}
I cannot figure out where I'm going wrong — can you help?
I changed this
printf("\n");
for (i = 4; i >= 0; i--)
{
for (p = 4; p >= i; p--)
{
printf("%c", ch[p+1]-1);
}
printf("\n");
}
for this
printf("\n_\n");
for (i = 1; i < 5; i++) {
for (p = i; p >= 0; p--) {
printf("%c", ch[p]);
}
printf("\n");
}
And got this output (console):
Enter a character
ABCDE
_
BA
CBA
DCBA
EDCBA
#include <stdio.h>
#include <conio.h>
#define SIZE 5
int main(void){
char ch[SIZE+1] = {0};
int i;
printf("Enter a character\n");
for (i = SIZE-1; i >= 0; --i){
scanf_s(" %c", &ch[i], 1);
}
printf(" \n");
for (i = SIZE-2; i >= 0; --i) {
printf("%s\n", ch + i);
}
_getch();
return 0;
}

Resources