While Loop:-
=> import java.util.*;
=> import java.util.*;
class one
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
int n,a,b=1;
double c,s=0.0;
System.out.print("Enter a number for n terms:");
n=sc.nextInt();
System.out.print("Enter a number for a:");
a=sc.nextInt();
while(i<=n)
{
c=Math.pow(a,b);
s=s+c;
b+=3;
i++;
}
System.out.println("Sum="+s);
}
}
do-while Loop:-
import java.util.*;
class one
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
int n,a,b=1;
double c,s=0.0;
System.out.print("Enter a number for n terms:");
n=sc.nextInt();
System.out.print("Enter a number for a:");
a=sc.nextInt();
do
{
c=Math.pow(a,b);
s=s+c;
b+=3;
i++;
}
while(i<=n);
System.out.println("Sum="+s);
}
}
For Loop:-
import java.util.*;
class one
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,a,b=1;
double c,s=0.0;
System.out.print("Enter a number for n terms:");
n=sc.nextInt();
System.out.print("Enter a number for a:");
a=sc.nextInt();
for(i=1;i<=n;i++)
{
c=Math.pow(a,b);
s=s+c;
b+=3;
}
System.out.println("Sum="+s);
}
}
Q 2. Print the sum and product of any 5 numbers input by the user.
While Loop:-
=> import java.util.*;
class two
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
int n,s=0,p=1;
while(i<=5)
{
System.out.print("Enter the number "+i+":");
n=sc.nextInt();
s=s+n;
p=p*n;
i++;
}
System.out.println("Sum="+s);
System.out.println("Product="+p);
}
}
do-while Loop:-
import java.util.*;
class two
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
int n,s=0,p=1;
do
{
System.out.print("Enter the number "+i+":");
n=sc.nextInt();
s=s+n;
p=p*n;
i++;
}
while(i<=5);
System.out.println("Sum="+s);
System.out.println("Product="+p);
}
}
For Loop:-
import java.util.*;
class two
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s=0,p=1;
for(i=1;i<=5;i++)
{
System.out.print("Enter the number "+i+":");
n=sc.nextInt();
s=s+n;
p=p*n;
}
System.out.println("Sum="+s);
System.out.println("Product="+p);
}
}
While Loop:-
=> import java.util.*;
class three
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
System.out.print("Enter the number:");
int n=sc.nextInt();
int s=0;
System.out.println("Factors of "+n+" =>");
while(i<=n)
{
if(n%i==0)
{
System.out.println(i);
s=s+i;
}
i++;
}
System.out.print("Sum of factors of "+n+" = "+s);
}
}
do-while Loop:-
import java.util.*;
class three
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
System.out.print("Enter the number:");
int n=sc.nextInt();
int s=0;
System.out.println("Factors of "+n+" =>");
do
{
if(n%i==0)
{
System.out.println(i);
s=s+i;
}
i++;
}
while(i<=n);
System.out.print("Sum of factors of "+n+" = "+s);
}
}
For Loop:-
import java.util.*;
class three
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number:");
int n=sc.nextInt();
int i,s=0;
System.out.println("Factors of "+n+" =>");
for (i=1;i<=n;i++)
{
if(n%i==0)
{
System.out.println(i);
s=s+i;
}
}
System.out.print("Sum of factors of "+n+" = "+s);
}
}
While Loop:-
=> import java.util.*;
class four
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
int n,f=1;
System.out.print("Enter a number:");
n=sc.nextInt();
while(i<=n)
{
f=f*i;
i++;
}
System.out.print("Factorial of "+n+" = "+f);
}
}
do-while Loop:-
import java.util.*;
class four
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i=1;
int n,f=1;
System.out.print("Enter a number:");
n=sc.nextInt();
do
{
f=f*i;
i++;
}
while(i<=n);
System.out.print("Factorial of "+n+" = "+f);
}
}
For Loop:-
import java.util.*;
class four
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,f=1;
System.out.print("Enter a number:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.print("Factorial of "+n+" = "+f);
}
}
While Loop:-
=> import java.util.*;
class five
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int i=1;
int n,a,b,c,s=0;
System.out.print("Enter number for n terms:");
n=sc.nextInt();
a=0;
b=1;
s=a+b;
while (i<=n-2)
{
c=a+b;
s=s+c;
a=b;
b=c;
i++;
}
System.out.print("Sum="+s);
}
}
do-while Loop:-
import java.util.*;
class five
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int i=1;
int n,a,b,c,s=0;
System.out.print("Enter number for n terms:");
n=sc.nextInt();
a=0;
b=1;
s=a+b;
do
{
c=a+b;
s=s+c;
a=b;
b=c;
i++;
}
while (i<=n-2);
System.out.print("Sum="+s);
}
}
For Loop:-
import java.util.*;
class five
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int i,n,a,b,c,s=0;
System.out.print("Enter number for n terms:");
n=sc.nextInt();
a=0;
b=1;
s=a+b;
for(i=1;i<=n-2;i++)
{
c=a+b;
s=s+c;
a=b;
b=c;
}
System.out.print("Sum="+s);
}
}
Post a Comment