this is to be solved using for loops in java
Answers
Question 1:-
Write a program to print the sum of the series.
S=x^2 - x^4 + x^6 - x^8...N terms.
Program:-
class Program1
{
public static void main(String args[])
{
int n=10, x=5, a=2;
double s=0.0;
for(int i=1;i<=n;i++)
{
if(i%2==1)
s+=Math.pow(x, a);
else
s-=Math.pow(x, a);
a=a+2;
}
System.out.println("Sum of the series: "+s);
}
}
Question 2:-
Write a program to find the largest of the 10 numbers and also the sum of the numbers.
Program:-
Since, array declaration not allowed, I am declaring 10 variables.
class Program2
{
public static void main(String args[])
{
int a=1, b=2, c=3, d=4, e=5, f=6, a1=7, a2=8, a3=9, a4=10;
int max=Math.max(a, Math.max(b, Math.max(c, Math.max(d, Math.max(e, Math.max(f, Math.max(a1, Math.max(a2, Math.max(a3, a4)))))));
int s=a+b+c+d+e+f+a1+a2+a3+a4;
System.out.println("Largest Number is: "+max);
System.out.println("Sum of all the numbers is: "+s);
}
}
Question 3:-
Write a program to calculate the sum and product of its factors.
Program:-
class Program3
{
public static void main(String args[])
{
System.out.print("Enter a number: ");
int n=6;
int s=0,p=1;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
s+=i;
p*=i;
}
}
System.out.println("Sum of factors: "+s);
System.out.println("Product of factors is: "+p);
}
}
Answer: