0% found this document useful (0 votes)
68 views11 pages

Lecture No 2: How To Take Input From User in C++ JAVA

How to take input from user in c++ and java comparison between method of inputs between these two languages

Uploaded by

Muhammad Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views11 pages

Lecture No 2: How To Take Input From User in C++ JAVA

How to take input from user in c++ and java comparison between method of inputs between these two languages

Uploaded by

Muhammad Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lecture No 2

How to take input from user in C++ JAVA

Input In Java
Input in java occurred through Scanner class
Scanner is the built in class of java.
Scanner class is present in java.util package

so we import this package in our program


We can access this class by creating object
of this class
Always use S capital because java is case
sensitive

Input in java Continued


For taking input from user method will be
Scanner o=new Scanner(System.in);
In the above sentence we use Scanner class

by creating object o
To use Scanner Class we must create an
object that point to this Scanner class
New keyword is used to allocate the
memory
System.in is input stream used to get input
from user

Continued
Scanner a=new Scanner(System.in);
Int number=a.nextInt();
Here a is the object name nextInt is used to

input an integer and stored it in a variable


named as number
Float number=a.nextFloat();
Here nextFloat is used to input float number
and stored it in variable named as float
Double number=a.nextDouble();

Char Input
For character input we use following method
Scanner a =new Scanner(System.in);
Char c=a.next().charAt(0);
We get the string by this method and takes it

only first character by using charAt(0).


As it takes only first character of string.

Input In C++
For taking input from user in c++
We use cout and cin method to display and

take input from the user


For using cout and cin we must use header file
known as #include<iostream> if we dont
include this header file our programme will not
compile and it gives error that cout was not
declared in this scope it takes cout as variable
not function of print.Same case with cin

Input In C++ Continued


After Including #include<iostream>
We use using using namespace std;
Using namespace std; is instructing our C++

compiler to use the standard C++ library. If


you don't give this instruction, then you will
have to usestd::, each time you use a
standard C++ function/entity.
If you dont use Using namespace std our
programme will not compile and throws error
on cout and cin

Input In C++ Continued


For cin we use >>
For cout we use <<
Int a;
Cout<<entre number;
Cin>>a;
Cout will display the string that is entre

number
Cin will take the integer value and store it in
the variable named as a.

Comparison of inputs
In JAVA

In C++

In java we use Scanner

class which is built in class


of java
To access this class we
create an object of this
class
nextInt() will only input
integer values.
We must import
java.util.scanner

In c++ we use cout and

cin built in methods


For using cout and cin we
must include<iostream>
and Using namespace std;
For cout use <<
For cin use >>

Simple Example
In C++
#include<iostream>
using namespace std;
int main()
{

int a,b;

cout<<"entre number 1"<<endl;

cin>>a;

cout<<"entre number 2"<<endl;

cin>>b;

int add;

add=a+b;

cout<<"after addition ans is "<<add;


}
Thanks to Devc++

In Java
*/
public class JavaApplication15 {
/**

* @param args the command line arguments

*/
public static void main(String[] args) {

int a,b;

System.out.println("entre number 1");

Scanner c=new Scanner(System.in);

a=c.nextInt();

System.out.println("entre number 2");

b=c.nextInt();

int add;

add=a+b;

System.out.println("After addition "+add);


}

}
Thanks to netbeans

Thanks alot
Contact me at
muhammadhaseeb562@g
mail.com
Mobile no:0336-5686312

You might also like