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

Ict Program

These are important python programs with their function

Uploaded by

ankurjan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Ict Program

These are important python programs with their function

Uploaded by

ankurjan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Programs

### 1. Greet the User


```python
name = input("Enter your name: ")
print("Hello,", name + "!")
```

### 2. Sum of Two Numbers


```python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("The sum is:", sum)
```

### 3. Average of Three Numbers


```python
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
average = (num1 + num2 + num3) / 3
print("The average is:", average)
```

### 4. Multiplication Table


```python
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
```

### 5. Check Even or Odd


```python
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
```
### 6. Factorial of a Number
```python
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)
```

### 7. Sum of Natural Numbers


```python
n = int(input("Enter a number: "))
sum = 0
for i in range(1, n + 1):
sum += i
print("The sum of natural numbers up to", n, "is", sum)
```

### 8. Print Each Character of a String


```python
string = input("Enter a string: ")
for char in string:
print(char)
```

### 9. Count Vowels in a String


```python
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count += 1
print("Number of vowels:", count)
```

### 10. Reverse a String


```python
string = input("Enter a string: ")
reversed_string = string[::-1]
print("Reversed string:", reversed_string)
```

### 11. Print a Range of Numbers


```python
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
for i in range(start, end + 1):
print(i)
```

### 12. Check Prime Number


```python
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
```

### 13. Fibonacci Sequence


```python
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci sequence:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
```

### 14. Sum of Digits


```python
num = int(input("Enter a number: "))
sum_of_digits = 0
while num > 0:
digit = num % 10
sum_of_digits += digit
num //= 10
print("Sum of digits:", sum_of_digits)
```

### 15. Find Largest of Three Numbers


```python
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
largest = max(num1, num2, num3)
print("The largest number is:", largest)
```

### 16. Check Palindrome


```python
string = input("Enter a string: ")
if string == string[::-1]:
print(string, "is a palindrome")
else:
print(string, "is not a palindrome")
```

### 17. Count Words in a String


```python
string = input("Enter a string: ")
words = string.split()
print("Number of words:", len(words))
```

### 18. Temperature Conversion (Celsius to Fahrenheit)


```python
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
```

### 19. Simple Calculator


```python
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")
if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
result = num1 / num2
else:
result = "Invalid operation"
print("The result is:", result)
```

### 20. Check Leap Year


```python
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
```

### 21. Count Occurrences of a Character


```python
string = input("Enter a string: ")
char = input("Enter a character to count: ")
count = string.count(char)
print("The character '", char, "' occurs", count, "times in the string.")
```

### 22. Armstrong Number Check


```python
num = int(input("Enter a number: "))
sum_of_cubes = sum([int(digit) ** 3 for digit in str(num)])
if sum_of_cubes == num:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
```

### 23. Generate Multiplication Table


```python
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
```

### 24. Calculate Compound Interest


```python
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
compound_interest = principal * (1 + rate / 100) ** time
print("The compound interest is:", compound_interest)
```

### 25. Display Even Numbers in a List


```python
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
even_numbers = [num for num in numbers if num % 2 == 0]
print("Even numbers in the list:", even_numbers)
```

You might also like