0% found this document useful (0 votes)
4 views8 pages

Assignment4 Java Programs

The document contains ten Java programs that demonstrate various mathematical concepts and number classifications, including Niven numbers, Duck numbers, number series, and GCD calculations. Each program is structured with a main method and utilizes loops (while, do-while, for) to perform calculations based on user input. The programs also showcase the use of conditionals to determine specific properties of numbers, such as amicable pairs and Jupiter numbers.

Uploaded by

microdlksystem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Assignment4 Java Programs

The document contains ten Java programs that demonstrate various mathematical concepts and number classifications, including Niven numbers, Duck numbers, number series, and GCD calculations. Each program is structured with a main method and utilizes loops (while, do-while, for) to perform calculations based on user input. The programs also showcase the use of conditionals to determine specific properties of numbers, such as amicable pairs and Jupiter numbers.

Uploaded by

microdlksystem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment - 4 (Combined Java Programs)

Q1. Niven Number (while loop)

--------------------------------

import [Link];

class NivenNumber {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

int n = [Link]();

int temp = n;

int sum = 0;

while(temp > 0) {

int d = temp % 10;

sum += d;

temp /= 10;

if(n % sum == 0)

[Link](n + " is a Niven Number");

else

[Link](n + " is not a Niven Number");

Q2. Duck Number (do-while loop)

--------------------------------

import [Link];

class DuckNumber {
public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

int n = [Link]();

int temp = n;

boolean flag = false;

do {

int d = temp % 10;

if(d == 0) {

flag = true;

break;

temp /= 10;

} while(temp > 0);

if(flag)

[Link](n + " is a Duck Number");

else

[Link](n + " is not a Duck Number");

Q3. Number Series (for loop)

--------------------------------

class NumberSeries {

public static void main(String args[]) {

int sum = 0;

for(int i = 1; i <= 10; i++) {

sum += i;
[Link](sum + " ");

Q4. Sum of Series 1 - 2 + 3 - 4 ... (for loop)

--------------------------------

import [Link];

class SeriesSum1 {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter n: ");

int n = [Link]();

int sum = 0;

for(int i = 1; i <= n; i++) {

if(i % 2 == 0)

sum -= i;

else

sum += i;

[Link]("Sum = " + sum);

Q5. Sum of Fraction Series (for loop)

--------------------------------

import [Link];

class SeriesSum2 {

public static void main(String args[]) {


Scanner sc = new Scanner([Link]);

[Link]("Enter n: ");

int n = [Link]();

double sum = 0.0;

for(int i = 1; i <= n; i++) {

sum += 1.0 / (i * (i + 1));

[Link]("Sum = " + sum);

Q6. Count numbers & sum of digits (while loop)

--------------------------------

import [Link];

class NumberCount {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

int count = 0, evenSum = 0, oddSum = 0;

int n;

[Link]("Enter numbers (0 to stop):");

while(true) {

n = [Link]();

if(n == 0) break;

count++;

int temp = n;
while(temp > 0) {

int d = temp % 10;

if(d % 2 == 0)

evenSum += d;

else

oddSum += d;

temp /= 10;

[Link]("Total numbers entered: " + count);

[Link]("Sum of even digits: " + evenSum);

[Link]("Sum of odd digits: " + oddSum);

Q7. Count Digits (while loop)

--------------------------------

import [Link];

class CountDigits {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

int n = [Link]();

int count = 0;

int temp = n;

while(temp > 0) {

count++;

temp /= 10;
}

[Link](count + " Digits");

Q8. GCD of Two Numbers (Corrected do-while loop)

--------------------------------

import [Link];

class GCDNumber {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

int i = 1, gcd = 1;

int limit = [Link](a, b);

do {

if(a % i == 0 && b % i == 0)

gcd = i;

i++;

} while(i <= limit);

[Link](gcd + " is GCD of " + a + " and " + b);

Q9. Jupiter Number (while loop)


--------------------------------

import [Link];

class JupiterNumber {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

int n = [Link]();

int last = n % 10;

int temp = n;

int first = 0;

while(temp > 0) {

first = temp;

temp /= 10;

int sum = first + last;

if(n % sum == 0)

[Link](n + " is a Jupiter Number");

else

[Link](n + " is not a Jupiter Number");

Q10. Amicable Pair (while loop)

--------------------------------

import [Link];

class AmicablePair {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);


[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

int sum1 = 0, sum2 = 0;

int i = 1;

while(i < a) {

if(a % i == 0)

sum1 += i;

i++;

i = 1;

while(i < b) {

if(b % i == 0)

sum2 += i;

i++;

if(sum1 == b && sum2 == a)

[Link]("They are Amicable Numbers");

else

[Link]("They are not Amicable Numbers");

You might also like