Follow on Facebook

Like and Share on Facebook

Wednesday, February 11, 2015

Write a Program to Calculate Sum and Factorial of N natural Numbers Using Buffered Reader

import java.io.*;

class nsumfact
{
public static void main (String args[]) throws IOException
{
int fact, sum, i, n;

fact=1;
sum=0;


InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br =new BufferedReader(ir);

System.out.println("Please Enter the value of N");

n=Integer.parseInt(br.readLine());

System.out.println("N Natural Numbers are =");

for(i=1;i<=n;i++)
{
System.out.println("\n Natural Numbers="+i);

sum=sum+i;

fact=fact*i;

}
System.out.println("\n Factorial of Last Number is ="+fact);
System.out.println("\n The Sum of the N Numbers is="+sum);




}
}

No comments:

Post a Comment