






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
edureka assignment module 1 programming for java and soa
Typology: Exercises
1 / 12
This page cannot be seen from the preview
Don't miss anything!







import java.until.scanner package election Class age{ public static void main(String arcs[]){ Scanner scan=new Scanner (System.in);
System.out.println(“Enter the age of person”);
into user_age=scan.nextInt();
System.out.println(“The age of person is”+user_age);
if(user_age>18)
{
System.out.println(“You are eligible to Vote”);
}
else{
System.out.println(“You are not eligible to vote”);
}
}
}
Write a program to check if the number is positive or negative.
import java.util.Scanner; public class JavaProgram { public static void main(String[] args) { int number; Scanner scan = new Scanner(System.in); System.out.print("Enter the number you want to check:"); number = scan.nextInt(); scan.close(); if(number > 0) { System.out.println(number+" is positive number"); } else if(number < 0) { System.out.println(number+" is negative number"); } } }
Extend the previous program to check whether the given number is positive, zero or negative. (Hint: use if-else conditions)
import java.util.Scanner; public class JavaProgram { public static void main(String[] args)
int a, b, big; Scanner scan = new Scanner(System.in);
System.out.print("Enter Two Number : "); a = scan.nextInt(); b = scan.nextInt();
if(a>b) { big = a; } else { big = b; }
System.out.print("Largest of Two Number is " +big); }
Write a program to check given number is even or odd. import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); int num = reader.nextInt(); if(num % 2 == 0) System.out.println(num + " is even"); else
System.out.println(num + " is odd"); } }
Write a program to print 10 even numbers and 10 odd numbers.
public class OddEvenInArrayExample{ public static void main(String args[]){ int a[]={1,2,3,4,5,6,7,8,9,10}; System.out.println("Odd Numbers:"); for(int i=0;i<a.length;i++){ if(a[i]%2!=0){ System.out.println(a[i]); } } System.out.println("Even Numbers:"); for(int i=0;i<a.length;i++){ if(a[i]%2==0){ System.out.println(a[i]); } } }}
Write a program to find factorial of a number.
public class Factorial { public static void main(String[] args) { int num = 10;
return sum; } public static void main(String[] args) { int n = 687; System.out.println(getSum(n)); } } Write a program to reverse the digits of a number. public class ReverseNumber { public static void main(String[] args) { int num = 1234567, reversed = 0; for(;num != 0; num /= 10) { int digit = num % 10; reversed = reversed * 10 + digit; } System.out.println("Reversed Number: " + reversed); } } Write a program to generate 10 Fibonacci numbers. public class Fibonacci { public static void main(String[] args) { int n = 10, t1 = 0, t2 = 1; System.out.print("First " + n + " terms: "); for (int i = 1; i <= n; ++i) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum;
While Loop: Write a program to print 10 even numbers and 10 odd numbers.
Print Even Numbers:-
import java.util.Scanner; class Even_Odd{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in);
System.out.print("Print all even number until:\n"); int num=scan.nextInt(); System.out.print("even number from i to "+num+" are: \n"); i=1; while(i<=num) { if(i%2==0){ System.out.print(i+"\n"); } i++; } }
while(i<=number) { fact = fact * i; i++; } System.out.println("Factorial of "+number+" is: "+fact); } }
Write a program to generate tables of 10. public class MultiplicationTable { public static void main(String[] args) { int num = 10, i = 1; while(i <= 10) { System.out.printf("%d * %d = %d \n", num, i, num * i); i++; } } }
Write a program to add the digits of a number.
class GFG { static int getSum(int n) {
int sum = 0;
while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } public static void main(String[] args) { int n = 687;
System.out.println(getSum(n)); } }
Write a program to reverse the digits of a number.
public class ReverseNumber { public static void main(String[] args) { int num = 1234, reversed = 0; while(num != 0) { int digit = num % 10; reversed = reversed * 10 + digit; num /= 10; } System.out.println("Reversed Number: " + reversed); }