For input N print 2*N number of rows in following Pattern
3
44
555
6666
555
44
3
The Solution is:
#include <iostream>
using namespace std;
int main()
{
int n=4,num=n-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
cout<<num;
num++;
cout<<endl;
}
num--;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
cout<<num;
num--;
cout<<endl;
}
return 0;
}
3
44
555
6666
555
44
3
The Solution is:
#include <iostream>
using namespace std;
int main()
{
int n=4,num=n-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
cout<<num;
num++;
cout<<endl;
}
num--;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
cout<<num;
num--;
cout<<endl;
}
return 0;
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThe above code will not print the same pattern as in the question!
ReplyDeleteOne more line 6666 will be there.
Just change ----
DeleteLine 13 : num-=2;
Line 14 : for(int i=n-1;i>=1;i--)
Can u write this cide by using functions
ReplyDelete
ReplyDelete#include
int i,j,k,start,n;
void PrintMatchPattern(start,n)
{
for(i=1; i<=2*n; i++)
{
if(i<=n)
{
for (j=1; j<=i; j++)
printf("%d", start);
start++;
printf("\n");
k=j;
}
else
{
start--;
for (j=1; j<k; j++)
printf("%d", start);
printf("\n");
k--;
}
}
}
int main()
{
PrintMatchPattern(2,5);
return 0;
}
Thank u
DeleteJava:-
ReplyDeletepublic class Testing {
public static void main(String[] args) {
int n = 4, num = n - 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print(num);
num++;
System.out.println();
}
num = num - 2;
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print(num);
num--;
System.out.println();
}
}
}
Is this correct?
DeleteI wrote the same but we showing error
public class xxx
ReplyDelete{
public static void main(String[] args)
{
int n=4,start=3;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
System.out.print(i+(start-1));
System.out.println();
}
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
System.out.print(i+(start-1));
System.out.println();
}
}
}
import java.util.*;
ReplyDeleteclass pattern{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int num=sc.nextInt(); int val=num;
for(int i=1;i<=(num+1);i++){
for(int j=1;j<=i;j++){
System.out.print(val+" ");
}
val++;
System.out.println();
}
val=val-2;
for(int i=num;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(val+" ");
}
val--;
System.out.println();
}
}
}