Ticker

6/recent/ticker-posts

Header Ads Widget

Input and Output program in Java

Input and Output program in Java


Introduction
In this tutorial we will learn how to take a user input and then show as output based on input.


This tutorial will give you the clear idea how to perform different  output and input operations.
With the help of various examples ,so they you can easily use them in further programs easily.

Lets begin!!!!!!

Example 1)Print text on the screen.

Q 1) Write a Java program to print the "Bigfootcode" on the screen.o

Sol.

class HelloWorld {
public static void main(String[] args) {
System.out.println("Bigfootcode"); 
   }



Output:


Bigfootcode



Note:-
you can change following interchangeable with your ease of use.

System.out.println(); or

System.out.print(); or

System.out.printf();


Example 2) Print the pre defined variable value on the screen.

Q 2)Write a program to print predefine value of variable using Java.

Sol.



public class Main {
public static void main(String[] args) {
int i=5;
System.out.print(i);
}
}


Output:


5


Example 3) Print user input as Output 

Q 3) Write a java program that take a input from user and then print that as output.

Sol.


import java.util.Scanner;

class GetInputFromUser
{
  public static void main(String args[])
  {
     int num;
     float fnum;
     String str;
 
     Scanner in = new Scanner(System.in);
 
     //Get input String
     System.out.println("Enter a string: ");
     str = in.nextLine();
     System.out.println("Input String is: "+str);
 
     //Get input Integer
     System.out.println("Enter an integer: ");
     num = in.nextInt();
     System.out.println("Input Integer is: "+num);
 
     //Get input float number
     System.out.println("Enter a float number: ");
     fnum = in.nextFloat();
     System.out.println("Input Float number is: "+fnum); 
  }


Here above we learnt how we can take different type of input from user like number , string and of course a float number. The way is almost same in all case but the differnce lie only while taking it input. You might notice the change in all the above .
Then also let see what is different in all .


      str = in.nextLine();
    
     num = in.nextInt();
    
     fnum = in.nextFloat();

Look at the bold part where lie all the difference,now i am sure you definitely understand how to take input of various type rest the program is same just like above.

Lets see output of the following program,if you understand the code above.so this program output as follows.

Output: 

Enter a string:
Bigfootcode
Input String is: Bigfootcode
Enter an integer:
007
Input Integer is: 7
Enter a float number:
0.07
Input Float number is: 0.07


After seeing the output you might now have clear idea about how input and output look like.


Some more questions like this you can find out on our website to check more questions for practice 

Post a Comment

0 Comments