Uses Of Computers
Essay by Manish • November 6, 2011 • 4,392 Words (18 Pages) • 1,131 Views
QUESTION 1.
Write a program to solve the following equation with values of n & c being entered by the user
(n! * (n-c)!) / c!
import java.io.*;
class Calc
{ //class begins
public void main()throws IOException
{ //main() begins
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of n.");
int n=Integer.parseInt(ob.readLine());
System.out.println("Enter the value of c.");
int c=Integer.parseInt(ob.readLine());
int x,y,z;
x=factorial(n);
y=factorial(c);
z=factorial(n-c);
System.out.print("nThe value of (n!*(n-c)!)/c! is : "+((double)(x*z)/y)); /*printing the answer
along with a message*/
} //main() ends
public int factorial(int a)
{ //factorial() begins
int i,f=1;
for(i=1; i<=a; i++)
{
f=f*i;
}
return f;
} //factorial() ends
} //class ends
VARIABLE DESCRIPTION
NAME
TYPE
PURPOSE
n
int
Value entered by user
c
Int
Value entered by user
x
Int
To store factorial of n
y
int
To store factorial of c
z
Int
To store factorial of (n-c)
i
Int
Index variable of the loop to find factorials
f
Int
To store factorial.
OUTPUT
Enter the value of n.
10
Enter the value of c.
19
The value of (n!*(n-c)!)/c! is : 0.03309688807531381
QUESTION 2.
Write a program that will display all the prime palindrome numbers with a suitable message.
class Palindromes
{ // class begins
public void main()
{ // main() begins
int i,j,rem,rev,num; //declaring the variables used
boolean x;
System.out.println("Prime Palindrome numbers from 10 to 1000 are :");
for(i=10; i<=1000; i++)
{ // outer for loop begins
rev=0;
num=i;
while(num!=0) // loop to find reverse of the numbers
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(rev==i)
{ //condition to check if the palindrome is prime or not
x=isPrime(i);
if(x==true)
System.out.print(i+" ");
}
} // outer for loop ends
} // main() ends
public boolean isPrime(int a)
...
...