0% found this document useful (0 votes)
3 views12 pages

Chap 1 Computer Problems Fbise

The document provides solutions to 15 programming problems, each accompanied by an algorithm and Python code. Topics include finding the largest number, checking odd/even status, calculating factorials, and more. Each solution is structured with clear steps and sample outputs.

Uploaded by

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

Chap 1 Computer Problems Fbise

The document provides solutions to 15 programming problems, each accompanied by an algorithm and Python code. Topics include finding the largest number, checking odd/even status, calculating factorials, and more. Each solution is structured with clear steps and sample outputs.

Uploaded by

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

Sure!

Here are the solutions to the problems listed, along with their algorithms and sample code in Python.

---

### 1. Find the Largest of Three Numbers

**Algorithm:**

1. Start

2. Let A, B, C be the three numbers.

3. If A > B and A > C, then largest = A

4. Else if B > A and B > C, then largest = B

5. Else largest = C

6. Output largest

7. Stop

**Python Code:**

```python

def find_largest(A, B, C):

if A > B and A > C:

return A

elif B > A and B > C:

return B

else:

return C

print(find_largest(5, 10, 3)) # Output: 10

```

---

### 2. Check if a Number is Odd or Even


**Algorithm:**

1. Start

2. Let N be the number.

3. If N % 2 == 0, print "Even"

4. Else, print "Odd"

5. Stop

**Python Code:**

```python

def check_odd_even(N):

if N % 2 == 0:

return "Even"

else:

return "Odd"

print(check_odd_even(4)) # Output: Even

```

---

### 3. Calculate the Factorial of a Number

**Algorithm:**

1. Start

2. Let N be the number.

3. Initialize factorial = 1

4. For i from 1 to N, multiply factorial by i

5. Output factorial

6. Stop

**Python Code:**

```python
def factorial(N):

factorial = 1

for i in range(1, N + 1):

factorial *= i

return factorial

print(factorial(5)) # Output: 120

```

---

### 4. Check if a Number is Prime

**Algorithm:**

1. Start

2. Let N be the number.

3. If N < 2, print "Not Prime"

4. For i from 2 to sqrt(N):

- If N % i == 0, print "Not Prime" and return

5. Print "Prime"

6. Stop

**Python Code:**

```python

import math

def is_prime(N):

if N < 2:

return "Not Prime"

for i in range(2, int(math.sqrt(N)) + 1):

if N % i == 0:

return "Not Prime"


return "Prime"

print(is_prime(7)) # Output: Prime

```

---

### 5. Reverse a Number

**Algorithm:**

1. Start

2. Let N be the original number.

3. Initialize reversed = 0

4. While N > 0:

- reversed = reversed * 10 + (N % 10)

- N = N // 10

5. Output reversed

6. Stop

**Python Code:**

```python

def reverse_number(N):

reversed_num = 0

while N > 0:

reversed_num = reversed_num * 10 + (N % 10)

N //= 10

return reversed_num

print(reverse_number(1234)) # Output: 4321

```

---
### 6. Find the Sum of Digits

**Algorithm:**

1. Start

2. Let N be the number.

3. Initialize sum = 0

4. While N > 0:

- sum = sum + (N % 10)

- N = N // 10

5. Output sum

6. Stop

**Python Code:**

```python

def sum_of_digits(N):

sum_digits = 0

while N > 0:

sum_digits += (N % 10)

N //= 10

return sum_digits

print(sum_of_digits(123)) # Output: 6

```

---

### 7. Count Vowels and Consonants

**Algorithm:**

1. Start

2. Let string be the input.


3. Initialize vowels = 0, consonants = 0

4. For each character in string:

- If character is a vowel, increment vowels

- Else if character is a consonant, increment consonants

5. Output vowels and consonants

6. Stop

**Python Code:**

```python

def count_vowels_consonants(s):

vowels = "aeiouAEIOU"

count_v = 0

count_c = 0

for char in s:

if char.isalpha():

if char in vowels:

count_v += 1

else:

count_c += 1

return count_v, count_c

print(count_vowels_consonants("Hello World")) # Output: (3, 7)

```

---

### 8. Find the GCD of Two Numbers

**Algorithm:**

1. Start

2. Let A and B be the two numbers.

3. While B ≠ 0:
- temp = B

-B=A%B

- A = temp

4. Output A

5. Stop

**Python Code:**

```python

def gcd(A, B):

while B != 0:

A, B = B, A % B

return A

print(gcd(48, 18)) # Output: 6

```

---

### 9. Calculate the Area of a Circle

**Algorithm:**

1. Start

2. Let radius be R.

3. Calculate area (A):

- A = π * R^2

4. Output A

5. Stop

**Python Code:**

```python

import math
def area_of_circle(R):

return math.pi * (R ** 2)

print(area_of_circle(5)) # Output: 78.53981633974483

```

---

### 10. Check for Palindrome

**Algorithm:**

1. Start

2. Let string be the input.

3. Reverse the string.

4. If original string == reversed string, print "Palindrome"

5. Else print "Not Palindrome"

6. Stop

**Python Code:**

```python

def is_palindrome(s):

return s == s[::-1]

print(is_palindrome("madam")) # Output: True

```

---

### 11. Generate Fibonacci Series

**Algorithm:**

1. Start
2. Let N be the number of terms.

3. Initialize a, b = 0, 1

4. For i from 0 to N-1:

- print a

- temp = a + b

-a=b

- b = temp

5. Stop

**Python Code:**

```python

def fibonacci_series(N):

a, b = 0, 1

for _ in range(N):

print(a, end=' ')

a, b = b, a + b

print()

fibonacci_series(5) # Output: 0 1 1 2 3

```

---

### 12. Sort an Array (Bubble Sort)

**Algorithm:**

1. Start

2. Let array be the list of numbers.

3. Repeat until no swaps:

- For each element in array:

- If current > next, swap them

4. Output sorted array


5. Stop

**Python Code:**

```python

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(0, n-i-1):

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

return arr

print(bubble_sort([64, 34, 25, 12, 22, 11, 90])) # Output: [11, 12, 22, 25, 34, 64, 90]

```

---

### 13. Convert Decimal to Binary

**Algorithm:**

1. Start

2. Let N be the decimal number.

3. Initialize binary = ""

4. While N > 0:

- binary = str(N % 2) + binary

- N //= 2

5. Output binary

6. Stop

**Python Code:**

```python

def decimal_to_binary(N):
binary = ""

while N > 0:

binary = str(N % 2) + binary

N //= 2

return binary

print(decimal_to_binary(10)) # Output: 1010

```

---

### 14. Find the Minimum Element in an Array

**Algorithm:**

1. Start

2. Let array be the list of numbers.

3. Initialize min_value with first element of array.

4. For each element in array:

- If element < min_value, update min_value

5. Output min_value

6. Stop

**Python Code:**

```python

def find_minimum(arr):

min_value = arr[0]

for num in arr:

if num < min_value:

min_value = num

return min_value

print(find_minimum([64, 25, 12, 22, 11, 90])) # Output: 11


```

---

### 15. Calculate Simple Interest

**Algorithm:**

1. Start

2. Let principal (P), rate (R), and time (T) be the inputs.

3. Calculate interest (I):

- I = (P * R * T) / 100

4. Output I

5. Stop

**Python Code:**

```python

def simple_interest(P, R, T):

return (P * R * T) / 100

print(simple_interest(1000, 5, 2)) # Output: 100.0

```

---

You might also like