Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 6 July 2019

Alphabet Pattern

Question:




























Solution:

//Method 1:

#include<stdio.h>

void main()
{

int no_of_lines, alphabet = 65, i, count, j;

printf("\nenter the number of lines you want to print\t:");
scanf("%d",&no_of_lines);
count=2*no_of_lines;
for(j=0;j<no_of_lines;j++)
{
if(j==0)
{
printf("\n\n");
for(i=0;i<count;i++)
{
if(i<no_of_lines)
{
printf(" %c", alphabet++);
}
else if(i == no_of_lines)
{
alphabet--;
}
else
{
printf(" %c",--alphabet);
}
}
}
else
{
printf("\n");
for(i=0;i<count-2*j;i++)
{
if(i<no_of_lines-j)
{
printf(" %c", alphabet++);
}
else
{
printf(" %c",--alphabet);
}
}

}
}
}



//Method 2: 
//Credit Rakshita

#include<stdio.h>
int main()
{
int n,i,j=1,m;
char alpha;
printf("enter a number");
scanf("%d",&n);
m=n;
for(i=1;i<=n;i++)
{
    alpha='A';
    printf("%c",alpha);
  for(j=1;j<m;j++)
  {
      alpha=alpha+1;
    printf("%c",alpha);
  }
  if(j>=m)
  {
      if(m!=n)
        printf("%c",alpha);
      while(j!=1)
      {
          alpha=alpha-1;
          printf("%c",alpha);
          j--;
      }
  }
  printf("\n");
  m--;
}
return 0;
}

Method 3:
//Shravya Shetty 
import java.io.*;

public class test {
public static void main(String agrs[]) throws IOException{
char ch;
int i=0,m;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter the number of lines you want to print :");

int n=Integer.parseInt(br.readLine());
m=n;
char arr[]=new char[n+1];
while(n!=0) {
i=0;
for(ch='A' ;ch<('A'+n);ch++)
{
System.out.print(ch+" ");
arr[i++]=ch;

}

for(i=(n-1);i>=0;i--)
{
if(i==(m-1)) {
continue;
}
System.out.print(arr[i]+" ");
}
System.out.println();
n--;
}
}
}

0 comments:

Post a Comment