Tuesday, July 3, 2018

Java: Standard Input / Output

case1: input from same line, out in different lines
import java.util.*;

public class Stdio1 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            int a = scan.nextInt();  //Takes input from the same line
            System.out.println(a);   //but gives out each number in separate lines
        }
      
    }
}

case 2: input from same line and output in same line without gap
import java.util.*;

public class Stdio2 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            int a = scan.nextInt();  //Takes input from the same line
            System.out.print(a);   //gives out each number in same line without any space

           
        }
      
    }
}

case 3: input from same line and output in same line , with space
import java.util.*;

public class Stdio3 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            int a = scan.nextInt();  //Takes input from the same line
            System.out.print(a+" ");   //gives out each number in same line with  space

           
        }
      
    }
}

case 4: Input from different lines and output also on different lines
import java.util.*;

public class Stdio4 {

    public static void main(String[] args) {
        int a=0;
        int arr[] = new int[3];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
             a = scan.nextInt();  //Takes input from separate lines
             arr[i]=a;

           
        }
        for(int i=0;i<3;i++)
        {
             System.out.println(arr[i]);   //outputs in separate lines also
        }
       
    }
}

case 5 Input from different lines , but output in same line with space
import java.util.*;

public class Stdio5 {

    public static void main(String[] args) {
        int a=0;
        int arr[] = new int[3];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
             a = scan.nextInt();  //Takes input from separate lines
             arr[i]=a;

           
        }
        for(int i=0;i<3;i++)
        {
             System.out.print(arr[i]+" ");   //outputs in same line with space
        }
       
    }
}


No comments:

Post a Comment

इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर

 इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...