hello

Can u help me ?


write an java program that outputs the following pattern. The program shuold prompt the user an odd number of lines (limit 1 to 11) .

For example, if the user prompt the number of the lines is 9, then the output should be:


+
++
+++
++++
+++++
++++
+++
++
+

Hi alreem,

We can only help you if you show that you have made some sort of an attempt. Do you have an algorithm or some code that you have tried? We may be able to point you in the right direction if we can see what you are thinking...

I tried this one, but there is some thing wrong >!

package javaapplication3;
import java.util.Scanner;
public class main {


    public static void main(String[] args) {
        Scanner read= new Scanner(System.in);

        int count,i,number;
        System.out.print("Enter the number: ");
        number= read.nextInt();
        if (number%2==0)
            System.out.println(" Plz enter an odd number");
        else
              System.out.println(" The number is odd ");

        count=1;
        while (count<= number)
        {
            i=1;
            while(i<=count)
            {
            System.out.print(count);
            i=i+1;
            }
            System.out.println();
            count=count+1;
        }

            count=(number-1);
        while (count<=(number-1))
        {
            i=1;
            while(i<=count)
            {
            System.out.print(count);
            i=i+1;
            }
            System.out.println();
        count=count-1;

    }

        }     

}

Hi alreem,
please next time use the code-tags to enclose your code,

public static void main(String[] args) {
        Scanner read= new Scanner(System.in);

        int count,i,number;
        System.out.print("Enter the number: ");
        number= read.nextInt();
        if (number%2==0){
            System.out.println(" Plz enter an odd number");
            // here you should read the number again until it's odd or 
            // use System.exit() to exit the program
        }
        else
            System.out.println(" The number is odd ");

        count=1;
        //while (count<= number){
        while (count<= (number/2)+1){    // loop until you get to the middle number
            i=1;
            while(i<=count){
                //System.out.print(count);
                System.out.print("+");
                i=i+1;
            }
            System.out.println();
            count=count+1;
        }

        //count=(number-1); // this will go into an infinite loop           
        //while (count<=(number-1)){ //since the counter will be always 
                        // smaller then (number-1)
        count = (number/2); // now starting from the middle
        while (count > 0){ // go backward 
            i=1;
            while(i<=count){
                // System.out.print(count);
                System.out.print("+");
                i=i+1;
            }
            System.out.println();
            count=count-1;
        }       
    }

anyway I hope I didn't just served the code to you and that you will understand what's modified

GL

hi ,

I understand it ,, but if I want the pattern like this
...............+
.............+++
..........+++++
........+++++++
......... +++++
............+++
..............+
hoW i will do it ...?
WITHOUT THE DOTS.


Thanks So Much 4 helpinG ..
=)

>>?

You should find a formula for computing the withe spaces for each line and add it to the code.
Something like:

  • for the longest line you need 7 "+" signs
  • then for the first line you need 1 "+" sign and 3 spaces in the right side
  • for the second line you need 3 "+" signs and 2 spaces
  • for the third line you need 5 "+" signs and 1 space
    so we have ...
    space_characters = (longest_line + 1) / 2 - line_number;
    plus_characters = line_number *2 - 1;

of course in your code, inside while loops you will use:

int space_characters = (number + 1) / 2 - count;
int plus_characters = count * 2 -1;
for (int k = 0; k < space_characters; k++)
  System.out.println(" ");
for (int k = 0; k < plus_characters; k++)
  System.out.println("+");

or something like that ...
though you might not get the same pattern on screen because the width of the " " sign might be smaller than the one of the "+"

emm

how I do it ?

Plz , help me ...


I did not know how to do it . . ?

Try this:

public static void main(String[] args) {
		Scanner read= new Scanner(System.in);

		int count,i,number;
		System.out.print("Enter the number: ");
		number= read.nextInt();
		if (number%2==0){
			System.out.println(" Plz enter an odd number");
			// here you should read the number again until it's odd or 
			// use System.exit() to exit the program
		}
		else
			System.out.println(" The number is odd ");

		count=1;
		//while (count<= number){
		while (count<= (number/2)+1){	// loop until you get to the middle number
			int space_characters = (number + 1) / 2 - count;
			int plus_characters = count * 2 -1;
			for (int k = 0; k < space_characters; k++)
			  System.out.print(" ");
			for (int k = 0; k < plus_characters; k++)
			  System.out.print("+");
			}
			System.out.println();
			count=count+1;
		}
		
		//count=(number-1); // this will go into an infinite loop 			
		//while (count<=(number-1)){ //since the counter will be always 
						// smaller then (number-1)
		count = (number/2); // now starting from the middle
		while (count > 0){ // go backward 
			int space_characters = (number + 1) / 2 - count;
			int plus_characters = count * 2 -1;
			for (int k = 0; k < space_characters; k++)
			  System.out.print(" ");
			for (int k = 0; k < plus_characters; k++)
			  System.out.print("+");
			}
			System.out.println();
			count=count-1;
		}		
	}

the firs part it give this pattern and the run does not stop .. !

+

+

+

and when i put the second part , it gives me an error for all . . !

package javaapplication47;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner read= new Scanner(System.in);
        int i,k, number;
        System.out.print (" enter the number: ");
        number= read.nextInt ();
        

        i=1;
        while (i<=number)
        {
            k=1;
            while (k<=(number-i)){
                System.out.print(" ");
                k++; }
            k=1;
            while (k<=i){
                System.out.print("+");
                k++;}
            k=i-1;
            while (k>=1){
                System.out.print("+");
                k--;}
            System.out.println();
            i++;
            }
        }

    }

I do the first part ..
how I can inverse it ?

go backward from i = number -1;
while (i > 0)
and replace i++ with i--

i = number - 1;
		while (i>0)
		{
			k=1;
			while (k<=(number-i)){
				System.out.print(" ");
				k++; }
			k=1;
			while (k<=i){
				System.out.print("+");
				k++;}
			k=i-1;
			while (k>=1){
				System.out.print("+");
				k--;}
			System.out.println();
			i--;
        }
    }

Thanx alot =)

write aprogramto generate a business travel express attaachment for an income tax return. The program should request ias input the name of the organization visited, the date and location of the visit,and the expenses for meals and entertainment,airplane fare,loding,and taxi fares.(Only 50% of the expenses for meals and entertainment are deductible) . Sub procedures should be used for the input and output .

write aprogramto generate a business travel express attaachment for an income tax return. The program should request ias input the name of the organization visited, the date and location of the visit,and the expenses for meals and entertainment,airplane fare,loding,and taxi fares.(Only 50% of the expenses for meals and entertainment are deductible) . Sub procedures should be used for the input and output .

One, the rule is that we don't help without effort on your part. Two, you should start your own thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.