JAVA-------SEM-1 Assignment 5
Q_01
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
for(int i = 0 ; i <= n ; i++) {
System.out.println(i +"\t\t\t" + (int)Math.pow(2, i));
}
Q_02
Scanner in = new Scanner(System.in);
System.out.print("Enter the no.of students: ");
int n = in.nextInt();
int count = 0;
for(int x = 0 ; x < n ; x++) {
System.out.print("Enter the mark of the student: ");
int marks = in.nextInt();
if(marks >= 40)
count++;
}
System.out.println(count + " students passed the exams.");
Q_03
int count = 0;
for(int n = 101 ; n <= 1000 ; n++) {
if(n%5==0&&n%6==0) {
System.out.print(n + " ");
count++;
if(count%10==0)
System.out.println();
}
}
Q_04
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0: ");
int num;
int p = 0 , n = 0 , count = 0;
double sum = 0;
do {
num = in.nextInt();
if(num>0)
p++;
else if(num<0)
n++;
sum += num;
count++;
}while(num!=0);
if(count == 1)
System.out.println("No numbers are entered except 0");
else {
System.out.println("The number of positives is " + p);
System.out.println("The number of negatives is " + n);
System.out.println("The total is " + sum);
System.out.println("The average is " + sum/(p+n));
}
Q_05
Scanner in = new Scanner(System.in);
System.out.print("Average of how many numbers you wanna calculate: ");
int n = in.nextInt();
double sum = 0;
for(int i = 0 ; i < n ; i++) {
System.out.println("Enter the number: ");
int num = in.nextInt();
sum += num;
}
System.out.println("The sum is " + sum);
System.out.println("The average is " + sum/n);
Q_06
Scanner in = new Scanner(System.in);
System.out.print("Enter the no.of terms: ");
int n = in.nextInt();
double sum = 0 , h;
for(int i = 1 ; i <= n ; i++) {
System.out.print("Enter the value of a" + n +": ");
double num = in.nextInt();
sum += (1/num);
}
h = n/sum;
System.out.println("The value of h is " + h);
Q_07
Scanner in = new Scanner(System.in);
System.out.print("Enter the no.of terms: ");
int n = in.nextInt();
int i = 1 , sum = 0;
for(int j = 1 ; j <= (2*n - 1) ; j = j + 2) {
sum += i*j;
i = -i;
}
System.out.println("The required sum is: " + sum);
Q_08
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = in.nextInt();
int fac = 1;
for(int i = n ; i > 1 ; i--) {
fac *= i;
}
System.out.println("The factorial is " + fac);
Q_09
Scanner in = new Scanner(System.in);
System.out.print("Enter x: ");
double x = in.nextDouble();
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 1;
for(double i = n ; i > 1 ; i--){
sum *= x/i;
}
System.out.println("The sum is: " + sum);
Q_10
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of x in radian for evaluating sin(x): ");
double x = in.nextDouble();
double sum = 0 , term = x , error = 0.000001;
for(int i = 3 ; Math.abs(term) >= error ; i = i+2) {
sum += term;
term *= -(x*x)/(i*(i-1));
}
System.out.println("Sin(" + x + ") = " + sum);
Q_11
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of x for evaluating cos(x): ");
double x = in.nextDouble();
double sum = 0 , term = 1 , error = 0.000001;
for(int i = 2 ; Math.abs(term) >= error ; i = i + 2) {
sum += term;
term *= -(x*x)/(i*(i-1));
}
System.out.println("Cos(" + x + ") = " + sum);
Q_12
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of x for e^x: ");
double x = in.nextDouble();
double term = 1 , sum = 0;
for(int n = 1 ; n < 100 ; n++) {
sum += term;
term *= x/n;
}
System.out.println("e^x = " + sum);
Q_13
int a = 0 , b = 1 , term;
Scanner in = new Scanner(System.in);
System.out.print("Enter the term of fibonacci: ");
int n = in.nextInt();
System.out.print(a + " ");
if(n>=2)
System.out.print(b + " ");
if(n>2) {
for(int i = 1 ; i <= n-2 ; i++) {
term = a + b;
System.out.print(term + " ");
a = b;
b = term;
}
}
Q_14
Scanner in = new Scanner(System.in);
int a = 0 , b = 1;
System.out.print("enter the number of terms: ");
int n = in.nextInt();
for(int i = 1 ; i <= n/2 ; i++) {
System.out.print(a + " " + b + " ");
a = a + b;
b = a + b;
}
if(n%2 != 0)
System.out.print(a);
Q_15
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount: ");
double amount = in.nextDouble();
System.out.print("Enter the annual rate of interest: ");
double rate = in.nextDouble();
System.out.print("Enter the no. of months: ");
double time = in.nextDouble();
double total = 0 , amount2 = amount;
rate /= 1200;
for(int i = 0 ; i < time ; i++) {
total = amount*(1 + rate);
amount = amount2 + total;
}
System.out.println("The total amount after " + time + " months is: " + total);
Q_16
Scanner in = new Scanner(System.in);
System.out.print("Enter any positive integer: ");
int n = in.nextInt();
int dig , sum = 0;
if(n > 0) {
while(n!=0) {
dig = n%10;
sum = sum*10 + dig;
n /= 10;
}
System.out.println("The reversed number is " + sum);
}
else
System.out.println("This is not a positive integer.");
Q_17
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
double n = Double.parseDouble(args[0]);
double x1 = n/2 , x2;
if(n>0) {
do {
x2 = x1;
x1 = (x2 + n/x2)/2;
}while(Math.abs(x1 - x2) > 0.000001);
System.out.println("The square root of " + n + " is " + x1);
}
else if(n == 0)
System.out.println("Root of " + n + " is 0");
else
System.out.println("Root of negative numbers don't exist.");
Q_18
System.out.print("Enter the number: ");
double n = Double.parseDouble(args[0]);
System.out.println(n);
System.out.print("For calculating the nth root of " + n + " , enter the value of n: ");
double k = Double.parseDouble(args[1]);
System.out.println(k);
double x2 , x1 = n/2;
if(n>0||k%2!=0) {
do {
x2 = x1;
x1 = (((k-1)*x2) + n/Math.pow(x2, k-1))/k;
}while(Math.abs(x1 - x2) > 0.000001);
System.out.println("The " + k + " root of " + n + " is: " + x2);
}
else if(n==0)
System.out.println("The " + k + " root of 0 is: 0");
else
System.out.println("No real roots.");
Q_19
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = in.nextInt();
int dig;
String s = "";
while(n!= 0) {
dig = n%2;
s = dig + s;
n /= 2;
}
System.out.println(s);
Q_20
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = in.nextInt();
int fac = 2;
while(n/fac >= 1) {
if(n%fac == 0) {
System.out.print(fac + " , ");
n /= fac;
}
else
fac++;
}
Q_21
Scanner in = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int n = in.nextInt();
System.out.print("Enter the second integer: ");
int m = in.nextInt();
System.out.print("The gcd of " + n + " and " + m + " is ");
int a = n , b = m;
while(b>0) {
if(b%a==0)
break;
int r = a%b;
a = b;
b = r;
}
System.out.println(a);
Q_22
Scanner in = new Scanner(System.in);
System.out.print("Enter the integer: ");
int n = in.nextInt();
int count = 0;
for(int i = 1 ; i <= n ; i++)
if(n%i==0)
count++;
if(count == 2)
System.out.println("This is a prime no.");
else
System.out.println("This is not a prime no.");
Q_23
System.out.print("Enter any positive integer: ");
int m = Integer.parseInt(args[0]);
System.out.println(m);
int prime = 0;
for(int n = 2 ; n <= m ; n++) {
int count = 0;
for(int i = 1 ; i <= n ; i++)
if(n%i==0)
count++;
if(count == 2)
prime++;
}
System.out.println("There are " + prime + " prime numbers upto " + m);
Q_24(WAY-1)
System.out.print("Enter any positive integer: ");
int n = Integer.parseInt(args[0]);
System.out.println(n);
int x = (int)(Math.pow(n, 0.34));
for(int a = 1 ; a <= x ; a++) {
for(int b = 1 ; b <= x; b++) {
if(b == a)
continue;
for(int c = 1 ; c <= x ; c++) {
if(c==b||c==a)
continue;
for(int d = 1 ; d <= x ; d++) {
if(d==c||d==b||d==a)
continue;
double A = Math.pow(a , 3) , B = Math.pow(b , 3) , C = Math.pow(c , 3) , D = Math.pow(d , 3);
if((A+B <= n)&&(A + B == C + D)) {
System.out.println(a + "^3 + " + b + "^3 " + " = " + (A+B) + " = " + c + "^3 + " + d + "^3");
}
}
}
}
}
Q_24(WAY-2)
Scanner in = new Scanner(System.in);
System.out.print("Enter the integer: ");
int n = in.nextInt();
for(int a = 1 ; a <= n ; a++) {
int a3 = a*a*a;
if(a3 > n)
break;
for(int b = a ; b <= n ; b++) {
int b3 = b*b*b;
if(a3+b3>n)
break;
for(int c = a+1 ; c <= n ; c++) {
int c3 = c*c*c;
if(c3 >= a3+b3)
break;
for(int d = c ; d <= n ; d++) {
int d3 = d*d*d;
if(c3 + d3 > a3 + b3)
break;
if(a3 + b3 == c3 + d3)
System.out.println((a3+b3) + " = " + a + "^3 + " + b + "^3 = " + c + "^3 + " + d + "^3");
}
}
}
}
----------------------HOMEWORK------------------------
Q_01
Scanner in = new Scanner(System.in);
System.out.print("How many integers you wanna enter?: ");
int n = in.nextInt();
int a = 0 , b = 0;
for(int i = 0 ; i < n ; i++) {
System.out.print("Enter the integer: ");
int val = in.nextInt();
if(val < 0)
a++;
else
b++;
}
System.out.println("You entered " + a + " negative integers.");
System.out.println("You entered " + b + " non-negative integers.");
Q_02
Scanner in = new Scanner(System.in);
System.out.println("We are going to compute the sum of the square of the n natural numbers.");
System.out.print("Enter the value of n: ");
int n = in.nextInt();
int n2 , sum = 0;
for(int i = 1 ; i <= n ; i++) {
n2 = i*i;
sum += n2;
}
System.out.println("The sum of square of first " + n + " natural number is " + sum);
Q_03
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
double m;
System.out.print("The series is: ");
for(int i = 0 ; i < n ; i++) {
System.out.print((int)(Math.pow(2 , i)) + " ");
}
Q_04
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
int m = 1;
for(int i = 0 ; i < n ; i++) {
System.out.print(m + " ");
m *= -1;
}
Q_05
Scanner in = new Scanner(System.in);
System.out.print("For computing 1/n! , enter the value of n: ");
int n = in.nextInt();
double term1 , term2 = 1;
for(int i = n ; i >= 1 ; i--) {
term1 = 1.0/i;
term2 *= term1;
}
System.out.println("1/" + n + "! = " + term2);
Q_06
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int n = in.nextInt();
for(int i = 1 ; n != 1 ; i++) {
if(n%i == 0)
n /= i;
else
break;
}
if(n==1)
System.out.println("Yes it is a factorial number.");
else
System.out.println("No it is not a factorial number.");
Q_07
import java.util.Scanner;
public class Q_07 {
public static int fac(int n) {
int sum = 1;
for(int i = n ; i >= 1 ; i--) {
sum *= i;
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int n = in.nextInt();
int result = 0;
for(int i = 1 ; i <= n/2 ; i++) {
if(n%fac(i)==0)
result = fac(i);
}
System.out.println(result);
}}
Q_08
Scanner in = new Scanner(System.in);
System.out.print("Enter the two integers: ");
int n = in.nextInt();
int m = in.nextInt();
int sum = 0;
System.out.print(n + " X " + m + " = ");
for(int i = 1 ; i <= Math.abs(m) ; i++) {
sum += n;
}
if(m < 0)
sum = -sum;
System.out.print(sum);
Q_09
import java.util.Scanner;
public class Q_09 {
public static int fac(int n) {
int sum = 1;
for(int i = n ; i >= 1 ; i--) {
sum *= i;
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the no.of terms: ");
int n = in.nextInt();
int sum = 1;
for(int i = 1 ; i < n ; i++) {
sum += fac(i);
}
System.out.println("The sum of first " + n + " terms is " + sum);
}
}
Q_10
int a = 1 , b = 3;
Scanner in = new Scanner(System.in);
System.out.print("Enter the no of member: ");
int n = in.nextInt();
int term;
for(int i = 1 ; i <= n ; i++) {
System.out.print(a + " ");
term = a+b;
a = b;
b = term;
}
Q_11
int a = 0 ,b = 1 , c = 1 , d = 2 , e;
Scanner in = new Scanner(System.in);
System.out.print("Enter no.of terms: ");
int n = in.nextInt();
int term = 0;
for(int i = 1 ; i <= n ; i++) {
System.out.print(a + " ");
e = c + d;
a = b;
b = c;
c = d;
d = e;
}
Q_12
Scanner in = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int n1 = in.nextInt();
System.out.print("Enter the second integer: ");
int n2 = in.nextInt();
int dif = 1;
while(dif > 0) {
dif = n2-n1;
n2 = n1;
n1 = dif;
}
if(dif == 0)
System.out.println("Yes! they are consectutive the members of a fabonacci series.");
else
System.out.println("No! they are not the consectutive the members of a fabonacci series.");
Q_13
import java.util.Scanner;
public class Q_13_using_methods {
public static int fac(int n) {
int sum = 1;
for(int i = n ; i >= 1 ; i--) {
sum *= i;
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("enter the term: ");
int n = in.nextInt();
for(int i = 0 ; i < n ; i++) {
System.out.println("f" + (i+3) + " = " + (i+1) + "! + " + i + "! = " + (fac(i)+fac(i+1)));
}
}
}
Q_14
Scanner in = new Scanner(System.in);
System.out.print("Enter the integer: ");
int n = in.nextInt();
int count = 0;
while(n != 0) {
n /= 10;
count++;
}
System.out.println(count);
Q_15
Scanner in = new Scanner(System.in);
System.out.print("Enter the integer: ");
int n = in.nextInt();
int sum = 0;
while(n!=0) {
sum += n%10;
n /= 10;
}
System.out.println(sum);
Q_16
Scanner in = new Scanner(System.in);
System.out.print("Enter the no.of digits: ");
int n = in.nextInt();
int sum = 0;
for(int i = 1 ; i <= n ; i++) {
System.out.print("Enter the number: ");
int dig = in.nextInt();
sum = 10*sum + dig;
}
System.out.println(sum);
Q_17
Scanner in = new Scanner(System.in);
System.out.print("Enter the integer: ");
int n = in.nextInt();
int i;
for(i = 2 ; i <= n/2 ; i++) {
if(n%i == 0)
break;
}
System.out.println("The smallest divisor of " + n + " other than one is: " + i);
Q_18
Scanner in = new Scanner(System.in);
System.out.print("Enter the integer: ");
int n = in.nextInt();
int i;
System.out.print("The divisors of " + n + " are: ");
for(i = 1 ; i <= n/2 ; i++) {
if(n%i == 0)
System.out.print(i + " , ");
}
System.out.print(n);
Q_19
int fac = 0;
for(int i = 1 ; i <= 100 ; i++) {
for(int j = 1 ; j <= i ; j++) {
if(i%j==0)
fac++;
}
}
Q_20
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = in.nextInt();
System.out.print("Enter the value of p: ");
int p = in.nextInt();
int scm = n*p;
for(int i = 1 ; i <= n ; i++) {
if(n%i==0&&p%i==0) {
scm /= i;
}
}
System.out.println("The lcm is: " + scm);
Q_21
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = in.nextInt();
System.out.print("Enter the value of p: ");
int p = in.nextInt();
int divisor = 0;
for(int i = 2 ; i <= p ; i++) {
if(n%i==0&&p%i==0) {
divisor = i;
break;
}
}
if(divisor == n || divisor == p)
System.out.println("No common divisor other than the numbers.");
else
System.out.println("The smallest common divisor is " + divisor);
Q_22
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of x: ");
int x = in.nextInt();
System.out.print("Enter the value of n: ");
int n = in.nextInt();
int product = 1;
for(int i = 1 ; i <= n ; i++) {
product *= x;
}
System.out.println(x + "^" + n + " = " + product);
Q_23
Scanner in = new Scanner(System.in);
int a = 0 , b = 1 , c = 0 , d, next = 0;
System.out.print("Which member of the fabonacci series you want: ");
int n = in.nextInt();
if(n == 1 || n ==2)
System.out.println(n-1);
else {
for(int i = 1 ; i < n/2 ; i++) {
c = a + b;
next = c + b;
a = c;
b = next;
}
if(n%2==0)
System.out.println(next);
else
System.out.println(a+b);
}
Q_24
Scanner in = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int x = in.nextInt();
System.out.print("Enter the second integer: ");
int y = in.nextInt();
for(int i = 1 ; i <= y ; i++) {
if(x%i==0 && y%i==0) {
int count = 0;
for(int j = 1 ; j <= i ; j++) {
if(i%j==0)
count++;
}
if(count == 2)
System.out.println(i);
}
}
Q_25
Scanner in = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int x = in.nextInt();
System.out.print("Enter the second integer: ");
int y = in.nextInt();
int sum1 = 0 , sum2 = 0;
for(int i = 1 ; i <= x/2 ; i++) {
if(x%i==0) {
sum1+= i;
}
}
for(int i = 1 ; i <= y/2 ; i++) {
if(y%i==0) {
sum2+= i;
}
}
if(sum1 == y && sum2 == x)
System.out.println("Yes! they are amicable numbers.");
else
System.out.println("No! they are not amicable numbers.");
Q_26
System.out.print("The perfect numbers between 1 to 500 are: ");
for(int i = 1 ; i <= 500 ; i++) {
int sum = 0;
for(int j = 1 ; j <= i/2 ; j++) {
if(i%j==0)
sum += j;
}
if(sum == i)
System.out.print(i + " ");
}
XXXX------------THE END---------------XXXX
Comments
Post a Comment