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

Algorithms, Flowchart Programs

Uploaded by

igatharv9
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)
46 views8 pages

Algorithms, Flowchart Programs

Uploaded by

igatharv9
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

Algorithms, Flowcharts, and Java programs.

Prime Number, Factorial Number, Fibonacci Series, Largest of three numbers,


Palindrome number, Sum of digits, Armstrong Number

1. Check whether a number is prime or not


Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag ← 1
i←2
Step 4: Read n from the user.
Step 5: Repeat the steps until i = (n/2)
5.1 If remainder of n÷i equals 0
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
Display n is not prime
else
Display n is prime
Step 7: Stop

2. Find the factorial of a number


Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop

3. Find the Fibonacci series till the term less than 1000
Step 1: Start
Step 2: Declare variables first_term, second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop

4. Find the largest number among three numbers


Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

5. Algorithm to check if a given number is Palindrome or not


Step 1: Start
Step 2: Read the input number from the user
Step 3: Declare and initialize the variable reverse and assign input to a temp variable
tempNum=num
Step 4: Start the while loop until num !=0 becomes false
rem = num % 10
reverse = reverse * 10 + rem
num = num / 10
Step 5: Check if reverse == tempNum
Step 6: If it’s true then the number is a palindrome
Step 7: If not, the number is NOT a palindrome
Step 8: Stop

6. Algorithm for finding Sum of Digits of a given number.


Step 1: Input the number.
Step 2: Declare a variable called sum and initialize it to 0.
Step 3: while (number > 0)
Step 3.1: Get the rightmost digit of the number using % operator by dividing it with 10 and add it
to sum
Step 3.2: Divide the number by 10.
Step 4. Return sum

7. Algorithm to find whether a given number is Armstrong or not.


Step 1: Start
Step 2: Declare Variable sum, temp, num
Step 3: Read num from User
Step 4: Initialize Variable sum=0 and temp=num
Step 5: Repeat Until num>=0
5.1 sum=sum + cube of last digit i.e [(num%10)*(num%10)*(num%10)]
5.2 num=num/10
Step 6: IF sum==temp
Print "Armstrong Number"
ELSE
Print "Not Armstrong Number"
Step 7: Stop
Flowcharts
1. Flowchart to check whether the number is Prime Number or not.

2. Flowchart to find the factorial of a number.


3. Flowchart to print the Fibonacci Series

4. Flowchart to find the Largest of three numbers


5. Flowchart to check whether the given number is Palindrome number or not.

6. Flowchart to find the Sum of digits in a number


7. Flowchart to check whether the given number is Armstrong Number or not.

Java Programs

1. Java program to check whether the number is Prime or not.


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

int num = 29;


boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
2. Java program to find the Factorial of a number.

public class Factorial {


public static void main(String[] args) {

int num = 10;


long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial = factorial * i;
}
System.out.printf("Factorial of %d = %d", num, factorial);
}
}

3. Java program to display the Fibonacci Series for N terms.

public class Main {


public static void main (String[] args) {

int n = 10, firstTerm = 0, secondTerm = 1;


System.out.println ("Fibonacci Series till" + n + "terms :");

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


System.out.print(firstTerm + ", ");

// compute the next term


int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
4. Java Program to find the Largest of three numbers

public class Largest {


public static void main(String[] args) {

double n1 = -4.5, n2 = 3.9, n3 = 2.5;

if( n1 >= n2 && n1 >= n3)


System.out.println(n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
}
}
5. Java program to check whether the number is Palindrome number or not
class Main {
public static void main(String[] args) {
int num = 3553, reversedNum = 0, remainder;
// store the number to originalNum
int originalNum = num;

// get the reverse of originalNum store it in variable


while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
// check if reversedNum and originalNum are equal
if (originalNum == reversedNum) {
System.out.println(originalNum + " is Palindrome.");
}
else {
System.out.println(originalNum + " is not Palindrome.");
}
}
}
6. Write a Java Program to Compute the Sum of Digits in a given Integer.
public class Digit_Sum
{
public static void main(String args[])
{
int m, sum = 0;
m = 234;

while(m > 0)
{
n = m % 10;
sum = sum + n;
m = m / 10;
}
System.out.println("Sum of Digits:"+sum);
}
}
7. Java program to check whether the number is Armstrong Number or not.
public class Armstrong {
public static void main(String[] args) {

int number = 371, originalNumber, remainder, result = 0;


originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}

You might also like