Loop Programs based on Mathematical Series
/ Programming QuestionsQuestion 1
WAP to input a number n and print the first n terms of the following series 2,5,8,11,14,17,….uptil n terms For example: If the value of n = 5, then your program should print out the following 2,5,8,11,14 (n = 5 terms)
java
import java.util.*;
public class Test
{
public static void main()
{
int n, i, x;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
x = (3 * i) - 1;
System.out.println(x);
}
}
}
python
n = int(input("Enter a number: "))
for i in range(1,n + 1):
x = (3 * i) - 1
print(x)
Question 2
WAP to print out the following series 1, 2, 4, 7, 11, 16, 22, 29 .. uptil n terms where n is taken as an input by the program
java
import java.util.*;
public class Test
{
public static void main()
{
int a = 1, n, i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
System.out.println(a);
a += i;
}
}
}
python
n = int(input('Enter the number of terms: '))
a = 1
for i in range(1, n+1, 1):
print(a)
a += i
Question 3
WAP to take 2 inputs from the user - x and n. Then print out the following series x, x2, x3, x4, x5 + …. uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double x, n, i, a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
System.out.println("Enter the value of x");
x = sc.nextFloat();
for (i = 1; i <= n; i++)
{
a = Math.pow(x, i);
System.out.println(a);
}
}
}
python
n = int(input('Enter the value of n: '))
x = int(input('Enter the value of x: '))
for i in range(1, n+1, 1):
a = x**i
print(a)
Question 4
WAP to print out and find the sum of the following series uptil n terms where n comes in as an input from the user 1! + 2! + 3! + 4! + 5! + …. uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double n, i, a = 1, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
for (i = 1; i <= n; i++)
{
a *= i;
System.out.println(a);
s += a;
}
System.out.println("Sum of the series = " + s);
}
}
python
n = int(input('Enter the value of n: '))
a = 1
s = 0
for i in range(1, n+1, 1):
a *= i
print(a)
s += a
print('Sum of the series =', s)
Question 5
WAP to print out the following series uptil n terms where n comes in as an input from the user 1, 2, 9, 64, 625, … uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double n, i, a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
for (i = 1; i <= n; i++)
{
a = Math.pow(i, i-1);
System.out.println(a);
}
}
}
python
n = int(input('Enter the value of n: '))
for i in range(1, n+1, 1):
a = i ** (i - 1)
print(a)
Question 6
WAP to print out the following series uptil n terms where n comes in as an input from the user 0, 7, 26, 63 … uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double n, i, a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
for (i = 1; i <= n; i++)
{
a = Math.pow(i, 3) - 1;
System.out.println(a);
}
}
}
python
n = int(input('Enter the value of n: '))
for i in range(1, n+1, 1):
a = (i ** 3) - 1
print(a)
Question 7
WAP to display n terms of square natural number and their sum, where n is an input For example if n = 4 then the program should print out the series 1,4,9,16 and the sum 1+4+9+16 = 30
java
import java.util.*;
public class Test
{
public static void main()
{
int n, x, s = 0, i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
// loop from 1 to n
for (i = 1; i <= n; i++)
{
x = i * i;
System.out.println(x);
s += x;
}
System.out.println("Sum = " + s);
}
}
python
n = int(input("enter a number: "))
s = 0
for i in range(1, n + 1, 1):
x = i * i
s += x
print('sum =', s)
Question 8
WAP to print out the following series 1 + (1/2!) + (1/3!) + (1/4!) + … uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double n, i, a = 1, x;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
for (i = 1; i <= n; i++)
{
a *= i;
x = (1/a);
System.out.println(x);
}
}
}
python
n = int(input('Enter the value of n: '))
a = 1
for i in range(1, n+1, 1):
a *= i
x = (1/a)
print(x)
Question 9
WAP to print out and also find the sum of the following series 1 +11 + 111 + 1111 + .. uptil n terms where n is taken as an input by the program
java
import java.util.*;
public class Test
{
public static void main()
{
int n, i, x = 0, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
x = (x * 10) + 1;
System.out.println(x);
s += x;
}
System.out.println("Sum = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
x = 0
for i in range(1,n + 1):
x = (x * 10) + 1
print(x)
s += x
print('sum = ', s)
Question 10
WAP to input 2 numbers a and n - then print out and also find the sum of the following series a-a3+a5-a7+a9 … uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double n, i, x, p = 1, a, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
System.out.println("Enter the value of a");
a = sc.nextFloat();
for (i = 1; i <= n; i++)
{
x = Math.pow(a, p);
System.out.println(x);
p += 2;
a *= -1;
s += x;
}
System.out.println("Sum = " + s);
}
}
python
n = int(input('Enter the value of n: '))
a = int(input('Enter the value of a: '))
p = 1
s = 0
for i in range(1, n+1, 1):
x = a**p
print(x)
p += 2
a *= -1
s += x
print("sum of the series:", s)
Question 11
WAP to print out and calculate the sum of the following series 1 - 1/2 + 1/3 - 1/4 + 1/5 -… 1/n where n is a positive integer and input by user.
java
import java.util.*;
public class Test
{
public static void main()
{
int n ;
float i, x, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
if (i % 2 == 0)
x = -(1 / i);
else
x = (1 / i);
System.out.println(x);
s += x;
}
System.out.println("Sum = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1):
if i % 2 == 0:
x = -(1 / i)
else:
x = (1 / i)
print(x)
s += x
print('sum = ', s)
Question 12
WAP to take an input n from the user and find the sum of the following series ½ + ¼ + ⅙ + ⅛ + 1/10 + 1/12 + …. + 1/2n The value of n indicates the number of terms. For example, if the input n = 5, then the series will be ½ + ¼ + ⅙ + ⅛ + 1/10 i.e. n = 5 terms
java
import java.util.*;
public class Loops
{
public static void main()
{
int n;
float i, a, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i<= n; i++)
{
a = (1/(2*i));
s += a;
System.out.println(a);
}
System.out.println("Sum of the numbers = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1):
x = 1/(2*i)
print(x)
s += x
print('sum = ', s)
Question 13
WAP to input 2 numbers x and n - and find the sum of the following series
1+(x2/1)-(x3/2)+(x4/3)-(x5/4) … uptil n terms
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
double n, i, x, a, s = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextFloat();
System.out.println("Enter the value of x");
x = sc.nextFloat();
for (i = 1; i <= n; i++)
{
a = (( Math.pow(x, i + 1) )/ i);
// alternate terms are -ve
if (i % 2 == 0)
a *= -1;
s += x;
}
System.out.println("Sum of the series: " + s);
}
}
python
n = int(input('Enter the value of n: '))
x = int(input('Enter the value of x: '))
s = 1
for i in range(1, n+1, 1):
a = (x ** (i + 1))/i
# alternating terms are even
if i % 2 == 0:
a *= -1
s += x
print("sum of the series:", s)
Question 14
WAP to input a number n and print the first n terms of the following series 0, 3, 8, 15, 24, 35, 48, 63, 80, 99….uptil n terms For example: If the value of n = 4, then your program should print out the following 0,3,8,15 (n = 4 terms)
java
import java.util.*;
public class Test
{
public static void main()
{
int n, i, x = 0, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
x = (i * i) - 1;
System.out.println(x);
s += x;
}
System.out.println("Sum = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1):
x = (i*i) - 1
print(x)
s += x
print('sum = ', s)
Question 15
Take an input n from the user and find out the sum of the following series ½ + ⅔ + ¾ + ⅘ + … till n number of terms For example, if the value of n = 5, then the series will be ½ + ⅔ + ¾+ ⅘ + ⅚ (so, the series will have 5 terms)
java
import java.util.*;
public class Loops
{
public static void main()
{
int n;
float i, a, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i<= n; i++)
{
a = (i/(i + 1));
s += a;
System.out.println(a);
}
System.out.println("Sum of the numbers = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1):
x = i / (i + 1)
print(x)
s += x
print('sum = ', s)
Question 16
Take an input n from the user and find out the sum of the following series 2/1 + 3/2 +4/3 + 5/4 + … till n number of terms For example, if the value of n = 5, then the series will be 2/1 + 3/2 + 4/3+ 5/4 + 6/5 (so, the series will have 5 terms)
java
import java.util.*;
public class Test
{
public static void main()
{
int n;
float i, a, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
n = sc.nextInt();
for (i = 1; i<= n; i++)
{
a = (i + 1)/i;
s += a;
System.out.println(a);
}
System.out.println("Sum of the numbers = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1):
x = (i + 1)/i
print(x)
s += x
print('sum = ', s)
Question 17
Take an input n from the user and find the sum of the following series till n terms ⅕ + 1/10 + 1/15 + 1/20 + …. Till n terms. So, for example if the input of n = 6, the series will have 6 terms which are ⅕ + 1/10 + 1/15 + 1/20 + 1/25 + 1/30
java
import java.util.*;
public class Loops
{
public static void main()
{
int n;
float i, a, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter thember");
n = sc.nextInt();
for (i = 1; i<= n; i++)
{
a = (1/(5*i));
s += a;
System.out.println(a);
}
System.out.println("Sum of the numbers = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1):
x = 1/(5 * i)
print(x)
s += x
print('sum = ', s)
Question 18
Take an input n from the user and print out the Fibonacci Series till n terms. This loop_series_title is defined as follows - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…(Sum of 2 terms gives the next term)
java
import java.util.*;
public class Test
{
public static void main()
{
int a = 1, b = 1, c, n, i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
System.out.println(a);
c = b;
b = b + a;
a = c;
}
}
}
python
n = int(input('Enter the number of terms: '))
a = 1
b = 1
for i in range(n):
print(a)
c = b
b = b + a
a = c