CS Practical File 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

I. Write a Python program to print Fibonacci series up to a certain limit.

CODES
nterms = int(input("How many terms? "))

# first two terms

n1, n2 = 0, 1

count = 0

# check if the number of terms is valid

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

print(n1)

nth = n1 + n2

# update values

n1 = n2

n2 = nth

count += 1

RESULTS
How many terms? 10

Fibonacci sequence:

1
1

13

21

34

2. Write a program to accept a number,find out and display whether it is an


Armstrong number or not.

CODES :
# Python program to check if the number is an Armstrong number or not

# take input from the user


num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

RESULTS
Enter a number: 10
10 is not an Armstrong number
>

3. Write a program to accepts a number and display whether the number is a


palindrome or not.

CODES:
int main() {

int n, reversedN = 0, remainder, originalN;

printf("Enter an integer: ");

scanf("%d", &n);

originalN = n;

// reversed integer is stored in reversedN

while (n != 0) {

remainder = n % 10;

reversedN = reversedN * 10 + remainder;

n /= 10;

}
// palindrome if orignalN and reversedN are equal

if (originalN == reversedN)

printf("%d is a palindrome.", originalN);

else

printf("%d is not a palindrome.", originalN);

return 0;

RESULTS
Enter an integer: 1001

1001 is a palindrome.

4. Write the program to accept a number and find out whether it is a perfect
number or not .

CODES:

Number = int(input(" Please Enter any Number: "))


Sum = 0
for i in range(1, Number):
if(Number % i == 0):
Sum = Sum + i
if (Sum == Number):
print(" %d is a Perfect Number" %Number)
else:
print(" %d is not a Perfect Number" %Number)

RESULTS:
Please Enter any Number:6
6 is a Perfect Number
5. Write a program to accept a decimal number and display its binary
number.

CODES:
if num > 1:
decimalToBinary(num // 2)
print(num % 2, end='')
number = int(input("Enter any decimal number: "))

decimalToBinary(number)

RESULTS:
Enter any decimal number:42
101010

6. Write a program to find the factorial of a number.

CODES:
# Python program to find the factorial of a number provided by the user.

# change the value for a different result

num = 7

# To take input from the user

#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

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

factorial = factorial*i

print("The factorial of",num,"is",factorial)

RESULTS:
The factorial of 7 is 5040
>

7. Check whether a number is perfect or not .


CODES:
if __name__ == "__main__" :

# initialisation
i = 2;sum = 1;

# take input from user and typecast into integer


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

# iterating till n//2 value


while(i <= n//2 ) :

# if proper divisor then add it.


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

i += 1

# check sum equal to n or not


if sum == n :
print(n,"is a perfect number")

else :
print(n,"is not a perfect number")

RESULTS:
First run:
Enter a number: 28
28 is a perfect number

Second run:
Enter a number: 14
14 is not a perfect number

- BY : Atul Ratan
Class : XI - A

You might also like