Object

Write a program to input your name and print it diagonally on screen ten times.

Algorithm

  1. Declare integer variables row and col.
  2. Declare string name up to 10 characters.
  3. Read name.
  4. Repeat FOR row = 1 to 10 by 1
    • Repeat FOR col = 1 to row by 1
      • Give a space.
      • Write name and advance a line.
    • [End of inner FOR loop.]
    • [End of outer FOR loop.]
  5. Exit.

Flowchart

C++ Source Code

// program 47
#include <iostream>
using namespace std;

int main()
{
  int row, col;
  char name[10];
  cout << "Enter your name: ";
  cin >> name;

  for (row = 1; row <= 10; row++)
  {
    for (col = 1; col <= row; col++)
      cout << "  ";
    cout << name << "\n";
  }
    return 0;
}

C Source Code

/*program 47*/
#include<stdio.h>

int main()
{
  int row, col;
  char name[10];
  printf("Enter your name: ");
  scanf("%s", &name);
  for(row=1; row<=10; row++)
     {
     for(col=1; col<=row; col++)
	printf("  ");
     printf("%s\n", name);
     }
  return 0;
}

Output

Enter your name: shahzad
  shahzad
    shahzad
      shahzad
        shahzad
          shahzad
            shahzad
              shahzad
                shahzad
                  shahzad
                    shahzad
Design a site like this with WordPress.com
Get started