LAB ASSIGNMENT 04

mail@pastecode.io avatar
unknown
java
2 months ago
4.0 kB
55
Indexable
Never
//eval one

import java.util.Scanner;

public class HomeworkOne {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Input number of terms: ");
        int input_num = sc.nextInt();
        int num = 1;
        int sum = 0;

        for (int i = 1; i <= input_num; i++) {
            sum += num;
            System.out.println(num);
            num += 2;


        }
        System.out.println("The Sum of odd Natural Numbers upto " + input_num + " terms is: " + sum);
    }
}


//eval two

import java.util.Scanner;
public class HomeworkTwo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int sum = 0;

        for (int i = 1; i <= 10; i++) {
            System.out.print("Enter Number: ");
            int num = sc.nextInt();
            sum += num;
            System.out.println("Sum = " + sum);
        }
    }
}


//eval three

import java.util.Scanner;
public class HomeworkThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();

        for (int i = 0; i <= number;  i++) {
            if (number > 0) {
                if (i % 5 == 0 && i % 3 != 0) {
                    System.out.println(i);
                }
            }
        }
    }
}


//eval four

import java.util.Scanner;

public class HomeworkFour {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        boolean is_zero = false;
        int digits = 0;

        while (is_zero == false) {
            int new_num = number / 10;
            number = new_num;
            digits++;
            if (new_num == 0) {
                is_zero = true;
            }
        }

        System.out.println("Total digits = " + digits);


    }
}


// eval five

import java.util.Scanner;

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

        //Part 1
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int number2 = number;
        boolean is_zero = false;
        boolean end_of_prog = false;
        int digits = 0;

        while (is_zero == false) {
            int new_num = number / 10;
            number = new_num;
            digits++;
            if (new_num == 0) {
                is_zero = true;
            }
        }

        //Part 2

        double num2 = Math.pow(10, digits - 1);
        int num2int = (int) num2;

        //Part 3

        while (end_of_prog == false) {
            int x = number2 / num2int;
            System.out.print(x + ",");
            int y = number2 % num2int;
            number2 = y;
            int z = num2int / 10;
            num2int = z;

            if (y == 0) {
                end_of_prog = true;
            }


        }


    }
}


// eval six

import java.util.Scanner;

public class HomeworkSix {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int sum = 0;
        int count = 0;

        for (int i = 1; i < number; i++) {
            if (number % i == 0) {
                sum += i;
            }
        }

        //to check prime number
        if (number == 0 || number == 1) {
            count++;

        } else if (number > 1) {
            for (int i = 2; i < number; i++) {
                if (number % i == 0) {
                    count++;
                }
            }
        }

        if (count == 0) {
            System.out.println(number + " is a prime number");
        } else {
            System.out.println(number + " is not a prime number");
        }
        //to check perfect number
        if (sum == number) {
            System.out.println(number + " is a perfect number");
        } else {
            System.out.println(number + " is not a perfect number");
        }
    }

}
Leave a Comment