Reputation: 1
I have tried as per below code but not able to get the desired output
package patterns;
import java.util.Scanner;
public class OddSquarePattern {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int i=1;
while(i<=n) {
int j=n;
int odd= 2*i-1;
while(j>=i){
System.out.print(odd);
odd=odd+2;
j--;
}
int p=1;
while (p<=i-1) {
System.out.print(p);
p=p+2;
}
System.out.println();
i++;
}
}
}
Upvotes: 0
Views: 10629
Reputation: 1
Write a program that takes an integer n as input and generates a square pattern of n x n size.
Input Format The input consists of a single integer n, where 1 <= n <= 100.
Output Format The output is a square pattern of size n x n, where each row consists of n asterisks (*).
Sample Inputs & Outputs Sample Input 1 5 Sample Output 1
Constraints 1 <= n <= 100
Upvotes: 0
Reputation: 1
import java.util.*; public class Patterrn {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i=1;
while(i<=n)
{
int x=2*i-1;
int j=1;
while(j<=n)
{
if(x<=7)
{
System.out.print(x);
x=x+2;
}
else
{
x=1;
System.out.print(x);
x=x+2;
}
j++;
}
System.out.println("");
i++;
x=x+2;
}
}
}
Upvotes: 0
Reputation: 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int i=1
while (i<=n) {
int j = n;
int p = 2*i-1;
while(j>=0){
System.out.print(p);
p=p+2;
if(p>(2*n)-1) {
p=1;
}
j--;
}
System.out.println();
i++;
}
}
}
Upvotes: 0
Reputation: 1
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=1;i<=n;i++) {
int odd=2*i-1;
for(int j=n;j>=i;j--) {
System.out.print(odd);
odd=odd+2;
}
int p=1;
for(int j=1;j<=i-1;j++) {
System.out.print(p);
p=p+2;
}
System.out.println();
}
}
}
Upvotes: 0
Reputation: 1
import java.util.Scanner;
public class TestingoddSquare {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
if(n>=0 && n<=50) {
int i=1;
while(i<=n) {
int j=n;
int odd= 2*i-1;
while(j>=i){
System.out.print(odd);
odd=odd+2;
j--;
}
int p=1;
int k=1;
while (p<=i-1) {
System.out.print(k);
k=k+2;
p=p+1;
}
System.out.println();
i++;
}
}
}
}
Upvotes: 0
Reputation: 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int i=1;
while(i<=n) {
int j=1;
int num=2*i-1;
while(j<=n-i+1){
System.out.print(num);
num=num+2;
j++;
}
j=1;
int k=1;
while(j<=i-1){
System.out.print(k);
k=k+2;
j++;
}
System.out.println();
i++;
}
}
}
Upvotes: 0
Reputation: 21
for successfully getting output as N=5 output will be
13579
35791
57913
79135
91357
Code in Java:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int i = 1,j;
while(i<=N){
int odd = 2*i-1;
j=N;
while(j>=i){
System.out.print(odd);
odd = odd +2;
j--;
}
j = 1;
int p =1;
while(j<=i-1){
System.out.print(p);
p= p+2;
j++;
}
System.out.println();
i++;
}
}
}
Upvotes: 1
Reputation: 1
import java.util.*;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int i=1;
while(i<=n) {
int j=n;
int odd= 2*i-1;
while(j>=i){
System.out.print(odd);
odd=odd+2;
j--;
}
int p=1;
int k =1;
while (k<=i-1) {
System.out.print(p);
p=p+2;
k++;
}
System.out.println();
i++;
}
}
}
Upvotes: 0
Reputation: 1
Try this code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int i=1;
while(i<=n) {
int j=n;
int odd= 2*i-1;
while(j>=i) {
System.out.print(odd);
odd=odd+2;
j--;
}
if(j!=0) {
int p=1,temp=1;
while (temp<i) {
System.out.print(p);
p=p+2;
temp++;
}
}
System.out.println();
i++;
}
}
}
Upvotes: 0
Reputation: 69
You should import scanner package. Type this in previous line before public static void import java.util.Scanner;
Upvotes: 1