0% found this document useful (0 votes)
2 views5 pages

While 3

The document contains Java programs demonstrating various algorithms using loops, including checking for automorphic numbers, generating Fibonacci series, summing positive and negative numbers, calculating the sum and average of numbers, and a menu-driven program for digit analysis. Each program includes variable descriptions and user input prompts. The examples utilize while and do-while loops for iterative processing.

Uploaded by

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

While 3

The document contains Java programs demonstrating various algorithms using loops, including checking for automorphic numbers, generating Fibonacci series, summing positive and negative numbers, calculating the sum and average of numbers, and a menu-driven program for digit analysis. Each program includes variable descriptions and user input prompts. The examples utilize while and do-while loops for iterative processing.

Uploaded by

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

🔹 Q9.

Automorphic Number (using while loop)


A number is Automorphic if its square ends with the same number itself.
import [Link].*;
class AutomorphicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n, sq, temp;
[Link]("Enter any number: ");
n = [Link]();
sq = n * n;
temp = n;

while(temp > 0)
{
if(n % 10 != sq % 10)
break;
n = n / 10;
sq = sq / 10;
}

if(temp == 0)
[Link]("Automorphic Number");
else if(n == 0)
[Link]("Automorphic Number");
else
[Link]("Not an Automorphic Number");
}
}

🧾 VDT
VARIAB TYP
DESCRIPTION
LE E

n int Input number

sq int Square of number

Copy of original
temp int
number

🔹 Q10. Fibonacci Series (using while loop)


(Print the Fibonacci series up to n terms.)
import [Link].*;
class FibonacciSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n, a = 0, b = 1, c, i = 1;
[Link]("Enter number of terms: ");
n = [Link]();

[Link](a + " " + b + " ");

while(i <= n - 2)
{
c = a + b;
[Link](c + " ");
a = b;
b = c;
i++;
}
}
}

🧾 VDT
VARIAB TYP
DESCRIPTION
LE E

Number of
n int
terms

a int First term

b int Second term

Next term in
c int
series

i int Loop counter

🔹 Q11. Sum of Positive and Negative Numbers Separately (using


do-while loop)
(Input numbers one by one; stop when user enters 0.)
import [Link].*;
class SumPosNeg
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n, sumP = 0, sumN = 0;

do
{
[Link]("Enter a number (0 to stop): ");
n = [Link]();

if(n > 0)
sumP = sumP + n;
else if(n < 0)
sumN = sumN + n;

}
while(n != 0);

[Link]("Sum of positive numbers = " + sumP);


[Link]("Sum of negative numbers = " + sumN);
}
}
🧾 VDT
VARIAB TYP
DESCRIPTION
LE E

n int Input number

Sum of positive
sumP int
numbers

Sum of negative
sumN int
numbers

🔹 Q12. Sum and Average of Numbers (using do-while loop)


(Keep taking input until user enters 0.)
import [Link].*;
class SumAverage
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n, count = 0, sum = 0;
double avg;

do
{
[Link]("Enter a number (0 to stop): ");
n = [Link]();

if(n != 0)
{
sum = sum + n;
count++;
}
}
while(n != 0);

if(count > 0)
{
avg = (double)sum / count;
[Link]("Sum = " + sum);
[Link]("Average = " + avg);
}
else
[Link]("No numbers entered.");
}
}

🧾 VDT
VARIAB
TYPE DESCRIPTION
LE

n int Input number

sum int Sum of all numbers

Counts numbers
count int
entered
VARIAB
TYPE DESCRIPTION
LE

doubl
avg Average of numbers
e

🔹 Q13. Menu-Driven Program


(a. Total number of digits
b. Sum of all even digits
c. Smallest digit)
import [Link].*;
class MenuDrivenDigits
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int ch, n, d, count = 0, sumEven = 0, small = 9, temp;

[Link]("1. Total number of digits");


[Link]("2. Sum of even digits");
[Link]("3. Smallest digit");
[Link]("Enter your choice: ");
ch = [Link]();

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


n = [Link]();
temp = n;

switch(ch)
{
case 1:
while(n > 0)
{
count++;
n = n / 10;
}
[Link]("Total digits = " + count);
break;

case 2:
while(n > 0)
{
d = n % 10;
if(d % 2 == 0)
sumEven = sumEven + d;
n = n / 10;
}
[Link]("Sum of even digits = " + sumEven);
break;

case 3:
while(n > 0)
{
d = n % 10;
if(d < small)
small = d;
n = n / 10;
}
[Link]("Smallest digit = " + small);
break;

default:
[Link]("Invalid choice");
}
}
}

🧾 VDT
VARIAB TYP
DESCRIPTION
LE E

ch int User’s choice

n int Input number

d int Extracted digit

count int Counts digits

sumEven int Sum of even digits

small int Smallest digit

Stores original
temp int
number

You might also like