freetimestudys@gmail.com          +91-7097633380
  Login Here                        

print the name of the entered natural no(1-10)

import java.io.*;
class natno
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a;
System.out.println("Enter the value");
a=Integer.parseInt(br.readLine());
switch(a)
{ case 1 : System.out.println("One");
break ;
case 2 : System.out.println("Two");
break ;
case 3 : System.out.println("Three");
break ;
case 4 : System.out.println("Four");
break ;
case 5 : System.out.println("Five");
break ;
case 6 : System.out.println("Six");
break ;
case 7 : System.out.println("Seven");
break ;
case 8: System.out.println("Eight");
break ;
case 9 : System.out.println("Nine");
break ;
case 10 : System.out.println("Ten");
break ;
default : System.out.println("Invalid choice");
break ;
}
}
}

intput and output

Enter the value
7
Seven

sum of n natural numbers.

import java.io.*;
class clssum
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of n");
int n,i;
int s=0;
n = Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{
s+=i;
}
System.out.println("Sum is"+s);
}}

intput and output

Enter the value of n
5
Sum is15

factorial of a number

import java.io.*;
class clssum
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of n");
int n,i;
int s=1;
n = Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{
s+=i;
}
System.out.println("Factorial is"+s);
}
}

intput and output

Enter the value of n
5
Factorial is 120