======================================================================
*1.Write a program to sum of digits of a given number.*
package pkgProgamList;
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String[] args)
{
int n;
Scanner input=new Scanner(System.in);
System.out.println("Enter a Number: ");
n=input.nextInt();
int sum = 0;
while(n!=0)
{
//gives you last digit of the input number
int lastDigit= (n % 10);
sum=sum+lastDigit;
//removes last digit of the input number
n= n / 10;
}
System.out.println("Sum of its digits is: "+sum);
input.close();
}
}
*Output:*
Enter a Number:
123456
Sum of its digits is: 21
======================================================================
*2. Write a Java Program to print Sum of Even Digits of A Number given.*
package pkgProgamList;
import java.util.Scanner;
public class SumOfEvenDigits
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter a Number: ");
n=input.nextInt();
int sum =0;
while(n!=0)
{
int lastDigit=(n%10);
if(lastDigit % 2 ==0)
{
sum=sum+lastDigit;
}
n=n/10;
}
System.out.println("Sum of Even Digits: "+sum);
input.close();
}
}
*Output:*
Enter a Number:
123456
Sum of Even Digits: 12
======================================================================
*3.Write a java program to print (a) Area of circle (b)Surface area =(4*pi*r) (c)
volume of circle =((4/2)*pi*r).*
package pkgProgamList;
import java.util.Scanner;
public class AreaOfCircle
{
public static void main(String[] args)
{
int radius;
Scanner input=new Scanner(System.in);
System.out.print("Enter radius of a Circle: ");
radius = input.nextInt();
//Calculate Area of Circle by formula
double Area=Math.PI * radius * radius;
double Surface_Area= 4*Math.PI *radius;
double Volumn_Of_Circle= (4/2)*Math.PI*radius;
System.out.println("\nArea of Circle of given radius("+radius+") is: "+Area);
System.out.println("Surface Area of a Circle is: "+Surface_Area);
System.out.println("Voulumn of a Circle is: "+Volumn_Of_Circle);
input.close();
}
}
*Output:*
Enter radius of a Circle: 12
Area of Circle of given radius(12) is: 452.3893421169302
Surface Area of a Circle is: 150.79644737231007
Voulumn of a Circle is: 75.39822368615503
======================================================================
*4.Write a Java program to print Area of Rectangle*
package pkgProgamList;
import java.util.Scanner;
public class AreaOfRectangle
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Length of Rectangle:");
double l = input.nextDouble();
System.out.print("Enter the Breadth of Rectangle:");
double b = input.nextDouble();
double AreaOfRectangle= l * b;
System.out.println("Area of Rectangle of Length("+l+")"+" and Breadth("+b+")"+" is:
"+AreaOfRectangle);
input.close();
}
}
*Output:*
Enter the Length of Rectangle:20
Enter the Breadth of Rectangle:10
Area of Rectangle of Length(20.0) and Breadth(10.0) is: 200.0
======================================================================
*5.Write a Java program to print below Pattern.*
A
BA
CBA
DCBA
package pkgProgamList;
public class Pattern1_ABCD
{
public static void main(String[] args)
{
int n =6;
for(int i=1;i<n;i++)
{
for(int k=i;k>=1;k--)
{
System.out.print((char)(k+64));
}
System.out.println();
}
}
}
*Output:*
A
BA
CBA
DCBA
EDCBA
======================================================================
*6.1. Write a Program to print Digits into Words.*
package pkgProgamList;
public class NumberToWordAdvanced
{
private static final String[] specialNames = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
private static final String[] tensNames = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
private static final String[] numNames = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
private String convertLessThanOneThousand(int number)
{
String current;
if (number % 100 < 20){
current = numNames[number % 100];
number /= 100;
}
else {
current = numNames[number % 10];
number /= 10;
current = tensNames[number % 10] + current;
number /= 10;
}
if (number == 0)
return current;
return numNames[number] + " hundred" + current;
}
public String convert(int number) {
if (number == 0) { return "zero"; }
String prefix = "";
if (number < 0) {
number = -number;
prefix = "negative";
}
String current = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand(n);
current = s + specialNames[place] + current;
}
place++;
number /= 1000;
} while (number > 0);
return (prefix + current).trim();
}
public static void main(String[] args)
{
NumberToWordAdvanced obj = new NumberToWordAdvanced();
System.out.println("*** " + obj.convert(1234567899));
System.out.println("*** " + obj.convert(-55));
}
}
*Output:*
*** one billion two hundred thirty four million five hundred sixty seven thousand
eight hundred ninety nine
*** negative fifty five
======================================================================
*6.2.Write a Program to print Digits into Words.*
package pkgProgamList;
import java.util.Scanner;
public class NumberToWord
{
public void pw(int n,String ch)
{
String one[]={" "," one"," two"," three"," four"," five"," six"," seven","
eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen","
sixteen"," seventeen"," eighteen"," nineteen"};
String ten[]={" "," "," twenty"," thirty"," forty"," fifty","
sixty","seventy"," eighty"," ninety"};
if(n > 19)
{
System.out.print(ten[n/10]+" "+one[n%10]);
}
else
{
System.out.print(one[n]);
}
if(n > 0)System.out.print(ch);
public static void main(String[] args)
{
int n=0;
Scanner scanf = new Scanner(System.in);
System.out.println("Enter an integer number: ");
n = scanf.nextInt();
if(n <= 0)
{
System.out.println("Enter numbers greater than 0");
}
else
{
NumberToWord a = new NumberToWord();
a.pw((n/1000000000)," Hundred");
a.pw((n/10000000)%100," crore");
a.pw(((n/100000)%100)," lakh");
a.pw(((n/1000)%100)," thousand");
a.pw(((n/100)%10)," hundred");
a.pw((n%100)," ");
}
scanf.close();
}
*Output:*
Enter an integer number:
987654321
ninety eight croreseventy six lakh fifty four thousand three hundred twenty one
======================================================================
*7.Write a method in java i.e. Two argument and written in power of it.Suppose X
and Y passing as an argument X is written X^Y.*
package pkgProgamList;
import java.util.Scanner;
public class XToPowerY
{
public int Multi(int x, int y)
{
return (int) Math.pow(x, y);
}
public static void main(String[] args)
{
int count=0;
Scanner input = new Scanner(System.in);
for(count=0;count<3;count++)
{
System.out.print("\nEnter value for x: ");
int a =input.nextInt();
System.out.print("Enter value for y: ");
int b =input.nextInt();
XToPowerY obj=new XToPowerY();
int pow =obj.Multi(a,b);
System.out.println(a+" to the power "+b+" is: "+pow);
}
input.close();
}
*Output:*
Enter value for x: 2
Enter value for y: 3
2 to the power 3 is: 8
Enter value for x: 5
Enter value for y: 2
5 to the power 2 is: 25
Enter value for x: 4
Enter value for y: 3
4 to the power 3 is: 64
======================================================================
*8.Write a java program for multiplying two matrices and print the product for the
same.*
package pkgProgamList;
public class MatrixMultiplication
{
public static void main(String[] args)
{
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print("\t"+c[i][j]+""); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}
}
*Output:*
6 6 6
12 12 12
18 18 18
=====================================================================
*9.Write a java program to create a class account the attributes like account
number, name and balance intilize the value using parameterized.*
package pkgProgamList;
class account
{
String name,address,type;
int accno,bal;
account(String n,int no,int b)
{
name=n; accno=no; bal=b;
}
account(String n,int no,String addr,String t,int b)
{
name=n; accno=no;
address=addr;
type=t; bal=b;
}
void deposite(int a)
{
bal+=a;
}
void withdraw(int a)
{
bal-=a;
}
int getbalance()
{
return(bal);
}
void show()
{
System.out.println("________________________");
System.out.println(" ACCOUNT DETAILS");
System.out.println("------------------------");
System.out.println("Name : "+name);
System.out.println("Account No : "+accno);
System.out.println("Address : "+address);
System.out.println("Type : "+type);
System.out.println("Balance : "+bal);
System.out.println("------------------------");
}
}
public class AccountParameterized
{
public static void main(String[] args)
{
account a1=new account("Paresh",1122945,50000);
account a2=new account("Sachin",1122845,"Mumbai","Current
Account",90000);
a1.address="Pune";
a1.type="Fixed Deposite";
a1.deposite(20000);
a2.withdraw(80000);
a2.deposite(a2.getbalance());
a1.show();
a2.show();
}
}
*Output:*
________________________
ACCOUNT DETAILS
------------------------
Name : Paresh
Account No : 1122945
Address : Pune
Type : Fixed Deposite
Balance : 70000
------------------------
________________________
ACCOUNT DETAILS
------------------------
Name : Sachin
Account No : 1122845
Address : Mumbai
Type : Current Account
Balance : 20000
------------------------
=====================================================================