1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=j-2;j>=1;j--)
printf("%d ",j);
printf("\n");
}
return 0;
}
Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C
i think here in last loop j should be equal to j-1 not j-2.please reply asap.
ReplyDeleteNo buddy, the logic is all right.... it works as fine... try it!!!
DeleteSir plz explain the logic
Deleteint main()
Delete{
for(int i=0; i<5; i++){
for(int k=5; k>i;k--){
printf(" ");
}
for(int j=0; j<=i; j++){
printf("%d" , j+1);
}
for(int p=i; p>0; p--){
printf("%d", p);
}
printf("\n");
}
return 0;
}
Can you make this program with instead of ?
ReplyDeletecan explain the logic please.
ReplyDeleteThe last loop should be initialized with j=i-1.
ReplyDeleteyou can also use $i in place $j and Logic is $j=$i-1: For E.G:=$i;$j--)
ReplyDelete{
echo "#";
}
for($j=1;$j<=$i;$j++)
{
echo "$j";
}
for($j=$i-1;$j>=1;$j--)
{
echo "$j";
}
echo "
";
}
?>
Generalise code (for any value of n)
ReplyDelete#include
using namespace std;
int main()
{
int n;
cin>>n;
int size=2*n-1;
int mid=(size-1)/2;
int x=0;
int p=mid,q=mid;
int flag=0;
for(int i=0;i=p&&j<=q)
{
if(j<=mid)
{
printf("%d",++x);
}
else
{
printf("%d",--x);
}
}
else
cout<<" ";
}
x=0;
p--;
q++;
cout<<endl;
}
}
#include
ReplyDeleteint main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=i-1;j>=1;j--)
printf("%d ",j);
printf("\n");
}
return 0;
}
This small change will be easy to understand
It have a doubt what value will be initially taken for j in 3rd loop
J=j-2
Please do clarify
its the same thing...if u write j=j-2 it takes the last value with which the second loop was ended...that is if the second loop had j=3 as the last value then in the third loop it will start with j=3-2 which is 1
Deletesir this logic is not working in java
ReplyDeleteplzzz give me a solution
import java.util.Scanner;
Deletepublic class Pattern10
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int row=0;
System.out.println("Enter the number of Rows");
row = scan.nextInt();
for(int i=1;i<=row;i++)
{
for(int j=row;j>i;j--)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
for(int j=i-1;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
scan.close();
}
}
Try something in different way
ReplyDelete#include
int main(void){
int i , j , k , l , row ,set ;
printf("Enter the rows = ");
scanf("%d",&row);
for( i = 0 ; i < row ; i ++ ){
for( j = 0 ; j < ( row - i - 1 ); j ++){
printf(" ");
}
set = 1 ;
for( k = 0 ; k <= i ; k ++ ){
printf("%d",set++);
}
set = set - 2 ;
for(l = 1 ; l <= i ; l++){
if(set != 0)
printf("%d",set --);
}
printf("\n");
}
return 1;
}
Hey guys, I was wondering if you guys know that you could do this pattern by multiplying two numbers and change those two numbers and again multiply. It's a math trick..
ReplyDeleteright sir
ReplyDeletei am also blogger...... sir...
ReplyDeletewhat will be program for the following output:
ReplyDelete1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
public class Pattern
Delete{
public static void main(String args[])
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
int k = i-1;
for(int j=k;j>=1;j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
please help me out with this pattern 5
ReplyDelete545
54345
5432345
Please help me in
ReplyDelete11
11 10 11
11 10 9 10 11
11 10 9 8 9 10 11