You need to take an integer input and then draw the pattern according to it. Say for example if you enter 5 then, the pattern should be like this-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Input Format:
You will take an integer input from stdin and the range of an integer can be from 1 to 1000.
Output Format:
Your output should be the pattern according to the input which you had entered.
Sample Test Case:
Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code:
- #include <iostream>
- using namespace std;
- int main()
- {
- int i,j,k,n,x=1;
- cin>>n;
- int y=2;
- for(i=1;i<=n;i++)
- {
- for(j=0;j<n*2-y;j++)
- cout<<" ";
- for(k=0;k<i;k++)
- {
- if(k==0)
- cout<<x++;
- else
- cout<<" "<<x++;
- }
- x=1;
- if(i!=0)
- cout<<"\n";
- y=y+2;
- }
- }
You need to take an integer input and then draw the pattern according to it. Say for example if you enter 5 then, the pattern should be like this-
* * * * *
* * * *
* * *
* *
*
Input Format:
You will take an integer input from stdin and the range of an integer can be from 1 to 1000.
Output Format:
Your output should be the pattern according to the input which you had entered.
Sample Test Case:
Sample Input:
5
Sample Output:
* * * * *
* * * *
* * *
* *
*
#include <iostream>
using namespace std;
int main()
{
int i,j,n,k;
cin>>n;
int x=2;
for(i=n;i>0;i--)
{
if(i!=n)
{ for(j=0;j<x;j++)
cout<<" ";
x+=2;
}
for(k=0;k<i;k++)
{
if(k==0)
cout<<"*";
else
cout<<" *";
}
if(i>0)
cout<<"\n";
}
}
Pattern Problem - 13
Task:
You need to take an integer input and then draw the pattern according to it. Say for example if you enter 5 then, the pattern should be like this-
A
C B A
E D C B A
G F E D C B A
I H G F E D C B A
Input Format:
You will take an integer input from stdin and the range of an integer can be from 1 to 26.
Output Format:
Your output should be the pattern according to the input which you had entered.
Note:
Please do not add space after the last character in each line. Failing to this will affect your submission and you will not get desired result.
Sample Test Case 1:
Sample Input:
5
Sample Output:
A
C B A
E D C B A
G F E D C B A
I H G F E D C B ASample Test Case 2:
Sample Input:
3
Sample Output:
A
C B A
E D C B A
Code:
#include <iostream>
using namespace std;
int main()
{
int i,n,j,k,x=2,ch='A',y=1;
cin>>n;
int h=n;
for(i=0;i<n;i++)
{
for(j=0;j<n*2-x;j++)
{
cout<<" ";
}
x+=2;
h=y-1;
for(k=0;k<y;k++)
{
if(k==0)
cout<<(char)(ch+h);
else
cout<<" "<<(char)(ch+h);
h--;
}
h=n;
y+=2;
if(i<n)
cout<<"\n";
}
}
#include <iostream>
using namespace std;
int main()
{
int i,n,j,k,x=2,ch='A',y=1;
cin>>n;
int h=n;
for(i=0;i<n;i++)
{
for(j=0;j<n*2-x;j++)
{
cout<<" ";
}
x+=2;
h=y-1;
for(k=0;k<y;k++)
{
if(k==0)
cout<<(char)(ch+h);
else
cout<<" "<<(char)(ch+h);
h--;
}
h=n;
y+=2;
if(i<n)
cout<<"\n";
}
}
package com.mystring;
ReplyDeleteimport java.util.*;
public class Apattern
{
public static void main(String arg[])
{
Scanner d=new Scanner(System.in);
int h=d.nextInt();
for(int i=1;i<=h;i++)
{
for(int j=1;j<=h-i;j++)
{
System.out.print(" ");
}
for(int k=(i*2)-1;k>0;k--)
{
char a=(char) (64+k);
System.out.print(a);
}
System.out.println();
}
}
}