Chapter - 5 (Mixed question)
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1: This programme is use to see how many numbers are greater than 10 which enter by user.
Code:
count=0
for i in range(10):
num=eval(input('Enter a number:'))
if num >10:
count= count+1
print('There are', count, 'numbers greater than 10.')
Result:
Enter a number:12
Enter a number:5
Enter a number:2
Enter a number:84
Enter a number:84
Enter a number:10
Enter a number:2
Enter a number:4
Enter a number:5
Enter a number:40
There are 4 numbers greater than 10.
- Here we use count, it is use for count. In the first line use count=0, You can use any word instead of count, It tells that counting starting from zero.
- After in 2nd line use for loop which repeat for 10 times and then we use if statement , and use in if statement that num in this is greater than 10 then the counting start and it show result in the last line.
Example-2: This programme is use for see then number enter by user greater than 10 and how many zero in it.
Code:count1=0
count2=0
for i in range(10):
num=eval(input('Enter a number:'))
if num> 10:
count1=count1+1
if num==0:
count2=count2+1
print('There are', count1, 'numbers greater than 10.')
print('There are', count2, zeroes.')
Result:
Enter an number:82
Enter an number:10
Enter an number:45
Enter an number:2
Enter an number:4
Enter an number:8
Enter an number:62
Enter an number:10
Enter an number:15
Enter an number:40
There are 5 numbers greater than 10.
There are 0 zeroes.
Explanation: You can compare with the previous example but in this example we use 2 count statement. One for greater than 10 and other for zero.You can easily understand by see the result.
Example-3: This programme is use for see how many number between 1 and 100 have square which remainder is 4.
Code:
count=0
for i in range(1,101):
if (i**2)%10==4:
count=count+1
print(count)
Result:
20
Explanation: In this programme in 1st line use for loop between 1 and 100 then see then 2nd line square all number and divide by 10 if result 4 then it count and show in result.
Example-4: To sum all the digit between 1 and 9.
Code:
s=0
for i in range(1,10):
s=s+i
print('The sum is',s)
Result:
The sum is 45.
Explanation: In this programme use "s" instead of count to show that no effect if you use s instead of count.
In 2nd line use range between 1 and 9.
Then all these number add then result is 45.
Example-5: Ask user to take ten input and find average of all these number.
Code:
s =0
for i in range(10):
num=eval(input('Enter a number:'))
s= s+num
print('The average is ', s/10)
Result:
Enter a number:15
Enter a number:20
Enter a number:70
Enter a number:65
Enter a number:45
Enter a number:2
Enter a number:10
Enter a number:9
Enter a number:45
Enter a number:12
The average is 29.3
Explanation: Here we ask user to give input and then we sum all the digit then this digit multiply by 10 because average=sum of all digit/number of digit.
Example-6: To see whether the number is prime or not.
Code:
num=eval(input('Enter a number:'))
flag=0
for i in range(2, num):
if num%i==0:
flag=flag+1
if flag==1:
print('Not prime')
else:
print('Prime')
Result:
Enter a number: 25
Not prime
Explanation: Prime number as you know divisible by itself or 1. First we ask user to enter a number.
In second we use flag same as count term. In the next line use for loop and the number we enter as num.
If the number which we enter is in this example 25. Now this divide it between 2 and 24 if the remainder is zero then this number is not prime other wise prime.
Example-7: Ask user to enter 9 number and find the largest number in it.
Code:
largest=eval(input('Enter a positive number:'))
for i in range(9):
num=eval(input('Enter a positive number:'))
if num> largest:
largest=num
print('Largest number:', largest)
Result:
Enter a positive number:15
Enter a positive number:10
Enter a positive number:9
Enter a positive number:4
Enter a positive number:78
Enter a positive number:45
Enter a positive number:10
Enter a positive number:12
Enter a positive number:46
Enter a positive number:78
Largest number: 78
Explanation: Here first we use largest you can use any name here it is use to trace all the number which user write after in the loop. In further code we use if num> largest and after it largest=num if you not use this largest=num command then it print the first digit you enter.
Example-8: This programme is use to ask the user to enter a number and tell the user which number can divide it completely with remainder zero (prime or nom prime)
Code:
flag=0
num=eval(input('Enter number:'))
for i in range(2,num):
if num%i==0:
flag= flag+1
print(i, flag)
Result:
2 0
3 0
4 0
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
Explanation: You see this is the previous example-6 ,but here we use "flag" to count the number. In result you see counting on left side from 2 to 24 and to this right number 0 and 1. 1 start in front of 5 that means only 5 completely divide it and no other number completly divide it.
Example-9: In this programme we import a random number between 1 and 4 and multiply with 'Hello'
Code:
from random import randint
num=randint(1,5)
for i in range(6):
print('Hello'*num)
Result:
HelloHelloHelloHello
HelloHelloHelloHello
HelloHelloHelloHello
HelloHelloHelloHello
HelloHelloHelloHello
HelloHelloHelloHello
Explanation: In the first line we use random code which import the random number and in the 3rd line we use range(6) that means code repeat for 6 times and the random number it choose it multiply with Hello. Hello repeat for 4 times that means system choose '4'.That's why it print 4 time Hello in a line.
Example-10: Ask the user for the loop that goes 6 time and print random number in this loop.
Code:
from random import randint
for i in range(6):
num=randint(1,5)
print('Hello'*num)
Result:
Hello
HelloHello
HelloHelloHelloHello
HelloHelloHelloHello
HelloHelloHelloHello
HelloHelloHello
Explanation: In the previous example you see 4 time Hello in 6 line.
But here you see different time Hello repeat because you add num=randint(1,5) below the for loop. That's why it repeat different number in different time.
Example-11: Import 100 random number in between 1 and 100 and find the number which is divisible by 12.
Code:
from random import randint
count=0
for i in range(100):
num=randint(1,100)
if num%12==0:
count=count+1
print(num)
print('Number of multiples of 12:', count)
Result:
48
96
84
12
72
12
48
12
12
72
Number of multiples of 12:10
Explanation: First we import random command and then start for loop in which we import random 100 number because loop start 100 times and then find the digit which completely divide by 12 so system find 10 digit.
Note: This give another result if you again restart the shell.
No comments:
Post a Comment
If you have any doubt, let me know.