Download WAP to print addition, subtraction, multiplication and division. import

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Law of large numbers wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Addition wikipedia , lookup

Transcript
1. WAP to print addition, subtraction, multiplication and division.
import java.io.*;
class Pro1
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x: ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
//Accepting first value
System.out.print("Enter value of y: ");
String v2 = in.readLine();
int y = Integer.parseInt(v2);
//Accepting second value
System.out.println("Addition is " + (x+y));
// Displaying Sum of Numbers
System.out.println("Substraction is " + (x-y));
// Displaying Difference of Numbers
System.out.println("Multiplication is " + (x*y));
// Displaying Product of Numbers
System.out.println("Division is " + (x/y));
// Displaying Division of Numbers
}
}
Output:
Enter value of x: 5
Enter value of y: 2
Addition is 7
Substraction is 3
Multiplication is 10
Division is 2
Variable Used :
Variables
x
y
Datatypes
int
int
Description
Stores first value
Stores second value
2. WAP to convert the time entered in minutes to hours and minutes.
import java.io.*;
class Pro2
{
public static void main(String[]args)throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter Total Minutes : ");
String v1=in.readLine();
int minutes=Integer.parseInt(v1); //Accepting time in minutes
int hr=minutes/60;
// Calculating total hours
int min=minutes%60;
// Calculating remaining minutes
System.out.println("Hour = "+hr+ " Minute= "+min);
}
}
Output:
Enter Total Minutes : 125
Hour= 2 Minute= 5
Variable Used :
Variables
minutes
hr
min
Datatypes
int
int
int
Description
Accepts total minutes
Stores calculated hours
Stores calculated minutes
3. WAP to find greatest of two numbers.
import java.io.*;
class Pro3
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x: ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
System.out.print("Enter value of y: ");
String v2 = in.readLine();
int y = Integer.parseInt(v2);
int max;
if(x > y)
// checking whether x is greater than y
max = x;
else
max = y;
System.out. println("Greater number is " + max);
}
}
Output:
Enter value of x: 8
Enter value of y: 5
Greater number is 8
Variable Used :
Variables
x
y
max
Datatypes
int
int
int
Description
Stores first value
Stores second value
Stores maximum value
4. WAP to find greatest of three numbers.
import java.io.*;
class Pro4
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x: ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
System.out.print("Enter value of y: ");
String v2 = in.readLine();
int y = Integer.parseInt(v2);
System.out.print("Enter value of z: ");
String v3 = in.readLine();
int z = Integer.parseInt(v3);
int max;
if(x > y && x > z)
// checking whether x is > than y and z
max = x;
else
if(y > x && y > z)
// checking whether y is > than x and z
max = y;
else
max = z;
System.out. println("Greatest number is " + max);
}
}
Output:
Enter value of x: 10
Enter value of y: 2
Enter value of z: 5
Greatest number is 10
Variable Used :
Variables
x
y
z
max
Datatypes
int
int
int
int
Description
Stores first value
Stores second value
Stores third value
Stores the maximum value
5. WAP to find the smallest of four numbers.
import java.io.*;
class Pro5
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x: ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
System.out.print("Enter value of y: ");
String v2 = in.readLine();
int y = Integer.parseInt(v2);
System.out.print("Enter value of z: ");
String v3 = in.readLine();
int z = Integer.parseInt(v3);
System.out.print("Enter value of w: ");
String v4 = in.readLine();
int w = Integer.parseInt(v4);
int min;
if(x < y && x < z && x < w)
// checking if x is smaller than y,z and w
min = x;
else
if(y < x && y < z && y < w)
//checking if y is smaller than y,z and w
min = y;
else
if(z < x && z < y && z < w) //checking if z is smaller than y,z and w
min = z;
else
min = w;
System.out. println("Smallest number is " + min);
}
}
Output:
Enter value of x: 12
Enter value of y: 5
Enter value of z: 8
Enter value of w: 2
Smallest number is 2
Variable Used :
Variables
Datatypes
Description
x
int
Stores first value
y
int
Stores second value
z
int
Stores third value
w
min
int
int
Stores fourth value
Stores the maximum value
6. WAP to print even or odd for a number.
import java.io.*;
class Pro6
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
if(x % 2 == 0)
// checking divisibility by 2
System.out.println(x + " is Even ");
else
System.out.println(x + " is Odd ");
}
}
Output:
Enter any number : 85
85 is Odd
Variable Used :
Variables
x
Datatypes
int
Description
Stores input value
7. WAP to find that a year is leap year or not.
import java.io.*;
class Pro7
{
public static void main(String [] args)throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any year : ");
String v1 = in.readLine();
int yr = Integer.parseInt(v1);
if(yr % 4 == 0)
//checking divisibility by 4
System.out.println(yr + " is leap year ");
else
System.out.println(yr + " is not a leap year ");
}
}
Output:
Enter any year : 2008
2008 is leap year
Variable Used :
Variables
yr
Datatypes
int
Description
Stores year
8. WAP to find that a number is single digit or not.
import java.io.*;
class Pro8
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
if(x < 10)
// checking for single digit
System.out.println(x + " is single digit ");
else
System.out.println(x + " is not a single digit ");
}
}
Output:
Enter any number : 5
5 is single digit
Variable Used :
Variables
x
Datatypes
int
Description
Stores the value
9. WAP to print that a character is a vowel or a consonant using if else.
import java.io.*;
class Pro10
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter a character: ");
char ch =(char)in.read(); // to take character input through keyboard
if( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' ||ch=='I' ||
ch=='O' || ch=='U')
// checking whether character is vowel
System.out.print("It is a vowel");
else
System.out.println("It is a consonant");
}
}
Output:
Enter a character: u
It is a vowel
Variable Used :
Variables
ch
Datatypes
char
Description
Stores a character
10. WAP to print the numeric value entered by a user in words using switch case.
import java.io.*;
class Pro10
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter a single digit value : ");
String v1 = in.readLine();
int d = Integer.parseInt(v1);
switch(d)
// checking the value of variable d
{
case 0:
System.out.println("ZERO");
break;
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;
default:
System.out.println("INVALID");
}
}
}
Output:
Enter a single digit value : 6
SIX
Variable Used :
Variables
d
Datatypes
int
Description
Stores the value
11. WAP to calculate total, average and grade for a student for 5 subjects.
import java.io.*;
class Pro11
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter marks in 5 subjects : ");
String v1 = in.readLine();
int ss = Integer.parseInt(v1);
String v2 = in.readLine();
int eng = Integer.parseInt(v2);
String v3 = in.readLine();
int maths = Integer.parseInt(v3);
String v4 = in.readLine();
int sci = Integer.parseInt(v4);
String v5 = in.readLine();
int comp = Integer.parseInt(v5);
int total = ss + eng + maths + sci + comp; //Computing total marks
double avg = total/5.0;
//Computing average
char grade;
if(avg >= 70)
//Calculating grade
grade = 'A';
else
if(avg >= 60)
grade = 'B';
else
if(avg >= 45)
grade = 'C';
else
grade = 'D';
System.out.println("Total : " + total);
System.out.println("Average : " + avg);
System.out.println("Grade : " + grade);
}
}
Output:
Enter marks in 5 subjects : 98
99
100
95
99
Total : 491
Average : 98.2
Grade : A
Variable Used :
Variables
ss
Datatypes
int
Description
Stores ss marks
eng
maths
sci
total
avg
grade
int
int
int
int
double
char
Stores English marks
Stores Maths marks
Stores Science marks
Stores calculated total marks
Stores calculated average
Stores grade
12. WAP to print that a character is a vowel or a consonant using switch case.
import java.io.*;
class Pro12
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter Character : ");
char c = (char)in.read(); // To take character input through keyboard
switch(c)
//Checking for vowel
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("It is a vowel");
break;
default:
System.out.println("It is a consonant");
}
}
}
Output:
Enter Character : i
It is a vowel
Variable Used :
Variables
c
Datatypes
char
Description
Stores a character
13. WAP to find that the entered character is a number, upper case, lower case or a
special character using if-else-if.
import java.io.*;
class Pro12
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter Character : ");
char c = (char)in.read();
char i;
if(c>='A'&&c<='Z')
//Checking for uppercase
System.out.println(c+" is uppercase");
else if(c>='a'&&c<='z')
//Checking for lowercase
System.out.println(c+" is lowercase");
else if(c>='0'&&c<='9')
//Checking for digit
System.out.println(c+" is digit");
else
System.out.println(c+" is a special character");
}
}
Output:
Enter Character : *
* is a special character
Variable Used :
Variables
Datatypes
Description
c
char
Stores a character
14. WAP to calculate area of rectangle, circle, sphere and square using switch case.
import java.io.*;
class Pro14
{
public static void main(String args[])throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(ir);
System.out.println("1.Area of Square");
System.out.println("2.Area of Rectangle");
System.out.println("3.Area of Circle");
System.out.println("4.Area of Sphere");
System.out.println("Enter choice");
String v1=in.readLine();
int ch=Integer.parseInt(v1);
switch(ch)
{
case 1:
//For area of square
System.out.println("Enter side");
String v2=in.readLine();
int s=Integer.parseInt(v2);
int area1 = s*s;
System.out.println("Area = "+area1);
break;
case 2:
//For area of rectangle
System.out.println("Enter length");
String v4=in.readLine();
int l=Integer.parseInt(v4);
//Accepting length
System.out.println("Enter breadth");
String v5=in.readLine();
int b=Integer.parseInt(v5);
//Accepting breadth
int area2 = l*b;
System.out.println("Area = " + area2);
break;
case 3:
//For area of circle
System.out.println("Enter radius");
String v6=in.readLine();
int r=Integer.parseInt(v6);
//Accepting radius
double area3 = 3.14*r*r;
System.out.println("Area = "+ area3);
break;
case 4:
//For area of sphere
System.out.println("Enter radius ");
String v8=in.readLine();
int rad =Integer.parseInt(v8);
double area4 = 4*3.14*rad*rad;
System.out.println("Area = "+area4);
break;
default:
System.out.println("Invalid Choice");
}
}
}
Output:
1.Area of Square
2.Area of Rectangle
3.Area of Circle
4.Area of Sphere
Enter choice
3
Enter radius
2
Area = 12.56
Variable Used :
Variables
ch
s
area1
l
b
area2
r
area3
rad
area4
Datatypes
int
int
int
int
int
int
int
double
int
double
15. WAP to calculate +, -, *, /, % using switch case.
import java.io.*;
class Pro15
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter two numbers : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
String v2 = in.readLine();
int y = Integer.parseInt(v2);
System.out.print("Enter operator + - * / % : ");
char op = (char) in.read(); //To input the operator
switch(op)
//Checking the operator
{
case '+':
System.out.println((x+y));
break;
case '-':
System.out.println(x-y);
break;
case '*':
System.out.println(x*y);
break;
case '/':
Description
Stores the choice of user
Stores length of side of square
Stores area of square
Stores length of rectangle
Stores breadth of rectangle
Stores area of rectangle
Stores the radius of circle
Stores area of circle
Stores radius of sphere
Stores area of sphere
System.out.println(x/y);
break;
case '%':
System.out.println(x%y);
break;
default:
System.out.println("INVALID CHOICE");
}
}
}
Output:
Enter two numbers : 6
5
Enter operator + - * / % : *
30
Variable Used :
Variables
op
Datatypes
char
Description
Stores the operator
16. WAP to find factorial of a number using while loop.
import java.io.*;
class Pro16
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
int i, fact = 1;
//Declaring & Initialising variable fact to 1
for(i = x; i >= 1; i--)
{
fact = fact * i;
//Calulating factorial
}
System.out.println("Factorial of " + x + " is " + fact);
}
}
Output:
Enter any number : 5
Factorial of 5 is 120
Variable Used :
Variables
Datatypes
Description
x
int
To accept a number
i
fact
int
int
Looping variable
To store factorial of number
17. WAP to print any number in reverse order using for loop.
import java.io.*;
class Pro17
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
int rem, rev = 0;
for(;x != 0;)
//Loop runs till x equals 0
{
rem = x%10;
/Extracting last digit
rev = rev * 10 + rem;
//calculating reverse
x=x/10;
}
System.out.println("Reverse is " + rev);
}
}
Output:
Enter any number : 456
Reverse is 654
Variable Used :
Variables
x
rem
rev
Datatypes
int
int
int
Description
To accept a number
To extract last digit
To store reverse of number
18. WAP to print any number in reverse order using do while loop.
import java.io.*;
class Pro17
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
int rem, rev = 0;
do
{
rem = x%10;
//Extracting last digit
rev = rev * 10 + rem; // Reversing the number
x=x/10;
}
while(x!=0);
System.out.println("Reverse is " + rev);
}
}
Output:
Enter any number : 456
Reverse is 654
Variable Used :
Variables
x
rem
rev
Datatypes
int
int
int
Description
To accept a number
To extract last digit
To store reverse of number
19. WAP to input any number and print if it is an Armstrong number. [Hint: 153 is an
Armstrong number in which the sum of cube of each digit is equal to the number
itself].
import java.io.*;
class Pro19
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
int rem, cube, sum = 0, y = x;
for(;x != 0;)
//Loop runs till x becomes 0
{
rem = x%10;
// Extracting last digit
cube = rem * rem * rem; //Calculating cube of each digit
sum = sum + cube;
//Calculating sum of cubes
x=x/10;
}
if(y == sum)
System.out.println(y + " is an armstrong number");
else
System.out.println(y + " is not an armstrong number");
}
}
Output:
Enter any number : 153
153 is an armstrong number
Variable Used :
Variables
x
rem
cube
sum
y
Datatypes
int
int
int
int
int
Description
To accept a number
To extract last digit
To store reverse of number
20. WAP to enter any number and print if it is a perfect number. [Hint: A number is
said to be perfect if it is equal to the sum of all its factors. Eg: 6=1+2+3=6].
import java.io.*;
class Pro20
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
int i, sum = 0;
for(i = 1; i < x ; i++)
{
if(x%i == 0)
sum = sum + i;
}
if(x == sum)
System.out.println(x + " is a perfect number");
else
System.out.println(x + " is not a perfect number");
}
}
21. WAP to enter a number and print the multiplication table till n.
import java.io.*;
class Pro21
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter any number : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
System.out.print("Enter limit : ");
String v2 = in.readLine();
int n = Integer.parseInt(v2);
int i;
for(i = 1; i <= n ; i++)
{
System.out.println(x + "*" + i + "=" + x*i);
}
}
}
22. WAP to print even and odd number till n in descending order and calculate the
sum of even and odd number. Print it in proper format.
import java.io.*;
class Pro22
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x: ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
int i, sume=0, sumo=0;
System.out.println("Printing even numbers in reverse order ");
for(i = x; i >=1; i--)
{
if(i%2==0)
{
System.out.println(i);
sume=sume+i;
}
}
System.out.println("Sum of even numbers " + sume);
System.out.println("Printing odd numbers in reverse order ");
for(i = x; i >=1; i--)
{
if(i%2!=0)
{
System.out.println(i);
sumo=sumo+i;
}
}
System.out.println("Sum of odd numbers " + sumo);
}
}
23. WAP to print Fibonacci series till n.
import java.io.*;
class UptoNFibo
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int last = 1, prev = 1, curr;
System.out.print(last + "\t" + prev + "\t");
while(true) // always true : infinite loop
{
curr = last + prev;
if(curr <= n)
{
System.out.print(curr + "\t");
last = prev;
prev = curr;
}
else
break;
// to come out of infinite loop
}
}
}
24. WAP to calculate the following Series:
a) S = 1 – 2 + 3 – 4 ………………………….N
import java.io.*;
class S1
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int i, sum = 0;
for(i=1;i<=n;i++)
{
if(i%2==0)
sum=sum-i;
else
sum=sum+i;
}
System.out.println("===============");
System.out.println("Sum of series = " +sum);
}
}
b) S = 1*2 + 2*3 + 3*4 …………………..9*10
import java.io.*;
class S2
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int i;
double sum = 0.0;
for(i=1;i<n;i++)
{
sum = sum + i*(i+1);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
c) S = 1^1 + 2^2 + 3^3 …………………..N^N
import java.io.*;
class S3
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int i;
double sum = 0.0;
for(i=1;i<=n;i++)
{
sum = sum + Math.pow(i,i);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
d) S= 1^1 + 3^3 + 5^5 …………………..N^N
import java.io.*;
class S4
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int i;
double sum = 0.0;
for(i=1;i<=n;i+=2)
{
sum = sum + Math.pow(i,i);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
e) S= 2^2 + 3^3 + 4^4 …………………..N^N
import java.io.*;
class S5
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int i;
double sum = 0.0;
for(i=2;i<=n;i++)
{
sum = sum + Math.pow(i,i);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
f) S = 1 + X + X^2 + ……………………….X^N
import java.io.*;
class S6
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
System.out.print("Enter value of x : ");
String v2 = in.readLine();
int x = Integer.parseInt(v2);
int i;
double sum = 1.0;
for(i=1;i<=n;i++)
{
sum = sum + Math.pow(x,i);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
g) S = 1 + x2/2 + x2/3 + x2/4 +……………xn/n
import java.io.*;
class S7
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
System.out.print("Enter value of x : ");
String v2 = in.readLine();
int x = Integer.parseInt(v2);
int i;
double sum = 1.0;
for(i=2;i<=n;i++)
{
sum = sum + Math.pow(x,2)/i;
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
h) S = 1 + 1/3^2 + 1/5^2…………………..10 terms
import java.io.*;
class S8
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1 = in.readLine();
int n = Integer.parseInt(v1);
int i;
double sum = 1.0;
for(i=3;i<=n;i+=2)
{
sum = sum + 1/(Math.pow(i,2));
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
i) S = 1 + (1+2) + (1+2+3) +……………..+ (1+2+3+……+N)
import java.io.*;
class S9
{
public static void main(String [] args) throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1=in.readLine();
int n=Integer.parseInt(v1);
int s=0,sum = 0,i;
for(i=1;i<=n;i++)
{
s = s + i;
sum = sum + s;
}
System.out.println("Sum of series = " +sum);
}
}
j) S= 1! + 2! + 3! .................N!
import java.io.*;
class S10
{
public static void main(String [] args) throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1=in.readLine();
int n=Integer.parseInt(v1);
int f = 1, i, sum = 0;
for(i=1;i<=n;i++)
{
f = f * i;
sum = sum + f;
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
k) S= 1! + 3! + 5! .................N!
import java.io.*;
class S11
{
public static void main(String [] args) throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter value of n : ");
String v1=in.readLine();
int n=Integer.parseInt(v1);
int f = 1, j, i, sum = 0;
for(i=1;i<=n;i+=2)
{
f=1;
for(j=i;j>=1;j--)
{
f = f * j;
}
sum = sum + f;
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
l) S= 1/1! +2/ 2! +3/ 3! .................N/N!
import java.io.*;
class S12
{
public static void main(String [] args) throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter value of n : ");
String v2=in.readLine();
int n=Integer.parseInt(v2);
int i,j;
double f,sum=0.0;
for(i=1;i<=n;i++)
{
f = 1;
for(j=1;j<=i;j++)
{
f = f * j;
}
sum= sum + i/f;
}
System.out.println("Sum of series = " +sum);
}
}
m) S= X/1! +X/ 2! +X/ 3! .................X/N!
import java.io.*;
class S13
{
public static void main(String [] args) throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter value of x : ");
String v1=in.readLine();
int x=Integer.parseInt(v1);
System.out.print("Enter value of n : ");
String v2=in.readLine();
int n=Integer.parseInt(v2);
int i,j;
double f,sum= 0;
for(i=1;i<=n;i++)
{
f = 1;
for(j=1;j<=i;j++)
{
f = f * j;
}
sum = sum + x/f;
}
System.out.println("Sum of series = " +sum);
}
}
n) S = 1 + X^4 + X^6 + X^8 ……………..X^N
import java.io.*;
class S14
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
System.out.print("Enter value of n : ");
String v2 = in.readLine();
int n = Integer.parseInt(v2);
int i;
double sum =x;
for(i=4;i<=n;i+=2)
{
sum = sum + Math.pow(x,i);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
o) S = X^N + X^(N-1) + X^(N-2) ……………..1
import java.io.*;
class S15
{
public static void main(String [] args) throws IOException
{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
System.out.print("Enter value of x : ");
String v1 = in.readLine();
int x = Integer.parseInt(v1);
System.out.print("Enter value of n : ");
String v2 = in.readLine();
int n = Integer.parseInt(v1);
int i;
double sum =0.0;
for(i=n; i>=0; i--)
{
sum = sum + Math.pow(x,i);
}
System.out.println("===============");
System.out.println("Sum of series = " + sum);
}
}
p) S = 1 - X^2/2! + X^3/3! - X^4/4! ……………..X^N/N!
import java.io.*;
class S16
{
public static void main(String [] args) throws IOException
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(r);
System.out.print("Enter value of x : ");
String v1=in.readLine();
int x=Integer.parseInt(v1);
System.out.print("Enter value of n : ");
String v2=in.readLine();
int n=Integer.parseInt(v2);
int i,j;
double f,sum= 1;
for(i=2;i<=n;i++)
{
f = 1;
for(j=1;j<=i;j++)
{
f = f * j;
}
if(i%2==0)
sum = sum - Math.pow(x,i)/f;
else
sum = sum + Math.pow(x,i)/f;
}
System.out.println("Sum of series = " +sum);
}
}
25. Pattern
a)
*
**
***
****
class Pattern1
{
public static void main(String [] args)
{
int i,j;
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
b)
1
22
333
4444
class Pattern2
{
public static void main(String [] args)
{
int i,j;
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
c)
1
12
123
1234
class Pattern3
{
public static void main(String [] args)
{
int i,j;
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
d)
4444
333
22
1
class Pattern4
{
public static void main(String [] args)
{
int i,j;
for (i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
e)
4321
432
43
4
class Pattern5
{
public static void main(String [] args)
{
int i,j;
for (i=1;i<=4;i++)
{
for(j=4;j>=i;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
f)
1111
222
33
4
class Pattern6
{
public static void main(String [] args)
{
int i,j;
for (i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
System.out.print(i);
}
System.out.println();
}
}
}
g)
1234
123
12
1
class Pattern7
{
public static void main(String [] args)
{
int i,j;
for (i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
h)
a
ab
abc
abcd
class Pattern8
{
public static void main(String [] args)
{
char i,j;
for (i='a';i<='d';i++)
{
for(j='a';j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
i)
a
bb
ccc
dddd
class Pattern9
{
public static void main(String [] args)
{
char i,j;
for (i='a';i<='d';i++)
{
for(j='a';j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}