0% found this document useful (0 votes)
6 views1 page

Conditionals and Loops

The document contains various Python code snippets for basic programming tasks such as checking if a number is positive, negative, or zero, summing numbers, converting Fahrenheit to Celsius, and performing basic arithmetic operations. It also includes functions for reversing a number, checking for palindromes, summing even and odd digits, and calculating the Nth Fibonacci number. Each code snippet is accompanied by example inputs and outputs.

Uploaded by

ishaan0643.be21
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)
6 views1 page

Conditionals and Loops

The document contains various Python code snippets for basic programming tasks such as checking if a number is positive, negative, or zero, summing numbers, converting Fahrenheit to Celsius, and performing basic arithmetic operations. It also includes functions for reversing a number, checking for palindromes, summing even and odd digits, and calculating the Nth Fibonacci number. Each code snippet is accompanied by example inputs and outputs.

Uploaded by

ishaan0643.be21
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/ 1

Check number

In [11]:
n=int(input())
if n>0:
print("Positive")

elif n<0:
print("Negative")

else:
print("Zero")

10
Positive

Sum of n numbers
In [12]:
n=int(input())
count=1
sum=0
while(count<=n):
sum=sum+count
count=count+1
print(sum)

5
15

Sum of Even Numbers


In [13]:
n=int(input())
i=2
sum=0
while (i<=n):
sum=sum+i
i=i+2

print(sum)

10
30

Fahrenheit to Celsius
In [2]:
s=int(input())
e=int(input())
w=int(input())
while True:
c=0
if s<=e:
c=(s-32)*5/9
print(s,int(c))
s = s + w

else:break

98
120
2
98 36
100 37
102 38
104 40
106 41
108 42
110 43
112 44
114 45
116 46
118 47
120 48

Assignment
Calculator

In [1]:
while True:
choice=int(input())
if choice == 1:
num1 = int(input())
num2 = int(input())
print(int(num1+num2))

elif choice == 2:
num1 = int(input())
num2 = int(input())
print(int(num1-num2))

elif choice == 3:
num1 = int(input())
num2 = int(input())
print(int(num1*num2))
elif choice == 4:
num1 = int(input())
num2 = int(input())
print(int(num1/num2))

elif choice == 5:
num1 = int(input())
num2 = int(input())
print(int(num1%num2))

elif choice == 6:
break
else:
print("Invalid Operation")

5
10
2
0
6
Reverse of a number

In [3]:
n=int(input())
rev=0
while (n>0):
dig=n%10
rev=rev*10+dig
n=n//10

print(rev)

9657
7569
Palindrome number

In [5]:
n=int(input())
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("true")
else:
print("false")

1235
false
Sum of even & odd

In [19]:
n=int(input())
sum_e=0
sum_o=0
while(n>0):
d=n%10
rem=d%2
if rem==0:
sum_e=sum_e+d
else:
sum_o=sum_o+d

n=n//10

print(sum_e ,sum_o)

12548
14 6
Nth Fibonacci Number

In [20]:
n=int(input())
a=0
b=1
i=1
while (i<n):
c=a+b
a=b
b=c
i=i+1

print(b)

20
6765

You might also like