FAQs Help and Tutorials

Program to print sum of series 1-(x/1! +x^2 /2!+……+x^n /n!)( JAVA)

Posted on Updated on

Import java.io.*;
import java.lang.Math;
class series2
{ public static void main(String ar[])
{
int i;
double x=0,n=0,sum=0;
DataInputStream obj= new DataInputStream(System.in);
try
{ System.out.println(“Enter the value of x:”);
x=Integer.parseInt(obj.readLine());
System.out.println(“Enter the value of n:”);
n=Integer.parseInt(obj.readLine());
}
catch(IOException e)
{}
for(i=1;i<=n;i++)
{
sum=sum+Math.pow(x,i)/fact(i);
}
System.out.println(“Answer is:”+(1-sum));Zotechno freaks!
}
static int fact(int no)
{ int f=1;
for(int j=no;j>1;j–)
f=f*j;
return f;
}
}

Program to check wether a number is Armstrong or not (JAVA)

Posted on

import java.io.*;
class arm
{ public static void main(String ar[])
{
int n=0,sum=0;
DataInputStream obj= new DataInputStream(System.in);
System.out.println(“Enter any integer:”);
try
{ n=Integer.parseInt(obj.readLine());
}
catch(IOException e)
{}
int q,r;
q=n;
while(q>0)
{ r=q%10;
sum=sum+r*r*r;
q=q/10;
}
if(n==sum)
System.out.println(“No.is armstrong”);
else
System.out.println(“No.is not armstrong”);
}
}