Object

Write a program to print a string, starting with the first character increasing one character in the following line until the end of the string is reached.

Algorithm

  1. Declare a string word up to 20 characters and integer variables length, i and j.
  2. Read string word.
  3. Find length of string word, using strlen(word).
  4. Repeat FOR i = 1 to length of string by 1
    • Repeat FOR j = 0 to (i-1) by 1
      • Write word[j].
      • Advance a line.
    • [End of inner FOR loop.]
    • [End of outer FOR loop.]
  5. Exit.

Flowchart

C++ Source Code

// program 96
#include<iostream>
#include <cstring>
using namespace std;

int main()
{
  char word[20];
  int length, i, j;
  cout << "Enter a word up to 20 characters: ";
  cin.getline(word, 20);
  length = strlen(word);
  
  for (i = 1; i <= length; i++)
  {
    for (j = 0; j < i; j++)
      cout << word[j];
    cout << endl;
  }
  return 0;
}

C Source Code

/*program 96*/
#include<stdio.h>
#include<string.h>

int main()
{
  char word[20];
  int length, i, j;
  printf("\nEnter a word up to 20 characters: ");
  gets(word);
  length = strlen(word);
  for(i=1; i<=length; i++)
     {
     for(j=0; j<i; j++)
        printf("%c", word[j]);
     printf("\n");
     }
  return 0;
}

Output

Enter a word up to 20 characters: PAKISTAN
P
PA
PAK
PAKI
PAKIS
PAKIST
PAKISTA
PAKISTAN

Design a site like this with WordPress.com
Get started