CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2015
    Posts
    2

    small help with java

    Hello I am a student and I am trying to write a code that does this

    This project will ask a user to input a number for example "please Enter a vaule of N"

    and a user types 30 and then the java code will display on the scren like this

    0 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 26 27 28 29

    30

    same like that and so and so for if he choose a number more than 30 it will show till the number he chose
    and it has to print 10 numbers each line
    I don't know how to do nothing seems to be working here
    is my code

    Code:
    package arrays;
     
    import java.util.Scanner; 
     public class Arrays{ 
     public static void main(String args[]){ 
     
     
     
     Scanner input = new Scanner(System.in); 
     int N; 
     System.out.print("Please Enter A Value Of 'N': "); 
     
     N = input.nextInt(); 
     
     int rows ; 
     int columns ; 
     int i, j; 
     for (i=0; i < N; i++){ 
     
     for (j=0; j < N; j++) 
     
     { 
     
     System.out.print( i+j +" ") ; 
     } 
     System.out.println(); 
     } 
     
     
     } 
     
     }
    and this is the out put ( hint I chose number 10 for the input )

    that's the out put I got when I run my code

    Code:
    1 2 3 4 5 6 7 8 9 10
    2 3 4 5 6 7 8 9 10 11
    3 4 5 6 7 8 9 10 11 12
    4 5 6 7 8 9 10 11 12 13
    5 6 7 8 9 10 11 12 13 14
    6 7 8 9 10 11 12 13 14 15
    7 8 9 10 11 12 13 14 15 16
    8 9 10 11 12 13 14 15 16 17
    9 10 11 12 13 14 15 16 17 18

  2. #2
    Join Date
    Jun 2011
    Posts
    12

    Re: small help with java

    either use debugger to understand what the second for loop is doing or use this software:
    http://cs.joensuu.fi/jeliot/
    it visualizes the variables quite nicely...

    one way to do it is like this:
    Code:
    import java.util.Scanner; 
     public class Arrays{ 
     public static void main(String args[]){ 
     Scanner input = new Scanner(System.in); 
     int N; 
     System.out.print("Please Enter A Value Of 'N': "); 
     N = input.nextInt(); 
     int i;
     int maxNumbersInRow=10;
     int numbersInRow=0;
     for (i=0; i < N+1; i++){ 
    			System.out.print( i +" ") ;
    			numbersInRow++;
    			if (numbersInRow>=maxNumbersInRow){
    					System.out.println();
    					numbersInRow=0;
    					}
    		}		 
     }
     }

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: small help with java

    Quote Originally Posted by BiG_EvOo View Post
    Hello I am a student and I am trying to write a code that does this

    This project will ask a user to input a number for example "please Enter a vaule of N"

    and a user types 30 and then the java code will display on the scren like this

    0 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 26 27 28 29

    30

    same like that and so and so for if he choose a number more than 30 it will show till the number he chose
    and it has to print 10 numbers each line
    I don't know how to do nothing seems to be working here
    is my code

    Code:
    package arrays;
     
    import java.util.Scanner; 
     public class Arrays{ 
     public static void main(String args[]){ 
     
     
     
     Scanner input = new Scanner(System.in); 
     int N; 
     System.out.print("Please Enter A Value Of 'N': "); 
     
     N = input.nextInt(); 
     
     int rows ; 
     int columns ; 
     int i, j; 
     for (i=0; i < N; i++){ 
     
     for (j=0; j < N; j++) 
     
     { 
     
     System.out.print( i+j +" ") ; 
     } 
     System.out.println(); 
     } 
     
     
     } 
     
     }
    and this is the out put ( hint I chose number 10 for the input )

    that's the out put I got when I run my code

    Code:
    1 2 3 4 5 6 7 8 9 10
    2 3 4 5 6 7 8 9 10 11
    3 4 5 6 7 8 9 10 11 12
    4 5 6 7 8 9 10 11 12 13
    5 6 7 8 9 10 11 12 13 14
    6 7 8 9 10 11 12 13 14 15
    7 8 9 10 11 12 13 14 15 16
    8 9 10 11 12 13 14 15 16 17
    9 10 11 12 13 14 15 16 17 18
    It's probably a bit late for me to reply, but if you haven't figured it out yet, then think about what you are doing with having an outer loop and an inner loop both counting to N. What you are effectively doing is executing your sysout line N*N times - not what you want. You only need a single loop. Think about what the modulo operator does, could it be used to figure out when to start a new line?

  4. #4
    Join Date
    Jul 2015
    Posts
    5

    Re: small help with java

    Can anyone help me to print the series which will ask the user to enter the number of term and print?

    100

    99,98

    97,96,95

    94,93,92,91

    and likewise......

  5. #5
    Join Date
    Aug 2015
    Posts
    5

    Re: small help with java

    Quote Originally Posted by camycent.solutions View Post
    Can anyone help me to print the series which will ask the user to enter the number of term and print?

    100

    99,98

    97,96,95

    94,93,92,91

    and likewise......
    If you ask your question on someone elses thread it will likely be missed by most of the community. You should create a post of your own so that we can answer as a community.

  6. #6
    Join Date
    Sep 2015
    Posts
    10

    Re: small help with java

    Generating N Random Numbers between two specified numbers

    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    void main()
    {
    clrscr();
    int min,max,i,range,r,x;
    unsigned first = time(NULL);
    cout<<"FIRST = " << first <<endl;
    srand(first);
    cout<<"ENTER THE MINIMUM NUMBER :";
    cin>>min;
    cout<<"ENTER THE MAXIMUM NUMBER :";
    cin>>max;
    cout<<"ENTER THE NO.OF TERMS YOU WANT :";
    cin>>x;
    range=max-min+1;
    for(i=0;i<x;i++)
    {
    r=rand()/100%range+min;
    cout<<r<<" ";
    }
    getch();
    }
    Generating N Random Numbers between two specified numbers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured