Name: Class Roll No: 5 6 Section: G Date: 12.11.2020 Assignment No: 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Name :

Class Roll No : 5 6
Section : G
Date : 12.11.2020
Assignment No : 7
Q1.

Ans :
#include <stdio.h>

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

printf("Enter the number of rows : ");


scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= i; j++) {
printf("* ");
}

printf("\n");
}

return 0;
}
Output:
Q2.
Ans:
#include <stdio.h>

int main() {

int i, j, n;

printf("Enter the number of rows : ");


scanf("%d", &n);

for(i = n; i >= 1; i--) {


for(j = 1; j <= i; j++) {
printf("* ");
}

printf("\n");
}

return 0;
}

Output:
Q3.
Ans:
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}

for(i = n - 1; i >= 1; i--) {


for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}

return 0;
}
Output:
Q4.
Ans :
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 65; j < 65 + i; j++) {
printf("%c", j);
}

printf("\n");
}

return 0;
}

Output:
Q5.
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


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

printf("\n");
}

return 0;
}

Output:
Q6.
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {


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

printf("\n");
}

return 0;
}

Output:
Q7.
#include <stdio.h>

int main() {

int i, j, k, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= n - i; j++) {
printf(" ");
}

for(k = 1; k <= (2 * i) - 1; k++) {


printf("* ");
}

printf("\n");
}

return 0;
}

Output:
Q8.
#include <stdio.h>

int main() {

int i, j, k, n, counter1 = 0, counter2 = 0;


printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 1; i <= n; i++) {


for(j = 1; j <= n - i; j++) {
printf(" ");
counter1++;
}

for(k = 0; k < (2 * i) - 1; k++) {


if (counter1 <= n - 1) {
printf("%d ", i + k);
++counter1;
} else {
++counter2;
printf("%d ", (i + k - 2 * counter2));
}
}

counter1 = 0;
counter2 = 0;
k = 0;

printf("\n");
}

return 0;
}
Output:
Q9.
#include <stdio.h>

int main() {

int n;
printf("Enter the number of rows : ");
scanf("%d", &n);

int i, j;
for(i = n; i >= 1; i--) {
for(j = 0; j < n - i; j++) {
printf(" ");
}

for (j = i; j <= 2 * i - 1; ++j){


printf("* ");
}

for (j = 0; j < i - 1; ++j){


printf("* ");
}

printf("\n");
}

return 0;
}
Output:
Q10.
#include <stdio.h>

int main() {

int n;
printf("Enter the number of rows : ");
scanf("%d", &n);

int c = 1, i, j, k;

for (i = 0; i < n; i++) {


for (k = 1; k <= n - i; k++){
printf(" ");
}

for (j = 0; j <= i; j++) {


if (j == 0 || i == 0)
c = 1;
else
c = c * (i - j + 1) / j;

printf("%d ", c);


}

printf("\n");
}

return 0;
}
Output:
Q11.
#include <stdio.h>

int main() {

int i, j, n, counter = 1;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 0; i < n; i++) {


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

printf("\n");
}

return 0;
}

Output:
Q12.
#include <stdio.h>

int main() {

int i, j, n, counter = 1;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = n; i >= 1; i--, counter++) {


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

for(j = 1; j <= counter; j++) {


printf("%d ", j);
}

printf("\n");
}

counter -= 2;

for(i = 1; i <= n; i++, counter--) {


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

for(j = 1; j <= counter; j++) {


printf("%d ", j);
}

printf("\n");
}

return 0;
}
Output:
Q13.
#include <stdio.h>

int main() {

int i, j, n, counter = 1;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = n; i >= 1; i--, counter++) {

if(counter < 3 || counter == n) {


for(j = 1; j <= counter; j++) {
printf("*");
}
} else {
printf("*");
for(j = 1; j <= counter - 2; j++) {
printf(" ");
}
printf("*");;
}

printf("\n");
}

return 0;
}
Output:
Q14.
#include <stdio.h>

int main() {

int i, j, n, counter = 1;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = n; i >= 1; i--, counter++) {


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

if(counter < 3 || counter == n) {


for(j = 1; j <= counter; j++) {
printf("*");
}
} else {
printf("*");
for(j = 1; j <= counter - 2; j++) {
printf(" ");
}
printf("*");;
}

printf("\n");
}

return 0;
}
Output:
Q15.
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {

if(i == 1 || i == n) {
for(j = 1; j <= n; j++) {
printf("*");
}
} else {
printf("*");
for(j = 1; j <= n - 2; j++) {
printf(" ");
}
printf("*");
}

printf("\n");
}

return 0;
}
Output:
Q16.
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = 5; i >= 1; i--) {


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

for(j = 1; j <= n; j++) {


printf("*");
}

printf("\n");
}

return 0;
}

Output:
Q17.
#include <stdio.h>

int main() {

int i, j, n;
printf("Enter the number of rows : ");
scanf("%d", &n);

for(i = n; i >= 1; i--) {


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

if(i == 1 || i == n) {
for(j = 1; j <= n; j++) {
printf("*");
}
} else {
printf("*");
for(j = 1; j <= n - 2; j++) {
printf(" ");
}
printf("*");
}

printf("\n");
}

return 0;
}
Output:

You might also like