Write a program for finding a sum of digits. for example - 123, sum of digits is 6
(Using do while loop)
/*Pay attention. This program will take user defined input from the user.*/
Input: Enter an integer 123
Output: Sum of digits = 6
Program Code -
import java.util.Scanner;
public class Digit_Sum_DoLoop
{
public static void main(String args[])
{
int m, n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
m = s.nextInt();
do
{
n = m % 10;
sum = sum + n;
m = m / 10;
}while(m > 0);
System.out.println("Sum of Digits:"+sum);
}
}
Write a program for finding a product of digits. for example - 123, product of digits is 6
(Using do while loop)
/*Pay attention. This program will take user defined input from the user.*/
Input: Enter an integer 123
Output: Product of digits = 6
Program Code -
import java.util.Scanner;
public class Digit_Product_DoLoop
{
static int getProduct(int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
int n = s.nextInt();
System.out.println(getProduct(n));
}
}
Write a program to input an integer and check whether it is Palindrome or not, using do while loop.
(Using do while loop)
/*Pay attention. This program will take user defined input from the user.*/
Input: Enter an integer 131
Output: 131 is Palindrome number
Program Code -
import java.util.Scanner;
public class Palindrome_DoLoop
{
public static void main(String[] args)
{
int reversedInteger = 0, remainder, originalInteger;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
int num = s.nextInt();
originalInteger = num;
do
{
remainder = num % 10;
reversedInteger = reversedInteger * 10 + remainder;
num /= 10;
}while( num != 0 );
if (originalInteger == reversedInteger)
System.out.println(originalInteger + " is a palindrome.");
else
System.out.println(originalInteger + " is not a palindrome.");
}
}
Write a program to input an integer and check whether it is Spy or not?
import java.util.Scanner; public class SpyNumber { public static void main(String[] args) { int n,product=1,sum=0; int ld; Scanner sc = new Scanner(System.in); System.out.print("Enter the number :" ); n=sc.nextInt(); while(n>0) { ld=n%10; sum=sum+ld; product=product*ld; n=n/10; } if(sum==product) System.out.println("Given number is spy number"); else System.out.println("Given number is not spy number"); } }
Write a program to input an integer and check whether it is Niven or not?
import
java.util.*;
class
Niven_Number
{
public
static
void
main(String args[])
{
Scanner sc =
new
Scanner(System.in);
System.out.print(
"Enter a number : "
);
int
n = sc.nextInt();
int
c = n, d, sum =
0
;
{
d = c%
10
;
sum = sum + d;
c = c/
10
;
}
while
(c>
0
);
if
(n%sum ==
0
)
System.out.println(n+
" is a Niven Number."
);
else
System.out.println(n+
" is not a Niven Number."
);
}
}
Write a program to input an integer and check whether it is Armstrong
or not, using do while loop.
public class Armstrong {
public static void main(String[] args) {
int number = 371, originalNumber, remainder, result = 0;
originalNumber = number;
do
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}while (originalNumber != 0);
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
Write a program to input an integer and check whether it is Pronic or
not?
import java.util.Scanner;
public class Example13Pronic {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
int num = sc.nextInt();
int ans = 0;
for(int i=0; i<num; i++)
{
if(i*(i+1) == num)
{
ans = 1;
break;
}
}
if(ans == 1)
System.out.println("Pronic Number.");
else
System.out.println("Not a Pronic Number.");
}
}
Write a program to input an integer and check whether it is Disarium or
not?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 import
java.util.Scanner;
public
class
JavaDisariumNumberProgram
{
private
static
boolean
isItDisariumNumber(
int
inputNumber)
{
int
noOfDigits = Integer.toString(inputNumber).length();
int
copyOfInputNumber = inputNumber;
int
sum =
0
;
while
(inputNumber >
0
)
{
int
lastDigit = inputNumber %
10
;
sum = sum + (
int
) Math.pow(lastDigit, noOfDigits);
inputNumber = inputNumber /
10
;
noOfDigits--;
}
if
(sum == copyOfInputNumber)
{
return
true
;
}
else
{
return
false
;
}
}
public
static
void
main(String[] args)
{
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter a number :"
);
int
inputNumber = sc.nextInt();
if
(isItDisariumNumber(inputNumber))
{
System.out.println(inputNumber+
" is a Disarium number"
);
}
else
{
System.out.println(inputNumber+
" is not a Disarium number"
);
}
sc.close();
}
}
Write a program to input time in seconds and
display the time broken up as hours, minutes and
seconds.
import java.util.*;
public class Exercise55 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input seconds: ");
int seconds = in.nextInt();
int p1 = seconds % 60;
int p2 = seconds / 60;
int p3 = p2 % 60;
p2 = p2 / 60;
System.out.print( p2 + ":" + p3 + ":" + p1);
System.out.print("\n");
}
}
Write a program to input a floating point number
and round it off to the nearest integer.
class Main_concne{ public static void main(String[] args) { float x = 5.60f; int y = (int)x; System.out.println("y = " + y); }}
Write a program to input the Basic Pay of an
employee and find the gross pay of the employee
for the following allowances and deductions
public class employee
{
public static void main(double salary)
{
double allowance=30/100*salary;
double rent =15/100*salary;
double pf=12.5/100*salary;
double gross=salary+allowance+rent;
double net=salary-pf
System.out.println("gross pay=Rs."+gross);
System.out.println("net pay=Rs."+net)'
}
}
Write a program to accept 3 sides of a triangle,
And check whether it can form a triangle or not.If
it forms a triangle , check whether it is an acute
angled, obtuse angled or right angled triangle.
public class accept3s
{
public static void main(String[] args)
{
Scanner Sc=new Scanner (System.in);
int a,b,c;
a=Sc.nextInt();
b=Sc.nextInt();
c=Sc.nextInt();
if((a+b+c)==180)
{
System.out.println("Triangle possible");
if (a==90)
System.out.println("Right angled");
else if ((a<90)&&(a>0))
System.out.println("Acute angle");
else if (a>90)&&(a<180)
System.out.println("Obtuse angled");
}
else
System.out.println("not possible");
}
}
A library charges a fine for books returned late.
Following are the fines
class retLate
{
public static void main(String args[])
{
double fine=0.0; int n;
System.out.println("Enter the no of days late");
n = 5
if(n<=5)
fine=0.40*n;
else if(n<=10)
fine=0.65*n;
else
fine=0.80*n;
System.out.println("Fine = "+fine);
}
}
Write a menu driven program to input an integer
and a choice of character by the user as M or B. If
choice is M check whether the number is Magic or
not.
No comments:
New comments are not allowed.