2. Programe to interchange the
values of two variables
In C++ In Java
#include<iostream>
using namespace std;
int main()
{
int num,num1,replace;
cout<<"entre number 1"<<endl;
cin>>num;
cout<<"entre number 2"<<endl;
cin>>num1;
replace=num;
num=num1;
num1=replace;
cout<<"after replacing
"<<endl<<num<<endl;
cout<<"after replacing "<<endl<<num1;
}
public static void main(String[] args) {
int num,num1,replace;
Scanner a=new Scanner(System.in);
System.out.println("entre number 1");
num=a.nextInt();
System.out.println("entre number 2");
num1=a.nextInt();
replace=num;
num=num1;
num1=replace;
System.out.println("after replacing
"+num);
System.out.println("after replacing
"+num1);
}
3. Programe to find absolute of a
negative number(Without if else)
In C++ In Java
#include<iostream>
using namespace std;
int main()
{
int num,num1,replace;
cout<<"entre negative number
"<<endl;
cin>>num;
int abs=num*-1;
cout<<"abs value is
"<<endl<<abs<<endl;
}
public static void main(String[] args) {
int num;
Scanner a=new
Scanner(System.in);
System.out.println("entre
negative number");
num=a.nextInt();
int abs=num*-1;
System.out.println("absolute
value is "+abs);
}
4. User entered 3 digit number break
it in single digits and display
accordingly
In c++ In Java
#include<iostream>
using namespace std;
int main()
{
int num,bre,bre1,bre2,b,c;
cout<<"entre 3 digit number "<<endl;
cin>>num;
bre=num/100;
c=num/10;
bre1=c%10;
bre2=num%10;
cout<<"first digit is "<<endl<<bre<<endl;
cout<<"Second digit is "<<endl<<bre1<<endl;
cout<<"third digit is "<<endl<<bre2<<endl;
}
public static void main(String[] args) {
// TODO code application logic here
int number,bre,bre1,bre2,b,c;
Scanner a =new Scanner(System.in);
System.out.println("entre 3 digit
number");
number=a.nextInt();
bre=number/100;
c=number/10;
bre1=c%10;
bre2=number%10;
System.out.println("first digit is "+bre);
System.out.println("second digit is
"+bre1);
System.out.println("third digit is
"+bre2);
}
5. Difference between / and %
Division / Modulus %
4/2 will give 2
10/5 will give 2
15/3 will give 3
21/2 will give 10
4%2 will give 0
10%2 will give 0
15/3 will give 0
21/2 will give 5
Help full tool used to break
the number in single digits
6. Marks entered by user find average
of these marks
In c++ In Java
#include<iostream>
using namespace std;
int main()
{
int num,num1,num2;
cout<<"entre marks of sub1 "<<endl;
cin>>num;
cout<<"entre marks of sub2 "<<endl;
cin>>num1;
cout<<"entre marks of sub3 "<<endl;
cin>>num2;
int avg=(num+num1+num2)/3;
cout<<"avg is "<<endl<<avg<<endl;
}
public class JavaApplication10 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int number,number1,number2;
Scanner a =new Scanner(System.in);
System.out.println("entre marks of sub 1");
number=a.nextInt();
System.out.println("entre marks of sub 2");
number1=a.nextInt();
System.out.println("entre marks of sub 3");
number2=a.nextInt();
int avg=(number+number1+number2)/3;
System.out.println("avarage is "+avg);
}
}
7. Temperature in fahrenheit is input
convert it into celsius
In C++ In Java
#include<iostream>
using namespace std;
int main()
{
float far,cel;
cout<<"entre temperature in farenhet
"<<endl;
cin>>far;
cel=(far-32)*5/9;
cout<<"temp in celseius is
"<<endl<<cel<<endl;
}
*/
public class JavaApplication10 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double far,cel;
Scanner a =new Scanner(System.in);
System.out.println("entre temparature in
farenheit ");
far=a.nextDouble();
cel=(far-32)*5/9;
System.out.println("Temp in cel is "+cel+"degree
celcius");
}
}
8. Ayaz basic salary is input through the keyboard. His dearness
allowance is 40% of basic salary, and house rent allowance is 20%
of basic salary. Write a program to calculate his gross salary.
In C++ In Java
#include<iostream>
using namespace std;
int main()
{
float bsal,da,hr,gsal;
cout<<"entre basic salary "<<endl;
cin>>bsal;
da=0.4*bsal;
hr=0.2*bsal;
gsal=bsal-da-hr;
cout<<"Gross salar is
"<<endl<<gsal<<endl;
}
public class JavaApplication10 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double bsal,da,hr,gsal;
Scanner a =new Scanner(System.in);
System.out.println("Ayaz entre your basic
salary ");
bsal=a.nextInt();
da=0.4*bsal;
hr=0.2*bsal;
gsal=bsal-da-hr;
System.out.println("gross salary is"+gsal);
}
}
9. If a three-digit number is input through the keyboard, write a program to print
a new number by adding one to each of its digits. For example if the number
that is input is 123 then the output should be displayed as 234.
In C++ In Java
#include<iostream>
using namespace std;
int main()
{
int number,bre,bre1,bre2,b,c,nbre,nbre1,nbre2;
cout<<"entre number"<<endl;
cin>>number;
bre=number/100;
nbre=bre+1;
c=number/10;
bre1=c%10;
nbre1=bre1+1;
bre2=number%10;
nbre2=bre2+1;
cout<<"after adding 1 is"<<endl;
cout<<nbre;
cout<<nbre1;
cout<<nbre2;
}
int
number,bre,bre1,bre2,b,c,nbre,nbre1,nbre2;
Scanner a =new Scanner(System.in);
number=a.nextInt();
bre=number/100;
nbre=bre+1;
c=number/10;
bre1=c%10;
nbre1=bre1+1;
bre2=number%10;
nbre2=bre2+1;
System.out.println("after adding 1 is");
System.out.print(nbre);
System.out.print(nbre1);
System.out.print(nbre2);
10. Radius is input from user find area
In C++ In Java
#include<iostream>
using namespace std;
int main()
{
float radius,area;
cout<<"entre radius"<<endl;
cin>>radius;
area=3.14*radius*radius;
cout<<"radius is"<<endl;
cout<<area;
}
public class JavaApplication10 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double radius,area;
Scanner a =new Scanner(System.in);
System.out.println("entre radius");
radius=a.nextDouble();
area=3.14*radius*radius;
System.out.println("area is"+area);
}
}
11. Key points
In C++ In Java
In order to give one line gap we
use endl
In C++ endl will give the one
line gap and cursor will go to
next line
cout<<"entre radius"<<endl;
In above instruction entre ra
dius will print and <<endl will
provide the one line gap
Join string with variable we use
string << variablename;
In order to give one line gap
we use System.out.println
Here ln will give one line
break if we use
System.out.print it do not
give one line break and print
in the same line
If we want to join string with
variable value we use + sign