python programs
python programs
Submitted by:
SANAM HARI PRASAD
204C1A05F1
III CSE
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
}
OUTPUT:
Enter a Number:
153
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
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.
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
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:
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
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:
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.
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
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;
}
}
}
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:
OUTPUT:
Enter a Number
22
Palindrome
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.
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 ..
}
}
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
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
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
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.
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.
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
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!
PYTHON CODE:
print("Hello World!")
OUTPUT:
Hello World!
15. 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
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;
}
}
if (n > 2)
System.out.print(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
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
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
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 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,
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 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
str1 and str2 are not equal. Hence, the result is False.
str1 and str3 are equal. Hence, the result is True.
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
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]
PYTHON CODE:
lst = ['gfg', 'is', 'a', 'portal', 'for', 'geeks']
lst.sort()
print(lst)
OUTPUT:
['a', 'for', 'geeks', 'gfg', 'is', 'portal']