Single For Loop Programs
/ Programming QuestionsQuestion 1
WAP to take a number (n) as input and then find the sum of all natural numbers between 1 to n.
java
import java.util.*;
public class Loops
{
public static void main()
{
int n, i, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
n = sc.nextInt();
// take loop from 1 to n
for (i = 1; i <= n; i++)
{
s += i;
}
System.out.println("Sum of the numbers = " + s);
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1,n + 1,1):
s += i
print('sum = ', s)
Question 2
WAP to take a number (n) as input and then print all natural numbers in reverse from n to 1.
java
import java.util.*;
public class Loops
{
public static void main()
{
int n, i;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
n = sc.nextInt();
// take loop from n to 1
for (i = n; i >= 1; i--)
{
System.out.println(i);
}
}
}
python
n = int(input("Enter a number: "))
for i in range(n, 0, -1):
print(i)
Question 3
A Perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3 Other perfect numbers are 28, 496, and 8128 WAP a program to input a number and check whether it is a perfect number or not
java
import java.util.*;
public class Test
{
public static void main()
{
int n, s = 0, i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
// find proper divisors
for (i = 1; i < n; i++)
{
if (n % i == 0)
s += i;
}
if (s == n)
System.out.println("Perfect Number");
else
System.out.println("Not a Perfect Number");
}
}
python
n = int(input("Enter a number: "))
s = 0
for i in range(1, n, 1):
if n % i == 0:
s += i
if n == s:
print('perfect number')
else:
print('not a perfect number')
Question 4
WAP to input Principal, Rate and Time. Calculate and display the amount which is compounded annualy for
each year by using the formula Interest = (Principal * Rate * Time)/100
For Example, if Principal = 2000, Rate = 10, Time in Years = 5 then
Amount after 1.0 year(s) = 2200.0
Amount after 2.0 year(s) = 2420.0
Amount after 3.0 year(s) = 2662.0
Amount after 4.0 year(s) = 2928.2
Amount after 5.0 year(s) = 3221.02
Finally, Total Interest = 1221.02
java
import java.util.*;
public class MyClass {
public static void main(String args[]) {
double principal, rate, time, interest, i, x;
Scanner sc = new Scanner(System.in);
// take inputs
System.out.println("Enter Principal: ");
principal = sc.nextDouble();
System.out.println("Enter Rate: ");
rate = sc.nextDouble();
System.out.println("Enter Time in Years: ");
time = sc.nextDouble();
// store the principal
x = principal;
// calculate amount for each year
for (i = 1; i <= time; i++)
{
interest = (principal * rate)/100;
principal = principal + interest;
System.out.println("Amount after " + i + " year(s) = " + principal);
}
System.out.println("Total Interest : " + (principal - x));
}
}
python
p = int(input("Enter Principal: "))
r = int(input("Enter Rate: "))
t = int(input("Enter Time: "))
# store the principal
x = p
for i in range(1, t + 1, 1):
p = p + ((p * r)/100)
print("Amount after ", i, " year(s):", p)
# Total interest
print("Total Interest:", (p - x))
Question 5
WAP to input a number n from the user. Then take n numbers as input from the user using a for loop and find the sum of all the numbers
java
import java.util.*;
public class Loops
{
public static void main()
{
int n, a, i, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number of numbers");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
System.out.println("enter number");
a = sc.nextInt();
// keep on adding to sum
s += a;
}
System.out.println("sum of all the numbers = " + s);
}
}
python
n = int(input("Enter the number of inputs: "))
s = 0
for i in range(n):
x = int(input("Enter number: "))
s += x
print('sum =', s)
Question 6
WAP to find the factorial value of any number taken in as input. The Factorial of n is written as n! = 1 *2 * 3 * … * n - For example: 5! = 1 * 2 * 3 * 4 * 5
java
import java.util.*;
public class Loops
{
public static void main()
{
int n, i, f = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
n = sc.nextInt();
// take loop from 1 to n
for (i = 1; i <= n; i++)
{
f *= i;
}
System.out.println("Factorial = " + f);
}
}
python
n = int(input("Enter a number: "))
f = 1
for i in range(1, n + 1, 1):
f *= i
print('factorial =', f)
Question 7
WAP to find the number and sum of all integer between 100 and 200 which are divisible by 9
java
import java.util.*;
public class Test
{
public static void main()
{
int s = 0, i;
// loop from 100 to 200
for (i = 100; i <= 200; i++)
{
if (i % 9 == 0)
s += i;
}
System.out.println("Sum = " + s);
}
}
python
s = 0
for i in range(100, 201, 1):
if i % 9 == 0:
s += i
print('sum =', s)
Question 8
Take two numbers as input from the user then WAP to find the value of one number raised to the power of another. (Do not use Java built-in method)
java
import java.util.*;
public class Loops
{
public static void main()
{
int a, b, i, p = 1;
Scanner sc = new Scanner(System.in);
// first number
System.out.println("enter the first number");
a = sc.nextInt();
//second number
System.out.println("enter the second number");
b = sc.nextInt();
// calculate a to the power b
for (i = 1; i <= b; i++)
{
p *= a;
}
System.out.println(a + " to the power " + b + " = " + p);
}
}
python
a = int(input("enter the first number: "))
b = int(input("enter the second number: "))
x = 1
for i in range(b):
x *= a
print('power =', x)
Question 9
WAP to input 2 numbers and print out their HCF (Highest Common Factor)
java
import java.util.*;
public class Test
{
public static void main()
{
int i, a, b, small, hcf = 1;
Scanner sc = new Scanner(System.in);
System.out.println("enter the first number");
a = sc.nextInt();
System.out.println("enter the second number");
b = sc.nextInt();
// find the smaller number
if (a < b)
small = a;
else
small = b;
// find all the common factors
for (i = 1; i<= small; i++)
{
if ((a % i == 0) && (b % i == 0))
{
hcf = i;
System.out.println(i + " is a common factor");
}
}
System.out.println("HCF of the numbers = " + hcf);
}
}
python
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a < b:
small = a
else:
small = b
hcf = 1
for i in range(1,small + 1, 1):
if a % i == 0 and b % i == 0:
print(i, 'is a factor')
hcf = i
print('HCF = ', hcf)
Question 10
WAP to print the factors of a number. Also print whether the number is prime or not.
java
import java.util.*;
public class Loops
{
public static void main()
{
int a, i, count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
a = sc.nextInt();
// if a is divisible by i then i is a factor of a
for (i = 1; i <= a; i++)
{
if (a % i == 0)
{
System.out.println(i + " is a factor");
count++; // count the number of factors
}
}
// if only 2 factors then number is prime
if (count == 2)
{
System.out.println("Prime Number");
}
}
}
python
n = int(input("enter a number: "))
c = 0
for i in range(1, n + 1, 1):
if n % i == 0:
print(i, 'is a factor')
c += 1
if c == 2:
print('prime number')
else:
print('not a prime number')
Question 11
WAP to input a number n and also input another number x. Then print out the first x multiples of n. For example: if n = 5 and x = 8, then the program should print out the first 8 multiples of 5 which are 5,10,15,20,…,40
java
import java.util.*;
public class Test
{
public static void main()
{
int i, m, n, x;
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
n = sc.nextInt();
System.out.println("enter the number of multiples");
x = sc.nextInt();
for (i = 1; i<= x; i++)
{
m = i * n;
System.out.println(m);
}
}
}
python
n = int(input("Enter a number: "))
x = int(input("Enter the number of multiples: "))
for i in range(1, x + 1, 1):
m = n * i
print(m)
Question 12
WAP to input a number n from the user. Then take n numbers as input from the user using a for loop and at the end it should display the count of positive, negative and zeros entered.
java
import java.util.*;
public class Loops
{
public static void main()
{
int n, a, i, pos = 0, neg = 0, zeros = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the number of numbers");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
System.out.println("enter number");
a = sc.nextInt();
// check if positive
if (a > 0)
{
pos++;
}
// check if negative
else if (a < 0)
{
neg++;
}
// if not +ve or -ve then 0
else
{
zeros++;
}
}
System.out.println("total positive numbers = " + pos);
System.out.println("total negative numbers = " + neg);
System.out.println("total zeros = " + zeros);
}
}
python
n = int(input("Enter the number of inputs: "))
pos = 0
neg = 0
zero = 0
for i in range(n):
x = int(input("Enter number: "))
if x > 0:
pos += 1
elif x < 0:
neg += 1
else:
zero += 1
print('positive numbers =', pos)
print('negative numbers =', neg)
print('zeroes =', zero)
Question 13
WAP to display 10 different numbers from the user and display the greatest and the least number from the set.
java
import java.util.*;
public class Test
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n, max, min, i;
// enter the first number
System.out.println("Enter number 1");
n = sc.nextInt();
max = n;
min = n;
// enter the remaining numbers
for (i = 2; i <= 10; i++)
{
System.out.println("Enter number " + i);
n = sc.nextInt();
// check if more than max
if (n > max)
max = n;
// check if less than min
if (n < min)
min = n;
}
// print out max and min
System.out.println("Maximum Number: " + max);
System.out.println("Minimum Number: " + min);
}
}
python
# enter the first number
n = int(input('enter number 1: '))
# initialize the maximum and minimum numbers
mx = n
mn = n
# enter the rest of the numbers
for i in range(2, 11, 1):
n = int(input('enter number ' + str(i) + ': '))
# if more than max
if n > mx:
mx = n
# if less than min
if n < mn:
mn = n
print('Maximum number: ', mx)
print('Minimum number: ', mn)