[ create a new paste ] login | about

Link: http://codepad.org/JZkmmFgL    [ raw code | output | fork | 2 comments ]

C, pasted on Sep 12:
#include<stdio.h>
#include<conio.h>

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

	printf("\nNhap n: ");
	scanf("%d", &n);

	// Câu a:
	for(i = 1; i <= n; ++i)
	{
		for(j = 1; j <= n + i - 1; ++j)
		{
			printf((j < n - i + 1) ? " " : "%c", 234);
		}
		putchar('\n');
	}



	// Câu b:
	for(i = 1; i <= n; ++i)
	{
		for(j = 1; j <= n + i - 1; ++j)
		{
			printf((j == n - i + 1 || j == n + i - 1 || i == n) ? "%c" : " ", 234);
		}
		putchar('\n');
	}
	printf("\n");
	// Câu c: 
	for(i = 1; i <= n; ++i)
	{
		for(j = 1; j <= n; ++j)
		{
			printf((j <= i) ? "%c" : " ", 234);
		}
		printf("\n\n");
	}

	// Câu d
	for(i = 1; i <= n; ++i)
	{
		for(j = 1; j <= n; ++j)
		{
			printf((j == i || j == 1 || i == n) ? "*" : " ");
		}
		printf("\n");
	}

	 
	getch();
	return 0;
}

//#include<stdio.h>
//#include<conio.h>
//int main()
//{
//	for (int i = 0; i < 255; i++)
//	{
//		printf("ASCII value of character %c: %d\n", i, i);
//	}
//	getch();
//	return 0;
//}


Output:
1
Line 17: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by lehoangan01 on Feb 1
#include <iostream>
#include <math.h>
using namespace std;

void triangle(int n)
{
// number of spaces
int k = 2 * n - 2;

// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {

// Inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++)
cout << " ";

// Decrementing k after each loop
k = k - 1;

// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}

// Ending line after each row
cout << endl;
}
}

int main() {
int n;
printf("\nNhap n: ");
scanf("%d", &n);
triangle(n);
return 0;
}

reply
posted by lehoangan01 on Feb 1
#include <iostream>
#include <math.h>
using namespace std;

void triangle(int n)
{
for (int i = 1; i <= n; i++){
for (int k = n - i; k > 0; k--)
cout << " ";

for (int j = 1; j <= i; j++)
cout << "* ";
cout << endl;

}
}

int main() {
int n;
printf("\nNhap n: ");
scanf("%d", &n);
triangle(n);
return 0;
}


reply