My program is as below:
package simplemirror;
public class simple {
public static void main (String arg[]){
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print(i+"*");
}
System.out.println("");
}
}
}
Above program output is as below:
1*
2*2*
3*3*3*
I need output as below:
1
2*2
3*3*3
2*2
1
this code will work try this.............:)
public class simple {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
n=3;
for(int i=1;i<2*n;i++){
if(i<=n){
for(int j=1;j<i;j++){
System.out.print(i+"*");
}
System.out.println(i);
}
else{
for(int j=i+1;j<2*n;j++){
System.out.print(2*n-i+"*");
}
System.out.println(2*n-i);
}
}
}
}
try
public static void main(String[] args) {
int limit=5;
int rows=limit+(limit-1);
int center=(int) Math.ceil(((double)rows/2));
boolean centerReached=false;
for(int i=1;i<=limit&&i>0;){
for(int j=1;j<=i;j++){
System.out.print(i);
if(i==j)
continue;
System.out.print("*");
}
if(center==i)
centerReached=true;
if(centerReached)
i--;
else
i++;
System.out.println();
}
}
ouptut
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
EDIT:
It's been a while, and I have learned a few things.
You can use streams!
private static void print(int num) {
IntStream.concat(
IntStream.iterate(1, (i) -> ++i).limit(num),
IntStream.iterate(num - 1, (i) -> --i).limit(num).limit(num - 1))
.boxed()
.map(i -> Stream.generate(i::toString).limit(i).collect(Collectors.joining("*")))
.forEach(System.out::println);
}
Please don't ask such question here. Ans to your question may be looks like follows but once again don't ask such questions here.
public class simple {
public static void main (String arg[])
{
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
if(j== i-1)
{
System.out.print(i);
}
else
{
System.out.print(i+"*");
}
}
System.out.println("");
}
}
}
Related
static void nLines(int n) {
for (int i= 0; i < n; i++) {
System.out.println(i);
}
}
You can recurse on n.
static void nLines(int n) {
if( n <= 0) {
return;
} else {
int x = n - 1;
nLines(x);
System.out.println(x);
}
}
You call the function like so:
nLines(n);
Here is a recursive function to print n lines:
static void nLinesRecursive(int n) {
int next = n - 1;
if (next >= 0) {
nLinesRecursive(next);
System.out.println(next);
}
}
Try this.
static void nLines(int n) {
if (--n < 0) return;
nLines(n);
System.out.println(n);
}
public static void main(String[] args) {
nLines(3);
}
output:
0
1
2
I am trying to iterate over an array and add the sum of the array except the number 13 and the number after it.
Example
[1,1,1,1,13,2] = [1,1,1,1,0,0] = 4
this is what I have so far the main things I need to know is how do I check if the array has a number 13 in it and how do I change it to a 0
public static int sum13(int[] nums) {
for(int i=0; i < nums.length; i++) {
if(nums.indexOf(i) == 13) {
}
}
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
sum13(a);
}
}
You can try this , to skip adding all number when you get 13 in your array :
public static int sum13(int[] nums) {
int sum = 0;
for(int i=0; i < nums.length; i++) {
if(nums[i] == 13) {
break;
}
sum += nums[i];
}
return sum;
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
System.out.println(sum13(a));
}
Try this.
public static int sum13(int[] nums) {
return IntStream.of(nums).takeWhile(i -> i != 13).sum();
}
Good day! Newbie in Java Programming. Would like to ask for some help on understanding the looping. The program's aim is to print a right-aligned triangle. I've created method for printing "*" and another one for " " (whitespace). I'm having trouble understanding on how can I implement the whitespace in my main method. Thank you!
Expected output:
"printTriangle(4);"
*
**
***
****
Here is my code:
public class PrintingLikeBoss {
public static void printStars(int amount) {
int i = 1;
while (i <= amount) {
System.out.print("*");
i++;
}
System.out.println("");
}
public static void printWhitespaces(int amount) {
int i = 1;
while (i <= amount) {
System.out.print(" ");
i++;
}
System.out.println("");
}
public static void printTriangle(int size) {
int i = 1;
int j = 1;
while (i >= 0) {
printStars(size);
i++;
}
}
printTriangle(4);
}
}
The printTriangle() methode will never end due to i always being greater then 0.
Also there is no main method in your code, therefore you will not be able to run it.
Now for the answer to your question:
public static void printTriangle(int size){
int i = size;
int j = 1;
while(j<=i){
printWhite(i-j);
printStar(j);
j++;
System.out.println("");
}
}
public static void printWhite(int size){
int i = size;
for(int j = 0; j<i; j++){
System.out.print(" ");
}
}
public static void printStar(int size){
int i = size;
for(int j = 0; j<i; j++){
System.out.print("*");
}
}
public static void main(String[] args){
printTriangle(4);
}
This should provide you an output like this:
*
**
***
****
The task of exercise is to make a method that will work like in Example.( We must use for loop. But if you know how to do it in another way, it will be also very interesting.) Input number can be any.
Example:
Input: 3
Output:
**1**
*121*
12321
*121*
**1**
My example:
public static void main(String[] args) {
printMatrix(5);
}
public static void printMatrix (int n) {
int d = n +(n-1);
for (int i = 0; i < n ; i++)
{
for(int j = 1; j <=d; j++){
int abs = Math.abs(j-n);
System.out.print(abs>i ? "*" : i-abs+1);
}
System.out.println("");
}
My output:
****1****
***121***
**12321**
*1234321*
123454321
I can't make the next step, to turn it upside down. Dose anybody hava an ideas?
I have solved this exercise, but I think it is not a perfect solution. Does anybody have another way to solve this task?
public static void main(String[] args) {
printMatrix(5);
}
public static void printMatrix (int n) {
int d = n +(n-1);
int k = n*2;
int g = -1;
for (int i = 0; i < d ; i++)
{
if(i<n){
g++;
}
else{k=n*2;
g--;}
for(int j = 1; j <=d; j++){
if (i < n){
int abs = Math.abs(j-n);
System.out.print(abs>i ? "*" : i-abs+1);
}
else{
k--;
int abs = Math.abs(k-n);
System.out.print(abs>g ? "*" : g-abs+1);
}
}
System.out.println("");
}
}
}
Output:
****1****
***121***
**12321**
*1234321*
123454321
*1234321*
**12321**
***121***
****1****
I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
Write a program to assign the integer values 1 through 25 to a 25
element integer array. Then, print the array as five separate lines
each containing five elements separated by commas. The last element
on each line should be followed by a newline instead of a comma. The
output of your program should appear exactly as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
Hints:
One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is
0, it is the 5th number.
You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same
line.
I have a little bit of code but I don't know where to go from here:
public class Program4
{
public static int[] array;
public static void main(String[] args);
{
int[] numbers = new int [25]
for(int i=0; i<25; i++)
array[i] = i + 1;}
public static void printArray()
{
for(int i=1; i<=25; i++);
{
System.out.print(array[i - 1]);
if (i % 5 == 0)
System.out.printIn();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this,
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;
public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}
public static void printArray()
{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}
public class Foo {
public static int[] nums;
public static void main(String[] args) {
nums = new int[25];
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
printArray(nums);
}
public static void printArray(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.print(String.valueOf(myArray[i]);
if (i % 5 == 0) {
System.out.println();
} else if (i % 5 != 4){
System.out.println(", ");
}
}
}
public class Test {
public static void main(String[] args) {
int[] array = new int [25];
for(int i=0; i<25; i++) {
array[i] = i + 1;
}
for (int i=1; i<=25; i++) {
System.out.print(array[i - 1]);
if (i % 5 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
And try to learn Java syntax first of all.
Here is an enhanced version of your code.
public class Program4
{
public static int[] array = new int[25];//instantiate the array to its default values
public static void main(String[] args)
{
//calling the methods from main
addToArray();
printArray();
}
//add the numbers to the array
public static void addToArray(){
for(int i=0; i<25; i++)
array[i] = i + 1;
}
//print the numbers from the array
public static void printArray()
{
for(int i = 1; i <= 25; i++){
if(i % 5 == 0){
System.out.print(i);
System.out.println();
}
else{
System.out.print(i + ",");
}
}
}
}