0% found this document useful (0 votes)
10 views

python programs

The document contains various Java and Python programs designed to check for specific numerical properties such as perfect numbers, Armstrong numbers, prime numbers, strong numbers, even or odd numbers, and palindrome numbers. Each section includes a brief explanation of the concept, followed by corresponding code examples in both Java and Python, along with sample outputs. The document serves as a resource for students preparing for placements in computer science.

Uploaded by

kovurukishore9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

python programs

The document contains various Java and Python programs designed to check for specific numerical properties such as perfect numbers, Armstrong numbers, prime numbers, strong numbers, even or odd numbers, and palindrome numbers. Each section includes a brief explanation of the concept, followed by corresponding code examples in both Java and Python, along with sample outputs. The document serves as a resource for students preparing for placements in computer science.

Uploaded by

kovurukishore9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

JAVA & PYTHON PROGRAMS FOR PLACEMENTS

Submitted by:
SANAM HARI PRASAD
204C1A05F1
III CSE

1.Program to check given number is a perfect number or not.


Perfect number is a positive number which sum of all positive divisors.
excluding that number is equal to that number.
Example:
6 is a perfect number, since divisors of 6 are 1, 2 and 3.
Sum of its divisor is 1 + 2+ 3 =6
Note: 6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128

1.1 JAVA program to check Perfect Number:


JAVA CODE:
import java.util.Scanner;
public class PerfectNumber
{
public static void main(String args[])
{
int a;
Scanner s = new Scanner(System.in);
System.out.println("Enter an number: ");
a = s.nextInt();
int sum=0;
for(int i=1;i<a;i++)
if(a%i==0)sum+=i;
if(sum==a)
System.out.println(+a+" is a Perfect Number");
else
System.out.println(+a+" is not a Perfect Number");
}
}

OUTPUT:
Enter an number:
28
28 is a Perfect Number
1.2 JAVA program to check Perfect Number in Range:
JAVA CODE:
import java.util.Scanner;
public class PerfectRange
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the Starting Number of the range: ");
int start = s.nextInt();
System.out.print("Enter the Ending Number of the range: ");
int end = s.nextInt();
for (int num = start; num <= end; num++)
{
int sum = 0;
for (int i = 1; i < num; i++)
{
if (num % i == 0)
sum += i;
}
if (sum == num)
System.out.println(num + " is a Perfect Number");
}

}
}

OUTPUT:
Enter the Starting Number of the range: 1
Enter the Ending Number of the range: 500
6 is a Perfect Number
496 is a Perfect Number
1.3 PYTHON program to check Perfect Number:

PYTHON CODE:
n = int(input("Enter the number: "))
sum = 0
for i in range(1,n+1):
if i%n == 0:
sum = sum+i
print(sum)
if sum == n:
print("Is a Perfect Number")
else:
print("Not a Perfect Number")

OUTPUT:
Enter the number: 6
6
Is a Perfect Number

1.4 PYTHON program to check Perfect Number in Range:


PYTHON CODE:
start = int(input("Enter the starting number of the range= "))
end = int(input("Enter the ending number of the range= "))

for n in range(start, end+1):


sum = 0

for i in range(1, n):


if n%i == 0:
sum += i
if n == sum:
print(n)
OUTPUT:

Enter the starting number of the range= 1


Enter the ending number of the range= 100
6
28
2.Program to check given number is Armstrong number or not.
Those numbers which sum of its digits to power of number of its digits is
equal to that number are known as Armstrong numbers.
Example-1:
153
Total digits in 153 is 3
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example-2:
1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,
1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725

2.1 JAVA Program to check Armstrong number or not.


JAVA CODE:
import java.util.Scanner;
public class ArmStrong
{
public static void main(String[] args)
{
int a = 0;
Scanner s=new Scanner(System.in);
System.out.print("Enter a Number:");
a =s.nextInt();
int duplicate=a;
int original=duplicate;
int digits=0;
int sum=0;
while(a>0)
{
a/=10;//deletes digits
digits++;
}
while(original>0)
{
int n=original%10;
sum+=(int)Math.pow(n, digits);
original/=10;
}
if(sum==duplicate)
System.out.println("Armstrong Number");
else
System.out.println("Not a Armstrong Number");
}

}
OUTPUT:
Enter a Number:
153
Armstrong Number

2.2 JAVA Program to check Armstrong number or not in Range.


JAVA CODE:
import java.util.Scanner;
public class ArmstrongRange
{
public static void main(String args[])
{
int num1, num2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
num1 = sc.nextInt();
System.out.println("Enter the second number:");
num2 = sc.nextInt();
for (int i = num1; i<num2; i++)
{
int check, rem, sum = 0;
check = i;
while(check != 0)
{
rem = check % 10;
sum = sum + (rem * rem * rem);
check = check / 10;
}
if(sum == i)
{
System.out.println(""+i+" is an Armstrong number.");
}
}
}
}

OUTPUT:
Enter the first number:
1
Enter the second number:
500
1 is an Armstrong number.
153 is an Armstrong number.
370 is an Armstrong number.
371 is an Armstrong number.
407 is an Armstrong number.
2.3 PYTHON Program to check Armstrong number or not.
PYTHON CODE:
n=int(input("Enter the number: "))
sum=0
temp=n
while n!=0:
rem=n%10
rem=rem**3
sum=sum+rem
n=n//10
if temp==sum:
print("Armstrong Number")
else:
print("Not Armstrong Number")
OUTPUT:
Enter the number:
153
Armstrong Number

2.4 PYTHON Program to check Armstrong number or not in Range.


PYTHON CODE:
maximum = int(input("Enter the Maximum Value: "))
for Number in range(1, maximum):
Temp = Number
Sum = 0
while(Temp > 0):
Reminder = Temp % 10
Reminder=Reminder**3
Sum = Sum + Reminder
Temp = Temp // 10
if (Sum == Number):
print(Number)

OUTPUT:
Enter the Maximum Value: 500
1
153
370
371
407
3.Program to check given number is prime number or not.
A positive integer which is only divisible by 1 and iself is known as prime
number.
Example of prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
197, 199 etc.
Example: 15 is not prime number because it is divisible by 1, 3, 5 and 15.
Note: 2 is only even prime number.
Take a loop and divide number from 2 to number/2. If the number is not
divisible by any of the numbers then we will print it as prime number.

3.1 JAVA Program to check given number is prime or not.


JAVA CODE:
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
int a;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
a = s.nextInt();
int count=0;
for(int i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if (count==2)
System.out.println(+a+" is a Prime Number");
else
System.out.println(+a+" is not a Prime Number");
}

OUTPUT:
Enter a number:
13
13 is a Prime Number
3.2 JAVA Program to check given number is prime or not in Range.
JAVA CODE:
import java.util.Scanner;
public class PrimeRange
{
public static void main(String args[])
{
int s1, s2, s3, flag = 0, i, j;
Scanner s = new Scanner(System.in);
System.out.println ("Enter the lower limit :");
s1 = s.nextInt();
System.out.println ("Enter the upper limit :");
s2 = s.nextInt();
System.out.println ("The prime numbers in between the entered limits are :");
for(i = s1; i <= s2; i++)
{
for( j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = 0;
break;
}
else
{
flag = 1;
}
}
if(flag == 1)
{
System.out.println(i);
}
}
}
}
OUTPUT:
Enter the Lower limit:
1
Enter the Upper limit:
50
The prime numbers in between the entered limits are:
3
5
7
11
13
17
19
23
29
31
37
41
43
47

3.3 PYTHON Program to check given number is prime or not.

PYTHON CODE:
n = int(input("Enter the number: "))
if n>1:
for i in range(2,n):
if n%i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")
else:
print("Not Prime Number")

OUTPUT:

Enter the number:


23
23 is a Prime Number
3.4 PYTHON Program to check given number is prime or not in Range.

PYTHON CODE:
n1=int(input("Enter the Upper limit= "))
n2=int(input("Enter the Lower limit= "))
for num in range(n1,n2+1):
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)

OUTPUT:
Enter the Upper limit= 1
Enter the Lower limit: 20
2
3
5
7
11
13
17
19
4. Program to check given number is strong number or not.

A number is called strong number if sum of the factorial of its digit is equal to
number itself. Example: 145 since 1! + 4! + 5! = 1 + 24 + 120 = 145

4.1 JAVA Program to check given number is strong number or not.

JAVA CODE:

import java.util.Scanner;
public class StrongNumber
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a number :");
int n=s.nextInt();
int original=n;
int sum=0;
while(n>0)
{
int d=n%10;
int factorial=1;
for(int i=1;i<=d;i++)
factorial*=i;
sum+=factorial;
n/=10;
}
if(sum==original)
System.out.println("Strong Number");
else
System.out.println("Not a Strong Number");
}
}

OUTPUT:
Enter a number:
145
Strong Number
4.2 JAVA Program to check given number is strong number or not in Range.

JAVA CODE:

import java.util.Scanner;
public class StrongRange
{
public static void main(String args[])
{
int a,b;
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();
for (int num = a; num <= b; num++)
{
int original = num;
int sum = 0;
int temp = num;
while (temp > 0)
{
int digit = temp % 10;
int factorial = 1;
for (int i = digit; i >= 1; i--)
{
factorial *= i;
}
sum += factorial;
temp /= 10;
}
if (sum == original)
{
System.out.println(+sum+" is Strong Number");
}
}
}
}

OUTPUT:
Enter the first number:
1
Enter the second number:
1000
1 is Strong Number
2 is Strong Number
145 is Strong Number
4.3 PYTHON Program to check given number is strong number or not.

PYTHON CODE:

n=int(input("Enter the number: "))


sum=0
temp=n
while n>0:
rem=n%10
fact=1
for i in range(1,rem+1):
fact=fact*i
sum=sum+fact
n=n//10
if temp==sum:
print("Strong Number")
else:
print("Not a Strong Number")

OUTPUT:
Enter the number:
145
Strong Number

4.4 PYTHON Program to check given number is strong number or not in Range.

PYTHON CODE:
import math
maximum = int(input("Enter the Maximum Value: "))
for Number in range(1, maximum):
Temp = Number
Sum = 0
while(Temp > 0):
Reminder = Temp % 10
Factorial = math.factorial(Reminder)
Sum = Sum + Factorial
Temp = Temp // 10
if (Sum == Number):
print(Number)

OUTPUT:
Enter the Maximum Value: 500
1
2
145
5. Program to check a number is Even or Odd.

5.1 JAVA Program to check given number is Even or Odd number.

JAVA CODE:

import java.util.Scanner;
public class EvenOdd
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = s.nextInt();
if(num % 2 == 0)
System.out.println(num + " is Even Number");
else
System.out.println(num + " is Odd Number");
}
}
OUTPUT:
Enter a number:
18
18 is Even Number

5.2 PYTHON Program to check given number is Even or Odd number.

PYTHON CODE:
n = int(input("Enter the number: "))
if n%2 == 0:
print(n,"is a Even Number")
else:
print(n,"is a Odd Number")

OUTPUT:
Enter the number:
7
7 is a Odd Number
6. Program to check given number is Palindrome number or not.
To check whether a number is palindrome or not first reverse it and then compare the
number obtained with the original; if both are same then number is palindrome otherwise
not.
6.1 JAVA Program to check given number is Palindrome number or not.

JAVA CODE:
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
int r,sum=0,temp;
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
n = sc.nextInt();
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("Palindrome Number ");
else
System.out.println("Not a Palindrome Number");
}
}

OUTPUT:
Enter a number:
54545
Palindrome Number
6.2 JAVA Program to check given number is Palindrome number or not in Range.

JAVA CODE:
import java.util.Scanner;
public class PalindromeRange
{
static boolean isPalindrome(int n)
{
int reverse = 0;
int temp = n;
while(temp>0)
{
reverse = reverse*10 + temp%10;
temp = temp/10;
}
if(n==reverse)
return true;
return false;
}

public static void main(String args[])


{
int min;
int max;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Lower Limit:");
min = sc.nextInt();
System.out.println("Enter a Upper Limit:");
max = sc.nextInt();
for(int i=min; i<=max; i++)
{
if(isPalindrome(i))
{
System.out.print(" "+i);
}
}

}
}

OUTPUT:
Enter a Lower Limit:
1
Enter a Upper Limit:
50
1 2 3 4 5 6 7 8 9 11 22 33 44
6.3 PYTHON Program to check given number is Palindrome number or not.

PYTHON CODE:

n=input("Enter a Number: ")


if n==n[::-1]:
print("Palindrome Number")
else:
print("Nat a Palindrome Number")

OUTPUT:
Enter a Number
22
Palindrome

6.4 PYTHON Program to check given number is Palindrome number or not.


PYTHON CODE:
def is_palindrome(num):
return str(num) == str(num)[::-1]
def palindrome_range(start, end):
nums = list(range(start, end+1))
palindromes = list(filter(lambda x: is_palindrome(x), nums))
return palindromes
start=int(input("Enter the start value: "))
end=int(input("Enter the end value: "))
print(palindrome_range(start,end))

OUTPUT:
Enter the start value: 1
Enter the end value: 100
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99]
7. Program to solve quadratic equation.

7.1 JAVA Program to solve quadratic equation.


JAVA CODE:
import java.util.Scanner;
public class QuadricEq
{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d= b * b - 4.0 * a * c;
if (d> 0.0)
{
double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
}
else
{
System.out.println("Roots are not real.");
}
}
}

OUTPUT:
Enter the value of a: 1
Enter the value of b: 5
Enter the value of c: 2
The roots are -0.4384471871911697 and -4.561552812808831
7.2 PYTHON Program to solve quadratic equation.

PYTHON CODE:
import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))

d = (b**2) - (4*a*c)

sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print(sol1,sol2)

OUTPUT:
Enter a: 8
Enter b: 5
Enter c: 9
(-0.3125-1.0135796712641785j) (-0.3125+1.0135796712641785j)
8. Program to print Fibonacci series of given range.
A series of numbers in which each sequent number is sum of its two previous numbers is
known as Fibonacci series and each number are called Fibonacci numbers. So Fibonacci
numbers is,
Algorithm for Fibonacci series: Fn = Fn-2 + Fn-1 • Example of Fibonacci series: 0 , 1 ,1 , 2 , 3 ,
5 , 8 , 13 , 21 , 34 , 55 ..

8.1 JAVA Program to print Fibonacci series of given range.


JAVA CODE:
import java.util.Scanner;
public class FibonacciSeries
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Series:");
count = sc.nextInt();
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}
}

OUTPUT:
Enter a Series:
12
0 1 1 2 3 5 8 13 21 34 55 89
8.2 PYTHON Program to print Fibonacci series of given range.

PYTHON CODE:
n=int(input("Enter the number: "))
a=0
b=1
print(a)
print(b)
for i in range(2,n+1):
c=a+b
a=b
b=c
print(c)

OUTPUT:
Enter the number: 5
0
1
1
2
3
5
9. Program to get factorial of given number.
Factorial value: Factorial (n) = 1*2*3 … * n

9.1 JAVA Program to get factorial of given number.

JAVA CODE:
import java.util.Scanner;
public class Factorial
{
public static void main(String args[])
{
int a;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
a = s.nextInt();
int fact=1;
for(int i=1;i<=a;i++)
fact*=i;
System.out.println("Factorial of "+a+" is: "+fact);
s.close();
}
}

OUTPUT:
Enter a number:
5
Factorial of 5 is: 120

9.2 PYTHON Program to get factorial of given number.

PYTHON CODE:
n=int(input("Enter the Number: "))
fact=1
for i in range(1,n+1):
fact=fact*i
print(fact)

OUTPUT:
Enter the Number: 6
720
10. Program to print Floyd’s triangle
Floyd's triangle is a right-angled triangular array of natural numbers, used in computer
science education.
Number of rows of Floyd's triangle to print is entered by the user.
First four rows of Floyd's triangle are as follows: 1 2 3 4 5 6 7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
The nth row sums to n(n2 + 1)/2, the constant of an n × n magic square

10.1 JAVA Program to print Floyd’s triangle.

JAVA CODE:
class Floyds
{
public static void main(String[] args)
{
int n ;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number of rows: ");
n = s.nextInt();
int i, j, k = 1;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}

OUTPUT:
Enter a number of rows: 5
1
23
456
7 8 9 10
11 12 13 14 15
10.2 PYTHON Program to print Floyd’s triangle.

PYTHON CODE:
rows = int(input("Please Enter the total Number of Rows : "))
number = 1
for i in range(rows):
for j in range(i + 1):
print(number, end = ' ')
number = number + 1
print()

OUTPUT:
Please Enter the total Number of Rows : 4
1
2 3
4 5 6
7 8 9 10
11. Program to print Pascal triangle.

11.1 JAVA Program to print Pascal triangle.


JAVA CODE:
import java.util.Scanner;
public class PascalTriangle
{
public static void printPascal(int n)
{
for (int line = 1; line <= n; line++)
{
for (int j = 0; j <= n - line; j++)
{
System.out.print(" ");
}
int C = 1;
for (int i = 1; i <= line; i++)
{
System.out.print(C + " ");
C = C * (line - i) / i;
}
System.out.println();
}
}
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
n = s.nextInt();
printPascal(n);
}
}

OUTPUT:
Enter a number:
5
1
11
121
1331
14641
11.2 PYTHON Program to print Pascal triangle.

PYTHON CODE:
from math import factorial
n=int(input("Enter the Number: "))
for i in range(n):
for j in range(n-i+1):
print(end=" ")
for j in range(i+1):
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")
print()

OUTPUT:
Enter the Number:
5
1
11
121
1331
14641
12. Program to generate multiplication table.

12.1 JAVA Program to generate multiplication table.


JAVA CODE:
import java.util.Scanner;
public class MultiplicationTable
{
public static void main(String[] args)
{
int n;
int a;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
n = s.nextInt();
for( a = 1; a <= 10; a++)
{
System.out.println(+n+" x "+a+" = "+n*a);
}
}
}

OUTPUT:
Enter a number:
18
18 x 1 = 18
18 x 2 = 36
18 x 3 = 54
18 x 4 = 72
18 x 5 = 90
18 x 6 = 108
18 x 7 = 126
18 x 8 = 144
18 x 9 = 162
18 x 10 = 180
12.2 PYTHON Program to generate multiplication table.

PYTHON CODE:
n = int(input("Enter the given number: "))
for i in range(1,11):
print(n,"x" , i ,"=",n * i)

OUTPUT:
Enter the given number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
13. Program to print ASCII value of all characters.
13.1 JAVA Program to print ASCII value of all characters.

JAVA CODE:
import java.util.Scanner;
public class ASCII
{
public static void main(String[] args)
{
char ch = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter a Character: ");
ch = s.next().charAt(ch);
int ascii = ch;
int castAscii = (int) ch;
System.out.println("The ASCII value of " + ch + " is: " + ascii);
System.out.println("The ASCII value of " + ch + " is: " + castAscii);
}
}

OUTPUT:
Enter a Character:
H
The ASCII value of H is: 72
The ASCII value of H is: 72

13.2 PYTHON Program to print ASCII value of all characters.

PYTHON CODE:
str = input("Enter the string: ")
for i in str:
print(i,"\t",ord(i))

OUTPUT:
Enter the string: hello
h 104
e 101
l 108
l 108
o 111
14. Program to print hello world without using semicolon.
14.1 Program to print hello world without using semicolon.

JAVA CODE:
public class HelloWorld
{
public static void main(String[] args)
{
int i = 0;
if(System.out.printf("") == null) {}
for(i = 1; i < 2; System.out.println("Hello World!"))
{
i++;
}
}
}

OUTPUT:
Hello World!

14.2 Program to print hello world without using semicolon.

PYTHON CODE:

print("Hello World!")

OUTPUT:
Hello World!
15. Program to find out Sum of digits of given number.

15.1 JAVA Program to find out sum of digits of given number.

JAVA CODE:
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String args[])
{
int number, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
number = sc.nextInt();
while(number > 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
System.out.println("Sum of Digits: "+sum);
}
}

OUTPUT:
Enter the number: 123
Sum of Digits: 6

15.2 PYTHON Program to find out sum of digits of given number.

PYTHON CODE:
n = int(input("Enter the number: "))
sum = 0
for i in range(1,n+1):
rem = n%10
sum = sum+rem
n = n//10
print(sum)

OUTPUT:
Enter the number: 66
12
16. Program to find out power of given number using recursion.
16.1 JAVA Program to find out power of given number using recursion.

JAVA CODE:
import java.util.Scanner;
public class PowerNum
{
public static void main(String args[])
{
int base;
int expo;
Scanner s = new Scanner(System.in);
System.out.println("Enter Base: ");
base = s.nextInt();
System.out.println("Enter Exponent: ");
expo = s.nextInt();
int power=1;
for(int i=1;i<=expo;i++) power*=base;
System.out.println("Powers of "+base+ " and " +expo+ " is: "+power);
s.close();
}
}

OUTPUT:
Enter Base:
2
Enter Exponent:
8
Powers of 2 and 8 is: 256

16.2 PYTHON Program to find out power of given number using recursion.

PYTHON CODE:
base=int(input("Enter the Base: "))
exp=int(input("Enter the Exponent: "))
res=pow(base,exp)
print(res)

OUTPUT:
Enter the Base: 4
Enter the Exponent: 3
64
17. Program to add two numbers without using addition operator.
17.1 JAVA Program to add two numbers without using addition operator.

JAVA CODE:
import java.util.Scanner;
public class Addition
{
static int add(int a, int b)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter a value: ");
a = s.nextInt();
System.out.println("Enter b value: ");
b = s.nextInt();
for (int i = 1; i <= b; i++)
a++;
return a;
}
public static void main(String[] args)
{
int a = 0;
int b = 0;
int c = add(a,b);
System.out.println("Addition of a and b is: "+c);
}

OUTPUT:
Enter a value:
2
Enter b value:
6
Addition of a and b is: 8
17.2 PYTHON Program to add two numbers without using addition operator.

PYTHON CODE:
x = int(input("Enter x value: "))
y = int(input("Enter y value: "))
def Add(x, y):
while (y != 0):
carry = x & y
x=x^y
y = carry << 1
return x
print(Add(x,y))

OUTPUT:
Enter x value: 6
Enter y value: 7
13
18. Program to subtract two numbers without using subtraction
operator.
18.1 JAVA Program to subtract two numbers without using subtraction operator.

JAVA CODE:
import java.util.Scanner;
public class Subtraction
{
static int subtract(int x, int y)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter x value: ");
x = s.nextInt();
System.out.println("Enter y value: ");
y = s.nextInt();
while (y != 0)
{
int borrow = (~x) & y;
x = x ^ y;
y = borrow << 1;
}
return x;
}
public static void main (String[] args)
{
int x = 0;
int y = 0;
System.out.println("Subtraction of x and y is: " +subtract(x,y));
}
}

OUTPUT:
Enter x value:
27
Enter y value:
20
Subtraction of x and y is: 7
18.2 PYTHON Program to subtract two numbers without using subtraction operator.

PYTHON:
x = int(input("Enter x value: "))
y = int(input("Enter y value: "))
def subtract(x,y):
while (y != 0):
borrow = (~x) & y
x=x^y
y = borrow << 1
return x
print(subtract(x,y))

OUTPUT:
Enter x value: 8
Enter y value: 6
2
19. Program to find largest among three numbers.
19.1 JAVA Program to find largest among three numbers.

JAVA CODE:
import java.util.Scanner;
public class Largest
{
public static void main(String[] args)
{
double n1,n2,n3=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter n1 value: ");
n1 = s.nextDouble();
System.out.println("Enter n2 value: ");
n2 = s.nextDouble();
System.out.println("Enter n3 value: ");
n3 = s.nextDouble();
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the Largest Number.");
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the Largest Number.");
else
System.out.println(n3 + " is the Largest Number.");
}
}

OUTPUT:
Enter n1 value:
23.9865
Enter n2 value:
87.9642
Enter n3 value:
78.6543
87.9642 is the Largest Number.
19.2 PYTHON Program to find largest among three numbers.

PYTHON CODE:
num1 = float(input("Enter num1 value: "))
num2 = float(input("Enter num2 value: "))
num3 = float(input("Enter num3 value: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

OUTPUT:
Enter num1 value: 23.9847
Enter num2 value: 445.9857
Enter num3 value: 345.9853
The largest number is 445.9857
20. Program to find out prime factor of given number.
20.1 JAVA Program to find out prime factor of given number.

JAVA CODE:
import java.util.Scanner;
import java.lang.Math;
public class PrimeFactors
{
public static void primeFactors(int n)
{
System.out.println("Enter an number: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
while (n % 2 == 0)
{
System.out.print(2 + " ");
n /= 2;
}

for (int i = 3; i <= Math.sqrt(n); i += 2)


{
while (n % i == 0)
{
System.out.print(i + " ");
n /= i;
}

}
if (n > 2)
System.out.print(n);
}

public static void main(String[] args)


{
int n = 0;
primeFactors(n);
}
}

OUTPUT:
Enter an number:
90
2335
20.2 PYTHON Program to find out prime factor of given number.

PYTHON CODE:
import math
num = int(input("Enter a number: "))
def prime_factors(num):
while num % 2 == 0:
print(2,)
num = num / 2
for i in range(3, int(math.sqrt(num)) + 1, 2):
while num % i == 0:
print(i,)
num = num / i
if num > 2:
print(num)
num = 200
prime_factors(num)

OUTPUT:
Enter a number: 90
2
3
3
5
21. Program to find out NCR factor of given number. In the
mathematics nCr has defined as nCr = n! /((n-r)!r!)
ncr=fact(n)/(fact(r)*fact(n-r));

21.1 JAVA Program to find out NCR factor of given number. In the mathematics nCr has
defined as nCr = n! /((n-r)!r!) ncr=fact(n)/(fact(r)*fact(n-r));

JAVA CODE:
import java.util.Scanner;
public class NCR {
static int nCr(int n, int r)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter n value: ");
n = s.nextInt();
System.out.println("Enter r value: ");
r = s.nextInt();
return fact(n) / (fact(r) *
fact(n - r));
}
static int fact(int n)
{
if(n==0)
return 1;
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static void main(String[] args)
{
int n = 0;
int r = 0;
System.out.println(nCr(n, r));
}
}

OUTPUT:
Enter n value:
5
Enter r value:
3
10
21.2 PYTHON Program to find out NCR factor of given number. In the mathematics nCr
has defined as nCr = n! /((n-r)!r!) ncr=fact(n)/(fact(r)*fact(n-r));

PYTHON CODE:
def nCr(n, r):
return (fact(n) / (fact(r)
* fact(n - r)))
def fact(n):
if n == 0:
return 1
res = 1
for i in range(2, n+1):
res = res * i
return res
n = int(input("Enter n value: "))
r = int(input("Enter r value: "))
print(int(nCr(n, r)))

OUTPUT:
Enter n value: 6
Enter r value: 3
20
22. Program to convert string to int without using library function
atoi()

22.1 JAVA Program to convert string to int without using library function atoi()

JAVA CODE:
import java.util.Scanner;
public class StringConver
{
public static void convert(String s)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
s = sc.nextLine();
int num = 0;
int n = s.length();
for(int i = 0; i < n; i++)
num = num * 10 + ((int)s.charAt(i) - 48);
System.out.print("String: "+s);
System.out.print("Integer: "+num);
}
public static void main(String[] args)
{
String s = null;
convert(s);
}
}

OUTPUT:
Enter a string:
187
String: 187
Integer: 187
22.2 PYTHON Program to convert string to int without using library function atoi()

PYTHON CODE:
s = input("Enter a string: ")
def convert(s):
num = 0
n = len(s)
for i in s:
num = num * 10 + (ord(i) - 48)
print("String: ",s)
print("Integer: ",num)
if __name__ == '__main__':
convert(s)

OUTPUT:
Enter a string: a
String: a
Integer: 49
23. Program to print 1 to 100 without using loop.
23.1 JAVA Program to print 1 to 100 without using loop.

JAVA CODE:
public class Print1_100
{
public static void printNum(int num)
{
if(num <= 100)
{
System.out.print(num + " ");
printNum(num+1);
}
}
public static void main(String[] args)
{
int n = 1;
printNum(n);
}

OUTPUT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
95 96 97 98 99 100

23.2 PYTHON Program to print 1 to 100 without using loop.

PYTHON CODE:
def num(a):
if a >0:
num(a-1)
print(a,end=" ")
num(100)

OUTPUT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
95 96 97 98 99 100
24. Program for swapping of two numbers.
24.1 JAVA Program for swapping of two numbers.

JAVA CODE:
import java.util.Scanner;
public class SwapTwoNum
{
public static void main(String[] args)
{
int x;
int y;
Scanner s = new Scanner(System.in);
System.out.println("Enter x value: ");
x = s.nextInt();
System.out.println("Enter y value: ");
y = s.nextInt();
System.out.println("Before swapping: x = " + x + " y = " + y);
int temp = x;
x = y;
y = temp;
System.out.println("After swapping: x = " + x + " y = "+y);
}

OUTPUT:
Enter x value:
18
Enter y value:
10
Before swapping: x = 18 y = 10
After swapping: x = 10 y = 18

24.2 PYTHON Program for swapping of two numbers.

PYTHON CODE:
a = int(input("Enter a value: "))
b = int(input("Enter b value: "))
print("Before swapping: a = {}, b = {}".format(a,b))
print("After swapping: a = {}, b = {}".format(b,a))

OUTPUT:
Enter a value: 45
Enter b value: 33
Before swapping: a = 45, b = 33
After swapping: a = 33, b = 45
25. Program to find largest of n numbers.
25.1 JAVA Program to find largest of n numbers.

JAVA CODE:
import java.util.Scanner;
import java.util.Arrays;
public class LargestNum
{
public static void main(String[] args)
{
int n, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements of array: ");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
max = a[0];
for(int i = 0; i < n; i++)
{
if(max < a[i])
{
max = a[i];
}
}
System.out.println("Largest number in the array is: "+max);
}

OUTPUT:
Enter the number of elements in the array: 4
Enter the elements of array:
1
45
67
89
Largest number in the array is: 89
25.2 PYTHON Program to find largest of n numbers.

PYTHON CODE:
lst = []
num = int(input('Enter the number of elements: '))
for n in range(num):
numbers = input('')
lst.append(numbers)
print("Largest number:", max(lst))

OUTPUT:
Enter the number of elements: 6
12
18
47
56
78
90
Largest number: 90
26. Program to find second largest of n numbers.
26.1 JAVA Program to find second largest of n numbers.

JAVA CODE:
import java.util.Arrays;
import java.util.Scanner;
public class SecondLargest
{
public static int getSecondLargest(int[] a, int total)
{
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-2];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
System.out.println("Second Largest: "+getSecondLargest(a,6));
}
}

OUTPUT:
Second Largest: 5
26.2 PYTHON Program to find second largest of n numbers.

PYTHON CODE:
def second_largest(list):
list.sort()
return list[-2]
li=[]
n=int(input("Enter size of list: "))
for i in range(0,n):
e=int(input(" "))
li.append(e)
print("second largest in ",li,"is")
print(second_largest(li))

OUTPUT:
Enter size of list: 3
3
6
9
Second Largest in [3, 6, 9] is:
6
27. Program to count number of digits in a number.
27.1 JAVA Program to count number of digits in a number.

JAVA CODE:
import java.util.Scanner;
public class CountNum
{
public static void main(String[] args)
{
int count = 0;
int num;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number:");
num = s.nextInt();
while (num != 0)
{
num /= 10;
++count;
}
System.out.println("Number of digits: " + count);
}
}

OUTPUT:
Enter a number: 123
Number of digits: 3

27.1 PYTHON Program to count number of digits in a number.

PYTHON CODE:
n=input("Enter the number: ")
count=0
for i in n:
count=count+1
print(count)

OUTPUT:
Enter the number: 4444444444444
13
28. Program to print Binary Numbers Pyramid Pattern.
28.1 JAVA Program to print Binary Numbers Pyramid Pattern.

JAVA CODE:
import java.util.Scanner;
public class BinPyramid
{
public static void main(String args[])
{
int i,j,k,count=1,num=1,space;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();
System.out.print("\nYour pattern here\n\n");
space=rows-1;
for(i=1; i<=rows; i++)
{
for(j=1; j<=space; j++)
{
System.out.print(" ");
}
for(k=1; k<=num; k++)
{
System.out.print(count%2);
count++;
}
space--;
num+=2;
System.out.print("\n");
}
}
}

OUTPUT:
Enter the number of rows: 5

1
010
10101
0101010
101010101
28.2 PYTHON Program to print Binary Numbers Pyramid Pattern.

PYTHON CODE:
n=int(input("Enter the number: "))
for i in range(n):
print(" "*(n-i-1),end=" ")
for j in range(i+1):
print((j+1)%2,end=" ")
print()

OUTPUT:
Enter the number: 4
1
10
101
1010
29. Program on Basic String Operations.
29.1 JAVA Programs on Basic String Operations.

Java String Operations


Java String provides various methods to perform different operations on strings. We will look
into some of the commonly used string operations.

1. Get length of a String


To find the length of a string, we use the length() method of the String. For example,

JAVA CODE:
import java.util.Scanner;
public class StrLen
{
public static void main(String[] args)
{
String greet;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
greet = sc.nextLine();
System.out.println("String: " + greet);
int length = greet.length();
System.out.println("Length: " + length);
}
}

OUTPUT:
Enter a string:
Hello! Java
String: Hello! Java
Length: 11

In the above example, the length() method calculates the total number of characters in the
string and returns it. To learn more, visit Java String length().
2. Join Two Java Strings
We can join two strings in Java using the concat() method. For example,

JAVA CODE:
import java.util.Scanner;
public class StrJoin
{
public static void main(String[] args)
{
String first;
String second;
Scanner sc = new Scanner(System.in);
System.out.println("First String: ");
first = sc.nextLine();
System.out.println("Second String: ");
second = sc.nextLine();
// join two strings
String joinedString = first.concat(second);
System.out.println("Joined String: " + joinedString);
}
}

OUTPUT:
First String:
Java
Second String:
Programming..!
Joined String:
JavaProgramming..!

In the above example, we have created two strings named first and second. Notice the
statement,

String joinedString = first.concat(second);


Here, the concat() method joins the second string to the first string and assigns it to the
joinedString variable.

We can also join two strings using the + operator in Java. To learn more, visit Java String
concat().
3. Compare two Strings
In Java, we can make comparisons between two strings using the equals() method. For
example,

JAVA CODE:
import java.util.Scanner;
public class StrCompare
{
public static void main(String[] args)
{
String first;
String second;
String third;
Scanner sc = new Scanner(System.in);
System.out.println("First String: ");
first = sc.nextLine();
System.out.println("Second String: ");
second = sc.nextLine();
System.out.println("Third String: ");
third = sc.nextLine();
boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);
boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}
}

OUTPUT:
First String:
Java
Second String:
Java
Third String:
Programs
Strings first and second are equal: true
Strings first and third are equal: false
29.2 PYTHON Programs on Basic String Operations.

Python String Operations


There are many operations that can be performed with strings which makes it one of the
most used data types in Python.

1. Compare Two Strings


We use the == operator to compare two strings. If two strings are equal, the operator
returns True. Otherwise, it returns False. For example,

PYTHON CODE:
str1 = str(input("Enter str1: "))
str2 = str(input("Enter str2: "))
str3 = str(input("Enter str3: "))
print(str1 == str2)
print(str1 == str3)

OUTPUT:
Enter str1: Hello, world!
Enter str2: I love Python.
Enter str3: Hello, world!
False
True

In the above example,

str1 and str2 are not equal. Hence, the result is False.
str1 and str3 are equal. Hence, the result is True.

2. Join Two or More Strings


In Python, we can join (concatenate) two or more strings using the + operator.

PYTHON CODE:
str1 = str(input("Enter str1: "))
str2 = str(input("Enter str2: "))
result = str1 + str2
print(result)

OUTPUT:
Enter str1: Hello,
Enter str2: Jack
Hello, Jack

In the above example, we have used the + operator to join two strings: str1 and str2.
3. Through a Python String
We can iterate through a string using a for loop. For example,

PYTHON CODE:
str1 = str(input("Enter str1: "))
for letter in str1:
print(letter)

OUTPUT:
Enter str1: Hello
H
e
l
l
o

4. Python String Length


In Python, we use the len() method to find the length of a string. For example,

PYTHON CODE:
str1 = str(input("Enter str1: "))
print(len(str1))

OUTPUT:
Enter str1: SriRama
7
5. String Membership Test
We can test if a substring exists within a string or not, using the keyword in.

PYTHON CODE:
print('a' in 'program')
print('at' not in 'battle')

OUTPUT:
True
False
30. Program to sort table of strings.
30.1 JAVA Program to sort table of strings.

JAVA CODE:
import java.util.Arrays;
public class SortTableStr
{
public static void main(String args[])
{
String[] countries = {"Zimbabwe", "South-Africa", "India", "America", "Yugoslavia", "
Australia", "Denmark", "France", "Netherlands", "Italy", "Germany"};
int size = countries.length;
for(int i = 0; i<size-1; i++)
{
for (int j = i+1; j<countries.length; j++)
{
if(countries[i].compareTo(countries[j])>0)
{
String temp = countries[i];
countries[i] = countries[j];
countries[j] = temp;
}
}
}
System.out.println(Arrays.toString(countries));
}
}

OUTPUT:
[ Australia, America, Denmark, France, Germany, India, Italy, Netherlands, South-Africa,
Yugoslavia, Zimbabwe]

30.2 PYTHON Program to sort table of strings.

PYTHON CODE:
lst = ['gfg', 'is', 'a', 'portal', 'for', 'geeks']
lst.sort()
print(lst)

OUTPUT:
['a', 'for', 'geeks', 'gfg', 'is', 'portal']

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy