Sie sind auf Seite 1von 12

Lecture No 3

Some Examples And Things To Remember


C++ And Java

Programe to interchange the values


of two variables
In C++
#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;


}

In Java
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);

Programe to find absolute of a


negative number(Without if else)
In C++

In Java

#include<iostream>
using namespace std;
int main()
{

public static void main(String[] args) {

int num;

Scanner a=new

int num,num1,replace;

cout<<"entre negative number


"<<endl;

cin>>num;
int abs=num*-1;

cout<<"abs value is
"<<endl<<abs<<endl;

Scanner(System.in);

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

num=a.nextInt();

int abs=num*-1;

System.out.println("absolute
value is "+abs);

User entered 3 digit number break it in


single digits and display accordingly
In c++
#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;

In Java
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);

Difference between / and


%
Division

4/2 will give 2


10/5 will give 2
15/3 will give 3
21/2 will give 10

Modulus %

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

Marks entered by user find average


of these marks
In c++
#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;

In Java
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);
}

Temperature in fahrenheit is input


convert it into celsius
In C++
#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;

In Java
*/
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");

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++
#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;

In Java
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);

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++
#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;

In Java
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);

Radius is input from user find area


In C++
#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;

In Java
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);

Key points
In C++

In Java

In order to give one line gap

In order to give one line

we use endl
gap we use
In C++ endl will give the one
System.out.println
line gap and cursor will go to Here ln will give one line
next line
break if we use
cout<<"entre radius"<<endl;
System.out.print it do
In above instruction entre ra
not give one line break
dius will print and <<endl will
and print in the same line
provide the one line gap
If we want to join string
Join string with variable we
with variable value we use
use string <<
+ sign
variablename;

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

Das könnte Ihnen auch gefallen