The most common pattern is
for n=5
we have output
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
The Solution is:
#include <iostream>
using namespace std;
int main()
{
int n=5,num;
num=1;
int l=1;
int k=num;
for(int i=1;i<=n;i++)
{
k=num-1;
for(int j=1;j<=num;j++)
{
if(j%2==0)
cout<<"*";
else
{
if(i%2==0)
{
cout<<k+l-num+i;
l++;
k=k-2;
}
else
cout<<l++;
}
}
num=num+2;//the loop is going on as a prime number 1,3,5,7
cout<<"\n";
}
return 0;
}
for n=5
we have output
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
The Solution is:
#include <iostream>
using namespace std;
int main()
{
int n=5,num;
num=1;
int l=1;
int k=num;
for(int i=1;i<=n;i++)
{
k=num-1;
for(int j=1;j<=num;j++)
{
if(j%2==0)
cout<<"*";
else
{
if(i%2==0)
{
cout<<k+l-num+i;
l++;
k=k-2;
}
else
cout<<l++;
}
}
num=num+2;//the loop is going on as a prime number 1,3,5,7
cout<<"\n";
}
return 0;
}
Can u pls type the code in c
ReplyDeleteThis comment has been removed by the author.
Delete#include
Deleteint main() {
int n=5,k=1;
long l;
for(int i=1;i<=n;i++)
{
if(i%2!=0)
{
for(int j=1;j<=2*i-1;j++)
{
if(i%2!=0&&j%2==0)
{
printf("*");
}
else
{
printf("%d",k);
k++;
}
}printf("\n");
}
else
{
l=((i*i+i)/2);
for(int j=1;j<=2*i-1;j++)
{
if(i%2==0&&j%2==0)
{
printf("*");
}
else
{
printf("%ld",l);
l--;
k++;
}
}printf("\n");
}
}
}
how to print same in reverse way.
Deletelike 11*12*13*14*15
10*9*8*7
4*5*6
3*2
1
why are you cheking the conditions again.
Deleteif(i%2!=0&&j%2==0), no need of i%2!=0 becuase, you have entered the loop after cheking it only, same in else case also.
import java.io.*;
ReplyDeleteimport java.net.*;
public class Selenium_Projext {
public static void main(String[] args){
PrintPat(9);
}
public static void PrintPat(int a)
{ int last=0;
for(int i=1;i<=a;i++)
{ last=last+i;
int l=i;
for(int j=last;l>0;l--)
{
System.out.print(j--);
if(l>1)
System.out.print("*");
}
System.out.println();
}
}}
This comment has been removed by the author.
Deletepattern is not the same as expected
DeleteThanks for writing the same pattern in Java..!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIn C (easy way)..
ReplyDelete#include
int main()
{
int i,j,n,count=0,k=0;
printf("Enter N");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
count=k;
for(j=1;j<=i;j++)
{
if(i%2==0)
{
printf("%d",count+i);
count=count-1;
if(j!=i)printf("*");
k++;
}
else
{
count=count+1;
printf("%d",count);
if(j!=i)printf("*");
k++;
}
}
printf("\n");
}
return 0;
}
import java.io.*;
ReplyDeleteimport java.net.*;
public class mindtree {
public static void main(String[] args)
{
PrintPat(5);
}
public static void PrintPat(int n)
{
int i,j,num=1,k=num,I=1;
for(i=1;i<=n;i++)
{
k=num-1;
for(j=1;j<=num;j++)
{
if(j%2==0)
System.out.print("*");
else
{
if(i%2==0)
{
System.out.print(k+I-num+i);
I++;
k=k-2;
}
else
System.out.print(I++);
}
}
num=num+2;
System.out.println();
}
This comment has been removed by the author.
ReplyDelete#include
ReplyDeleteint main(){
int i,j,k=1;
for(i=1;i<=5;i++){
for(j=1;j<=2*i-1;j++){
if(j%2==0){
printf("*");
}
else{
printf("%d",k);
k++;
}
}
printf("\n");
}
}
Your code is not for the pattern asked, its for different pattern
Deletecode in c language(easy to understand)
ReplyDelete#include
void main()
{
int i,j,k=1,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",k++);
else
printf("%d",k++);
}
}
else
{
for(j=1;j<=i;j++)
{
if(j<i)
{
if(j==1)
k=k+i-1;
printf("%d*",k--);
}
else
printf("%d",k--);
if(j==i)
k=k+i+1;
}
}
printf("\n");
}
}
awesome man, this was one of the easiest way :)
Deletesuper! all test casses are passed.
DeleteNice post. Number Pattern Programs in Java .
ReplyDeletethanks
#include
ReplyDelete#include
using namespace std;
int main()
{
int i,j,k=1;
for(i=1;i<=5;i++)
{
if(i%2==0)
{
int temp=k-1+i;
for(j=1;j<=i*2-1;j++)
{
if(j%2==0)
{
printf("*");
}
else
{
printf("%d",temp);
temp--;
k++;
}
}
}
else
{
for(j=1;j<=2*i-1;j++)
{
if(j%2==0)
{
printf("*");
}
else
{
printf("%d",k);
k++;
}
}
}
printf("\n");
}
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletePython 3.x code:
ReplyDeletecount,k = 0,0
n = int(input("enter n:"))
for i in range(1,n+1):
count = k
for j in range(1,i+1):
if i%2 == 0:
print(count+i,end="")
count = count - 1
if j!=i:
print("*",end="")
k += 1
else:
count = count +1
print(count,end="")
if j!=i:
print("*",end="")
k += 1
print("\r")
It doesn't take proper indentation when i'm posting it.
if anyone knows how to paste a code with proper indentation in this blog.please suggest me.
like in stackoverflow we do ctrl+k for code.
count,k = 0,0
ReplyDeleten = int(input())
for i in range(1,n+1):
count = k
for j in range(1,i+1):
if i%2 == 0:
print(count+i,end="")
count = count - 1
if j!=i:
print("*",end="")
k += 1
else:
count = count +1
print(count,end="")
if j!=i:
print("*",end="")
k += 1
print("\r")
count,k = 0,0
ReplyDeleten = int(input())
for i in range(1,n+1):
count = k
for j in range(1,i+1):
if i%2 == 0:
print(count+i,end="")
count = count - 1
if j!=i:
print("*",end="")
k += 1
else:
count = count +1
print(count,end="")
if j!=i:
print("*",end="")
k += 1
print("\r")
how to write java pattern program in below output
ReplyDelete1*2*3
7*8*9
4*5*6
1112
ReplyDelete3222
3334
how to write java pattern?
#include
Deleteint main()
{
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
if(i%2!=0)
{
if(j<=n)
{
printf("%d",i);
}
else
{
printf("%d",i+1);
}
}
else
{
if(j==1)
{
printf("%d",i+1);
}
else
{
printf("%d",i);
}
}
}
printf("\n");
}
return 0;
}
10*9*8*7
ReplyDelete6*5*4
3*2
1
Can u plz explain the logic?
ReplyDeleteC Program to print pattern
ReplyDeletenice post
ReplyDeleteC Program to print pattern