Core Java Programs
Core Java Programs
package pkgProgamList;
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String[] args)
{
int n;
n=input.nextInt();
int sum = 0;
while(n!=0)
{
//gives you last digit of the input number
int lastDigit= (n % 10);
sum=sum+lastDigit;
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;
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;
radius = input.nextInt();
*Output:*
======================================================================
*4.Write a Java program to print Area of Rectangle*
package pkgProgamList;
import java.util.Scanner;
double AreaOfRectangle= l * b;
input.close();
}
}
*Output:*
======================================================================
*5.Write a Java program to print below Pattern.*
A
BA
CBA
DCBA
package pkgProgamList;
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;
if (number < 0) {
number = -number;
prefix = "negative";
}
do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand(n);
current = s + specialNames[place] + current;
}
place++;
number /= 1000;
} while (number > 0);
}
}
*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;
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:*
======================================================================
*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;
for(count=0;count<3;count++)
{
System.out.print("\nEnter value for x: ");
int a =input.nextInt();
*Output:*
======================================================================
*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}};
*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;
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("------------------------");
}
}
*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
------------------------
=====================================================================