1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C
3 2
4 5 6
10 9 8 7
11 12 13 14 15
int main()
{
int i,j,k,l;
for(k=i=1;i<=5;i++)
{
l=k+i-1;
for(j=1;j<=i;j++)
{
if(i%2==1)
printf("%d ",k);
else
printf("%d ",l);
k++;
l--;
}
printf("\n");
}
return 0;
}
Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C
Hey the output of your given code does not correspond to the expected output.
ReplyDeleteHere is the my code that gives that output
import java.util.Scanner;
/* 1
* 3 2
* 4 5 6
* 10 9 8 7
* 11 12 13 14 15
*/
public class UltaPulta
{
public static void main(String[] args)
{
int count = 1;
Boolean direction = true;
int reverse = 0;
System.out.println("Enter number of rows to print :");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i < n; i++)
{
if(direction == true)
{
for(int j = 1; j <= i; j++)
{
System.out.print(count+" ");
count++;
}
direction = false;
}
else
{
reverse = count + i -1;
for(int j = i; j >=1; j--)
{
System.out.print(reverse+" ");
reverse--;
count++;
}
direction = true;
}
System.out.println();
}
}
}
Alternate code for the above output ??
ReplyDeleteI hv the altetnate solution and very simple coding
Deleteyes...i have a solution...it is in java language..you can change the syntax..the logic would be same
Deletepublic class d
{
public static void main(String[] args)
{
int i,j,b=1,r,c;
for(i=1;i<=5;i++)
{
if(i%2!=0)
{
for(j=1;j<=i;j++)
{
System.out.print(b+" ");
b++;
}
}
else
{
c=b-1;
r=c+i;
for(j=1;j<=i;j++)
{
System.out.print(r+" ");
r--;
}
b=c+i+1;
}
System.out.print("\n");
}
}
}
No the above logic code is giving correct outputoutput & is smaller
ReplyDelete#include
ReplyDelete#include
#include
int main(){
int i,j;
for(i=1;i<=5;i++){
if(i%2!=0){
for(j=(i*i-i+2)/2;j<=(i*i+i)/2;j++){
printf("%d ",j);
}
}
else{
for(j=(i*i+i)/2;j>=(i*i-i+2)/2;j--){
printf("%d ",j);
}
}
printf("\n");
}
}
the code given by admin is difficult to understand.Here is the most simplest solution and shorter than all the solutions given.
ReplyDeleteint main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==0)
{
value=(i*i+1)/2;
printf("%d",value);
value--;
}
else
{
value=((i*i-1)/2)+1;
printf("%d",value);
value++;
}
}
printf("\n");
}
return 0;
you forgot to declare int value
DeletespinysWlibpu1981 Paul Calhoun https://www.headoverhooves.org/profile/yamaryahvalorevittoria/profile
ReplyDeletenyalykuli