mansi kbchawla
mansi kbchawla

Reputation: 69

Why i am getting this output while printing numbers numbers of a particular interval

I am trying to print the the numbers in between particular intervals by taking starting, ending interval range of a particular interval but I am not able to get the correct output.

I am using Scanner class to take input from user.
I have used start1, start2 and end1, end2 of 2 intervals to show their starting and ending range.

import java.util.Scanner;
public class Smallest234Number
{
    public static void smallest2NoInterval(int p[],int x[])  
    {
        System.out.println("First interval");

        for(int k=0;k<p.length;k++)
        {
            System.out.println(p[k++]);
        }
        System.out.println("Second Interval");

        for(int k=0;k<x.length;k++)
        {
            System.out.println(x[k++]);
        }
    }

    public static void main(String[] args)
    {
        int ar1[]=new int[10];
        int ar2[]=new int[10];

        int i=0;
        int s=0;

        // i have taken 2 intervals for a base
        System.out.println("Enter the first Interval");
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first of starting interval");
        int startno1=sc.nextInt();
        //now take input from user startno

        System.out.println("Enter the end of  starting interval");
        int endno1=sc.nextInt();
        //now take input from user endno

        System.out.println("Enter the second interval");
        System.out.println("Enter the first of Second interval");
        int startno2=sc.nextInt();

        //now take input of 2Interval  from user startno1

        System.out.println("Enter the end of Second interval");
        int endno2=sc.nextInt();

        //now take input of 2Interval  from user endno1

        System.out.println("Enter the first interval nos only between start   no and end no");

        System.out.println("Enter the first interval nos only between   start no and end no");

       for(int a=startno1;a<=endno1;a++)
       {
            int inputNo=sc.nextInt();


            if(inputNo<=endno1&&inputNo>=startno1)
            {
                ar1[s]=inputNo;
                s++;
            }
        }
        System.out.println("Enter the Second interval nos only between start no1 and end no1");

       for(int a=startno2;a<=endno2;a++)
       {
            int inputNo=sc.nextInt();


            if(inputNo<=endno1&&inputNo>=startno1)
            {
                ar2[i]=inputNo;
                i++;
             }

        }
        smallest2NoInterval(ar1,ar2);
    }
}

**Output Shown:**

Enter the first Interval
Enter the first of starting interval
1
Enter the end of  starting interval
3
Enter the second interval
Enter the first of Second interval
2
Enter the end of Second interval
4
Enter the first interval nos only between start no and end no
Enter the first interval nos only between start no and end no
1
2
3
Enter the Second interval nos only between start no1 and end no1
2
3
4
First interval
1
3
0
0
0
Second interval
2
0
0
0
0

Upvotes: 0

Views: 68

Answers (1)

Sanjeev
Sanjeev

Reputation: 9946

you are using same variable i for indexing in both the arrays ar1 and ar2 that's why your program puts integers on first two places in ar1 and than 3rd place in ar2. You shall use different index variables or reset i=0 just before putting values in ar2.

EDIT Another problem is in your printing logic

for(int k=0;k<p.length;k++)
{
    System.out.println(p[k++]);
}
System.out.println("Second Interval");

for(int k=0;k<x.length;k++)
{
    System.out.println(x[k++]);
}

you are incrementing index variable twice in the loop Please correct it like this:

for(int k=0;k<p.length;k++)
{
    System.out.println(p[k]);
}
System.out.println("Second Interval");

for(int k=0;k<x.length;k++)
{
    System.out.println(x[k]);
}

Upvotes: 1

Related Questions