Set of instructions.
2) What are instructions?
Set of code for certain task.
3) What is code?
Some symbols and literals (words) to perform the task.
4) What is Computer Language?
Division of Computer language:
LANGUAGE PROCESSOR:
HIGH LEVEL LANGUAGE TO LOW LEVEL LANGUAGE = COMPILER OR INTERPRETER
JAVA LANGUAGE PROCESSOR OR EDITOR = BLUEJ, ECLIPSE, NETBEANS.
Java Programming Analysis
Program to print a line
public class p1
{
public static void main(String args[])
{
System.out.print("hello world");
}
}
Output:
hello world
Explanations:
Line 1: [ public class p1]
public:
Here the term ‘public’ means this class is accessible from any other classes. You can also change it to ‘private’ but then this class will be restricted to selected part of the program.
class:
This is the class definition of certain task. Actually we are making a new type of data which has a name to identify.
p1:
this is a name of a class to identify the class.
Line 2: [ public static void main(String args[])]
static:
You can declare a method as static or non-static and it has certain characteristics. Static means you can run it class name and you do not need to declare any instance name as object. Whereas for non-static methods you need to have an instance name to access its components.
void:
It will generate no return to the compiler. Actually there must be return from every functions in this language but ‘void’ keyword allows you to run the program without returning any value to the compiler.
main:
This is the main part of the program to run the all possible steps one by one. You need to use all accessories in this area. The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program.
String:
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. In easy language this is a word.
String greeting = "Hello world!";
args[]:
String args is an array of strings of Java string class, i.e., java. lang. String class. The supplied command-line arguments are stored as an array of String objects in String args in java. Here, the string type is referred to as args(arguments).
Line 3: [System.out.print("hello world");]
System.out.print:
To define System. out. println() in Java, it is a simple statement that prints any argument you pass and adds a new line after it. println() is responsible for printing the argument and printing a new line.
1.2 Write a program to addition two number
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1,num2,num3;
System.out.println("Enter the 1st number: ");
num1=input.nextInt();
System.out.println("Enter the 2nd number: ");
num2=input.nextInt();
num3=num1+num2;
System.out.println("Addition: "+num3);
}
}
Output:
Enter the 1st number:
10
Enter the 2nd number:
20
Addition:
30
Explanation:
[import]:
import is a Java keyword. It declares a Java class to use in the code below the import statement. Once a Java class is declared, then the class name can be used in the code without specifying the package the class belongs to.
[java.util.Scanner]:
The Scanner class is used to get user input, and it is found in the java.util
package.
To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings.
|
Method |
Description |
|
nextBoolean() |
Reads a boolean value from the user |
|
nextByte() |
Reads a byte value from the user |
|
nextDouble() |
Reads a double value from the user |
|
nextFloat() |
Reads a float value from the user |
|
nextInt() |
Reads a int value from the user |
|
nextLine() |
Reads a String value from the user |
|
nextLong() |
Reads a long value from the user |
|
nextShort() |
Reads a short value from the user |
1.3 Write a program to calculate the Addition, Subtraction, Multiplication, and Division of two numbers:-
------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int num1,num2,add,sub,mul,div;
System.out.println("Enter the first number:");
num1=input.nextInt();
System.out.println("Enter the second number:");
num2=input.nextInt();
add=num1+num2;
sub=num1-num2;
mul=num1*num2;
div=num1/num2;
System.out.println("The Addition of 2 numbers is:"+add);
System.out.println("The Subtraction of 2 numbers is:"+sub);
System.out.println("The Multiplication of 2 numbers is:"+mul);
System.out.println("The Division of 2 numbers is:"+div);
}
}
Output:
-----------------------
Enter the first number:
26
Enter the second number:
13
The Addition of 2 numbers is:39
The Subtraction of 2 numbers is:13
The Multiplication of 2 numbers is:338
The Division of 2 numbers is:2
-------------------------------------------------------------------------------------------------
1.4 Write a program to perform an addition three numbers
-------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int num1,num2,num3,add;
System.out.println("Enter the first number:");
num1=input.nextInt();
System.out.println("Enter the second number:");
num2=input.nextInt();
System.out.println("Enter the Third number:");
num3=input.nextInt();
add=num1+num2+num3;
System.out.println("The Addition of 3 numbers is:"+add);
}
}
Output:
-----------------------
Enter the first number:
15
Enter the second number:
23
Enter the Third number:
45
The Addition of 3 numbers is:83
---------------------------------------
1.5 Write a program to calculate the area and circumference of a circle
--------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
double r,cir,area;
System.out.println("Enter the Radius:");
r=input.nextDouble();
area=r*r*22/7;
cir=2*22/7*r;
System.out.println("The Area is "+area);
System.out.println("The Circumference is "+cir);
}
}
Output:
-----------------------
Enter the Radius:
2
The Area is 12.571428571428571
The Circumference is 12.0
----------------------------------------------------------------------------------------------------------
1.6 Write a program to calculate the area and perimeter of a rectangle
---------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
float l,b,area,peri;
System.out.println("Enter the Lenght: ");
l=input.nextFloat();
System.out.println("Enter the Width : ");
b=input.nextFloat();
area=l*b;
peri=2*(l+b);
System.out.println("The Area is: "+area);
System.out.println("The Perimeter is : "+peri);
}
}
Output:
-----------------------
Enter the Lenght:
10
Enter the Width :
5
The Area is: 50.0
The Perimeter is : 30.0
------------------------------------------------------------------------
1.7 Write a program to calculate the distance covered by a wheel in Kilometer if the radius in centimeter is given through the keyboard:
-----------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
float r,a,c,rot,dis;
System.out.println("Enter the radius(cm): ");
r=input.nextFloat();
System.out.println("Enter the rotation: ");
rot=input.nextFloat();
c=2*r*22/7;
dis=c*rot/100000;
System.out.println("Circumference: "+c);
System.out.println("Distance covered in km: "+dis);
}
}
Output:
-----------------------
Enter the radius(cm):
5
Enter the rotation:
100
Circumference: 31.428572
Distance covered in km: 0.03142857
------------------------------------------------------------------------
1.8 Write a program to calculate total cost for coloring a room if height, width, length, and color rate us given through the keyboard
-----------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
float h,w,l,cr,ta,tc;
System.out.println("Enter the Lenght(feet):");
l=input.nextFloat();
System.out.println("Enter the Width(feet):");
w=input.nextFloat();
System.out.println("Enter the Height(feet):");
h=input.nextFloat();
System.out.println("Enter the rate for the Coloring as per Square Feet :");
cr=input.nextFloat();
ta=(2*(l*h))+(2*(w*h))+(h*w);
tc=ta*cr;
System.out.println(" The Total Area is : "+ta);
System.out.println("Total Cost:"+tc);
}
}
Output:
-----------------------
Enter the Lenght(feet):
10
Enter the Width(feet):
20
Enter the Height(feet):
9
Enter the rate for the Coloring as per Square Feet :
100
The Total Area is : 720.0
Total Cost:72000.0
------------------------------------------------------------------------
1.9 Write a program to calculate the distance in Hectometer, Decameter, Meter, Decimeter, Centimeter, Millimeter, Inch, Feet and Gauge if it is entered as in Kilometer
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double km,hm=0,dcm=0,m=0,dm=0,cm=0,mm=0,in=0,ft=0,g=0;
System.out.println("Enter the distance in Kilometer: ");
km=input.nextDouble();
hm=km*10;
dcm=hm*10;
m=dcm*10;
dm=m*10;
cm=dm*10;
mm=cm*10;
in=cm*2.54;
ft=in/12;
g=ft/3;
System.out.println("Distance in Kilometer: "+km);
System.out.println("Distance in Hectometer: "+hm);
System.out.println("Distance in Decameter: "+dcm);
System.out.println("Distance in Meter: "+m);
System.out.println("Distance in Decimeter: "+dm);
System.out.println("Distance in Centimeter: "+cm);
System.out.println("Distance in Millimeter: "+mm);
System.out.println("Distance in Inch: "+in);
System.out.println("Distance in Feet: "+ft);
System.out.println("Distance in Gram: "+g);
}
}
Output:
-----------------------
Enter the distence in Kilometer:
20
Distance in Kilometer: 20.0
Distance in Hectometer: 200.0
Distance in Decameter: 2000.0
Distance in Meter: 20000.0
Distance in Decimeter: 200000.0
Distance in Centimeter: 2000000.0
Distance in Millimeter: 2.0E7
Distance in Inch: 5080000.0
Distance in Feet: 423333.3333333333
Distance in Gram: 141111.1111111111
------------------------------------------------------------------------
1.10 Write a program to calculate the retail price for a product if the name and rate and quantity are given through the keyboard
------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String pn;
double r=0,total=0,qty=0,cc=0,vat=0,p=0,rp=0;
System.out.println("Enter the product name: ");
pn=input.next();
System.out.println("Enter the rate: ");
r=input.nextDouble();
System.out.println("Enter the quantity: ");
qty=input.nextDouble();
total=r*qty;
cc=total*.1;
vat=total*0.05;
p=total*0.35;
rp=total+cc+p+rp;
System.out.println("Retail price: "+rp);
}
}
Output:
-----------------------
Enter the product name:
Milk
Enter the rate:
35
Enter the quantity:
4
Retail price: 203.0
------------------------------------------------------------------------
1.11 Write a program to calculate the estimated population on a certain period########
----------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double p,r,t,a,b,ci;
System.out.println("Ener the current population of the area: ");
p=input.nextDouble();
System.out.println("Ener the rate: ");
r=input.nextDouble();
System.out.println("Ener the time: ");
t=input.nextDouble();
a=1+r/100;
b=Math.pow(a, t);
ci=p*b;
System.out.println("The Estimated Population: "+ci);
}
}
Output:
-----------------------
Ener the current population of the area:
100000
Ener the rate:
200
Ener the time:
5
The Estimated Population: 2.43E7
------------------------------------------------------------------------
1.13 Write a program to calculate the simple interest and final amount if principal, interest rate and time in years are given through the keyboard
------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
double p,t,r,si,ta;
System.out.println("Enter the Principal amount= ");
p=input.nextFloat();
System.out.println("Enter the time in a year= ");
t=input.nextFloat();
System.out.println("Enter the rate= ");
r=input.nextFloat();
si=p*t*r/100;
ta=p+si;
System.out.println("The simple interest = "+si);
System.out.println("Th final amount= "+ta);
}
}
Output:
-----------------------
Enter the Principal amount=
1000
Enter the time in a year=
5
Enter the rate=
8
The simple interest = 400.0
Th final amount= 1400.0
------------------------------------------------------------------------
1.14 Write a program to calculate the simple interest and final amount with installment facilities if principal, interest rate and time in years are given through the keyboard
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
double p,t,r,si,amt,y,m,d;
System.out.println("Enter the Principal amount= ");
p=input.nextFloat();
System.out.println("Enter the time in a year= ");
t=input.nextFloat();
System.out.println("Enter the rate= ");
r=input.nextFloat();
si=p*t*r/100;
amt=p+si;
y=amt/t;
m=y/12;
d=m/365.25;
System.out.println("The simple interest = "+si);
System.out.println("The final amount= "+amt);
System.out.println("The yearly installment= "+y);
System.out.println("The monthly installment= "+m);
System.out.println("The daily installment= "+d);
}
}
Output:
-----------------------
Enter the Principal amount=
20000
Enter the time in a year=
6
Enter the rate=
6
The simple interest = 7200.0
The final amount= 27200.0
The yearly installment= 4533.333333333333
The monthly installment= 377.77777777777777
The daily installment= 1.034299186249905
------------------------------------------------------------------------
1.15 Write a program if input is 1 then output is 0 if input is 0 output is 1
-----------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Enter the no: ");
a= input.nextInt();
b=1-a;
System.out.println("Result= "+b);
}
}
Output:
-----------------------
Enter the no:
1
Result= 0
------------------------------------------------------------------------
1.16 Write a program to create a salary sheet
-----------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int basic;
double da,ta,hra,it,pf,gp,np;
System.out.println("Enter the Basic salary");
basic=input.nextInt();
da=basic*0.45;
ta=basic*0.30;
hra=basic*0.20;
it=basic*0.04;
pf=basic*0.08;
gp=da+ta+hra+basic;
np=gp-it-pf;
System.out.println("Basic salary is: "+basic);
System.out.println("D.A(Dearness_Allowance) is: "+da);
System.out.println("T.A(Travel_Allowance) is: "+ta);
System.out.println("H.R.A(House_Rent_Allowens) is: "+hra);
System.out.println("I.T(Income_Tax) is: "+it);
System.out.println("P.F(Provedent_Fund) is: "+pf);
System.out.println("G.P(Gross_Pay) is: "+gp);
System.out.println("N.P(Net_pay) is: "+np);
}
}
Output:
-----------------------
Enter the Basic salary
10000
Basic salary is: 10000
D.A(Dearness_Allowance) is: 4500.0
T.A(Travel_Allowance) is: 3000.0
H.R.A(House_Rent_Allowens) is: 2000.0
I.T(Income_Tax) is: 400.0
P.F(Provedent_Fund) is: 800.0
G.P(Gross_Pay) is: 19500.0
N.P(Net_pay) is: 18300.0
------------------------------------------------------------------------
1.17 Write a program to convert Celsius to Fahrenheit
-----------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
float f,c;
System.out.println("Enter the Celsius: ");
c=input.nextFloat();
f=(9*c+160)/5;
System.out.println("Fahrenheit: "+f);
}
}
Output:
-----------------------
Enter the Celsius:
-40
Fahrenheit: -40.0
------------------------------------------------------------------------
1.18 Write a program to convert Fahrenheit to Celsius
------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int basic;
float f,c;
System.out.println("Enter the Fahrenheit: ");
f=input.nextFloat();
c=(5*f-160)/9;
System.out.println("Celsius: "+c);
}
}
Output:
-----------------------
Enter the Fahrenheit:
104
Celsius: 40.0
------------------------------------------------------------------------
1.19 Write a program to find largest among three numbers using conditional operator
----------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a,b,c,d;
System.out.println("Enter 1st number: ");
a=input.nextInt();
System.out.println("Enter 2nd number: ");
b=input.nextInt();
System.out.println("Enter 3rd number: ");
c=input.nextInt();
d=(a>b?(a>c?a:c):b>c?b:c);
System.out.println("The large number is: "+d);
}
}
Output:
-----------------------
Enter 1st number:
2
Enter 2nd number:
3
Enter 3rd number:
4
The large number is: 4
------------------------------------------------------------------------
1.20 Write a program for swapping of two numbers
-----------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a,b,c;
System.out.println("Enter 1st number for swap: ");
a=input.nextInt();
System.out.println("Enter 2nd number for swap: ");
b=input.nextInt();
c=a;
a=b;
b=c;
System.out.println("Swaped result a= "+a);
System.out.println("Swaped result b= "+b);
}
}
Output:
-----------------------
Enter 1st number for swap:
5
Enter 2nd number for swap:
2
Swaped result a= 2
Swaped result b= 5
------------------------------------------------------------------------
1.21 Write a program for swapping of two numbers without using third variable
-----------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Enter 1st number for swap: ");
a=input.nextInt();
System.out.println("Enter 2nd number for swap: ");
b=input.nextInt();
a=a+b;
b=a-b;
a=a-b;
System.out.println("Swaped result a= "+a);
System.out.println("Swaped result b= "+b);
}
}
Output:
-----------------------
Enter 1st number for swap:
8
Enter 2nd number for swap:
7
Swaped result a= 7
Swaped result b= 8
------------------------------------------------------------------------
1.22 If a five digit number is input through the keyboard, write a program to reverse the number
--------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
long a,b=0;
System.out.println("Enter the five digit number: ");
a=input.nextLong();
b=b*10+a%10;
a=a/10;
b=b*10+a%10;
a=a/10;
b=b*10+a%10;
a=a/10;
b=b*10+a%10;
a=a/10;
b=b*10+a%10;
a=a/10;
System.out.println("The reverse of the Number= "+b);
}
}
Output:
-----------------------
Enter the five digit number:
65895
The reverse of the Number= 59856
------------------------------------------------------------------------
1.23 If a four digit number is input through the keyboard, write a program to the sum of the first and last number
------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
long a,b=0,m,n,x;
System.out.println("Enter the five digit number: ");
a=input.nextLong();
b=a%10;
a=a/10;
m=b;
b=a%10;
a=a/10;
b=a%10;
a=a/10;
b=a%10;
a=a/10;
b=a%10;
a=a/10;
n=b;
x=n+m;
System.out.println("The sum of the first and last digit= "+x);
}
}
Output:
-----------------------
Enter the five digit number:
48965
The sum of the first and last digit= 9
------------------------------------------------------------------------
1.24 In a town, the percentage of men is 52. The percentage of total literacy is 48. If percentage of total literacy men is 35 of the total population, write a program to find the total number of illiterate men and women if the population of the town is 80,000.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double totpop=80000;
double totmen,totlit,ilitmen,totwomen,totlitwomen,ilitwomen;
totmen=(52.0/100.0)*totpop;
System.out.println("Total number of men in the town is: "+totmen);
totlit=(48.0/100.0)*totpop;
System.out.println("Total number of literate in the town is: "+totlit);
ilitmen=(48.0/100.0)*totpop;
System.out.println("Total number of literate men in the town is: "+totmen);
totlitwomen=totlit-ilitmen;
totwomen=totpop-totmen;
ilitmen=totmen-ilitmen;
ilitwomen=totwomen-totlitwomen;
System.out.println("Total number of women is: "+totwomen);
System.out.println("Total number of illiterate men is: "+ilitmen);
System.out.println("Total number of illiterate women is: "+ilitwomen);
}
}
Output:
-----------------------
Total number of men in the town is: 41600.0
Total number of literate in the town is: 38400.0
Total number of literate men in the town is: 41600.0
Total number of women is: 38400.0
Total number of illiterate men is: 3200.0
Total number of illiterate women is: 38400.0
------------------------------------------------------------------------
1.25 A cashier has currency notes of denomination 10, 50 and 100. If the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each domination the cashier will have to give the withdrawer
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int amount, nohun,nofifty,noten;
System.out.println("Enter the amount to be withdrawn: ");
amount=input.nextInt();
nohun=amount/100;
amount=amount%100;
nofifty=amount/50;
amount=amount%50;
noten=amount/10;
System.out.println("Number of hundred notes required: "+nohun);
System.out.println("Number of fifty notes required: "+nofifty);
System.out.println("Number of ten notes required: "+noten);
}
}
Output:
-----------------------
Enter the amount to be withdrawn:
50000
Number of hundred notes required: 500
Number of fifty notes required: 0
Number of ten notes required: 0
------------------------------------------------------------------------
1.26 If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one item
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
-----
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
float sp,cp,profit;
System.out.println("Enter the total selling price: ");
sp=input.nextFloat();
System.out.println("Enter the profit: ");
profit=input.nextFloat();
cp=sp-profit;
cp=cp/15;
System.out.println("Cost price per item is s. = "+cp);
}
}
Output:
-----------------------
Enter the total selling price:
2000
Enter the profit:
250
Cost price per item is s. = 116.666664
------------------------------------------------------------------------
1.27 if a five-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 that number is input is 12391 the output should b displayed as 23402
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int num,a,n;
long newnum=0;
System.out.println("Enter a five digit number(less than 32767): ");
num=input.nextInt();
a=num/10000+1;
n=num%10000;
newnum=newnum+a*10000l;
a=n/1000+1;
n=num%1000;
newnum=newnum+a*1000;
a=n/100+1;
n=num%100;
newnum=newnum+a*100;
a=n/10+1;
n=num%10;
newnum=newnum+a*10;
a=n+1;
newnum=newnum+a;
System.out.println("The new numbers are= "+newnum);
}
}
Output:
-----------------------
Enter a five digit number(less than 32767):
25675
The new numbers are= 36786
------------------------------------------------------------------------
To be continued...........................................................................................................................................................................
No comments:
Post a Comment