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

CS Practical File 2

The document contains 7 code snippets that demonstrate Python programs for various tasks related to number processing. Each code snippet is followed by sample input/output results. The tasks include: 1) generating the Fibonacci sequence up to a given limit, 2) checking if a number is Armstrong, 3) checking if a number is a palindrome, 4) checking if a number is perfect, 5) converting a decimal number to binary, 6) calculating the factorial of a number, and 7) checking if a number is perfect.

Uploaded by

atul ratan
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)
49 views

CS Practical File 2

The document contains 7 code snippets that demonstrate Python programs for various tasks related to number processing. Each code snippet is followed by sample input/output results. The tasks include: 1) generating the Fibonacci sequence up to a given limit, 2) checking if a number is Armstrong, 3) checking if a number is a palindrome, 4) checking if a number is perfect, 5) converting a decimal number to binary, 6) calculating the factorial of a number, and 7) checking if a number is perfect.

Uploaded by

atul ratan
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/ 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