IT202 Basic IT Lab file
Name- Piyush Kumar
Roll no.- Btech/60120/19
Branch- ECE
Lab-1
Q1. Write a program in python to swap two variables.
var1=input("Enter 1st variable ")
var2=input("Enter 2nd variable ")
print("Before swapping :")
print("1st var = "+var1)
print("2nd var = "+var2)
temp=var1
var1=var2
var2=temp
print("After swapping :")
print("1st var = "+var1)
print("2nd var = "+var2)
Q2. Write a program in python to check if the input character is an alphabet
or not.
ch = input(" Enter the Character : ")
if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')):
print("The Given Character ", ch, "is an Alphabet")
else:
print("The Given Character ", ch, "is Not an Alphabet")
Q3. Write a program in python to input a number and display in hex and
octal equivalent.
var=int(input("Enter your number "))
print(var)
print(oct(var))
print(hex(var))
Q4. Write a program in python to calculate the area of the circle.
rad=float(input("ENTER RADIUS OF CIRCLE"))
rad=3.14*rad*rad
print("AREA OF CIRCLE IS :")
print(float(rad))
Q5. Write a program in python to input an amount(money) and calculate its
change in coins Rs 10, Rs 5, Rs 2, Rs 1.
amm=int(input("Enter the ammount "))
print('your ammount is Rs ',amm)
coin10=amm/10
amm=amm%10
coin5=amm/5
amm=amm%5
coin2=amm/2
coin1=amm%2
print(int(coin10)," 10Rs coins ")
print(int(coin5)," 5Rs coins ")
print(int(coin2)," 2Rs coins ")
print(int(coin1)," 1Rs coins ")
Q6. Write a program in python to input a year and check is it leap year or
not.
year = int(input("Enter Year: "))
if year % 4 == 0 and year % 100 != 0:
print(year, "is a Leap Year")
elif year % 100 == 0:
print(year, "is not a Leap Year")
elif year % 400 ==0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")
Q7. Write a program in python to input three sides of the triangles and
calculate its area.
a = int(input("enter the first side "))
b = int(input("enter the second side "))
c = int(input("enter the third side "))
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Q8. Write a program in python to calculate roots of a quadratic equation.
import cmath
a=float(input("Enter ‘a’ value : "))
b=float(input("Enter ‘b’ value : "))
c=float(input("Enter ‘c’ value : "))
d=float((b*2)-( 4*a *c))
root1=((-b-cmath.sqrt(d))/(2*a))
root2=((-b+cmath.sqrt(d))/(2*a))
print(" The roots are : ")
print(root1)
print(root2)
Q9. Write a program in python to input a character and determine if the
character entered is a vowel or not.
ch = input("Enter a character: ")
if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I'
or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
print(ch, "is a Vowel")
else:
print(ch, "not a vowel")
Q10. Write a program in python to input day number and prompt day of the
week.
weekday = int(input("Enter weekday day number (1-7) : "))
if weekday == 1 :
print("\nMonday")
elif weekday == 2 :
print("\nTuesday")
elif(weekday == 3) :
print("\nWednesday")
elif(weekday == 4) :
print("\nThursday")
elif(weekday == 5) :
print("\nFriday")
elif(weekday == 6) :
print("\nSaturday")
elif (weekday == 7) :
print("\nSunday")
else :
print("\nPlease enter weekday number between 1-7.")
Q11. Write a program in python to input annual salary and calculate tax :
If income is less than Rs 1,50,000 No tax
If income is Rs 1,50,000 - Rs 3,00,000 10%
If income is Rs 3,00,000 - Rs 5,00,000 20%
More than Rs 5,00,000 30%
amm=int(input("enter ammount"))
if amm<150000:
print("no tax")
elif amm>150000 and amm<=300000:
print("tax is ",amm*.1)
elif amm>300000 and amm<=500000:
print("tax is ",amm*.2)
else:
print("tax is ",amm*.3)
Q12. Write a program in python to input a character and if the entered
character is in lowercase then convert it into uppercase and if it is in the
uppercase then convert it into lower case.
ch=input("enter a character :")[0]
if ord(ch)>=65 and ord(ch)<=90:
print(ch.lower())
else:
print(ch.upper())
Q13. Write a program in python to input three numbers and check which is
largest.
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if (a > b) and (a > c):
largest = a
elif (b > a) and (b > c):
largest = b
else:
largest = c
print("The largest number is",largest)
Lab-2
Q1. Write a program in python to input a number and print its factors.
number = int(input("Please Enter any Number: "))
value = 1
print("Factors of a Given Number {0} are:".format(number))
while (value <= number):
if(number % value == 0):
print("{0}".format(value))
value = value + 1
print("Enter the Binary Number: ")
bnum = int(input())
dnum = 0
i=1
while bnum!=0:
rem = bnum%10
dnum = dnum + (rem*i)
i = i*2
bnum = int(bnum/10)
print("\nEquivalent Decimal Value = ", dnum)
Q2. Write a program in python to input a number in binary and convert into
decimal.
print("Enter the Binary Number: ")
bnum = int(input())
dnum = 0
i=1
while bnum!=0:
rem = bnum%10
dnum = dnum + (rem*i)
i = i*2
bnum = int(bnum/10)
print("\nEquivalent Decimal Value = ", dnum)
Q3. Write a program in python to input a number and print its multiplication
table.
num = int(input("Show the multiplication table of? "))
for i in range(1,11):
print(num,'x',i,'=',num*i)
Q4. Write a program in python to input two numbers and calculate its GCD.
num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
i=1
while(i <= num1 and i <= num2):
if(num1 % i == 0 and num2 % i == 0):
gcd = i
i=i+1
print("GCD is", gcd)
Q5. Write a program in python to generate a series 1,2,3,4,......N
number = int(input("Please Enter any Number: "))
print("The List of Natural Numbers from 1 to {0} are".format(number))
for i in range(1, number + 1):
print (i, end = ' ')
Q6. Write a program in python to input a number as N and print sum of
1+3+5+ ….N
maximum = int(input(" Please Enter the Maximum Value : "))
Oddtotal = 0
for number in range(1, maximum+1):
if(number % 2 != 0):
print("{0}".format(number))
Oddtotal = Oddtotal + number
print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, Oddtotal))
Q7. Write a program in python to calculate the average of N numbers.
n = int(input("Enter number"))
sum = 0
# loop from 1 to n
for num in range(1, n + 1, 1):
sum = sum + num
print("Sum of first ", n, "numbers is: ", sum)
average = sum / n
print("Average of ", n, "numbers is: ", average)
Q8. Write a program to find the sum of the series 1+½+⅓+.....+1/N .
n=int(input("Enter the number of terms: "))
sum1=0
for i in range(1,n+1):
sum1=sum1+(1/i)
print("The sum of series is",round(sum1,2))
Lab-3
Q1. Write a program in python that accepts any number and prints the
number of digits in that number.
Number = int(input("Please Enter any Number: "))
Count = 0
while(Number > 0):
Number = Number // 10
Count = Count + 1
print("\n Number of Digits in a Given Number = %d" %Count)
Q2. Write a program in python to print sum of all odd numbers from 1 to
100
i=1
sum=0
while i<=100:
if i%2!=0:
sum=sum+i
i=i+1
print ("Sum of odd numbers",sum)
Q3. Write a program in Python to print numbers from 20 to 1.
n = int(input("Enter the value of n: "))
if (n<=1):
print ("n should be greater than 1")
exit()
print ("value of n: ",n)
print ("numbers from {0} to {1} are: ".format(n,1))
for i in range(n,0,-1):
print (i)
Q4. Write a program in python that display the power of 2, one per line
21, 22, 23, 24, 25, 26, 27, 28, 29, 210
for x in range(1, 11):
print("2 ^", x, "=", 2**x)
Q5. A video library rents new videos for ₹ 75 a day, and old movies for ₹ 50
per day. Write a program to calculate the total charge for a customer’s
video rentals. The program should prompt the user for the number of each
type of video and output the total cost.
num1 = input('Enter the days for new video rented: ')
num2 = input('Enter the days for old video rented: ')
# Add two numbers
sum = int(num1)*75 + int(num2)*50
print('The total cost for',num1,'days new video and',num2,'days old video rent is
',sum)
Q6. Write a program to implement the series
1, 8, 27, 64 , ... ... ... Nth
print("Enter the range of number(Limit):")
n=int(input())
i=1
while(i<=n):
print(i*i*i,end=" ")
i+=1
Q7. Write a program to implement the series
-5 ,-2 ,0,3,6,9,12 ... N
print("Enter the range of number(Limit):")
n=int(input())
i=-9
while(i<=n-1):
print(i+3,end=" ")
i+=3
Q8. Write a program to print sum of the following nth series
-x + x2–x3+ x4+ ....
x = int(input("enter a term = "))
n = int(input("enter range"))
sum = 0
for i in range(1,n):
if i % 2 == 0 :
sum = sum - (x**i)
else :
sum = sum + (x**i)
print("sum of series = ",sum)
Q9. Write a program to print the pattern
1
12
123
1234
12345
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')
Q10. Write a program to print the pattern
*****
****
***
**
*
rows = 5
for i in range(rows + 1, 0, -1):
# nested reverse loop
for j in range(0, i - 1):
# display star
print("*", end=' ')
print(" ")
Lab-4
Q1.Write a program in python to create an array of 10 elements and print it.
arr = [1, 2, 3, 4, 5,6,7,8,10];
print("Elements of given array: ");
#Loop through the array by incrementing the value of i
for i in range(0, len(arr)):
print(arr[i])
Q2.Write a program in python to transpose a given matrix M = [[1, 2], [4, 5],
[3, 6]].
X = [[1,2],
[4 ,5],
[3 ,6]]
result = [[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
Q3. Write a program in python to print the median of a set of numbers in a
file.
n_num = [1, 2, 3, 4, 5]
n = len(n_num)
n_num.sort()
if n % 2 == 0:
median1 = n_num[n//2]
median2 = n_num[n//2 - 1]
median = (median1 + median2)/2
else:
median = n_num[n//2]
print("Median is: " + str(median))
Q4.Write a program in python and use in-built functions to convert a
decimal number to binary,octal and hexadecimal number.
dec = int(input("Enter the number "))
print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
Q5. Write a program in python to sort words in alphabetical order.
my_str = input("Enter a string: ")
words = my_str.split()
words.sort()
for word in words:
print(word)
Q6. Write a program in python to create a function to check if the inputted
number is prime or not.
def testprime(n):
if n==1:
print("the given number is a prime number.")
elif n==2:
print("the given number is a even prime number.")
else:
for x in range(2,n):
if n%x==0:
print("the given number is not a prime number.")
break
else:
print("the given number is a prime number.")
break
return n
n=int(input("enter a number to check whether it is prime or not :"))
testprime(n)
Q7. Write a program in python to create a function to check inputted text is
palindrome or not.
def isPalindrome(s):
return s == s[::-1]
s = input("Enter the string ")
ans = isPalindrome(s)
if ans:
print("Yes it is palindrome")
else:
print("No it is not palindrome")
Q8. Write a program in python to create a function to input a number and
print sum of digits.
def getSum(n):
sum = 0
for digit in str(n):
sum += int(digit)
return sum
n = int(input("Enter the number "))
print(getSum(n))
MATLAB
Ex. 1 Write your first Matlab program
a = 3;
b = 5;
c = a+b
Output:
Ex. 2 The meaning of "a = b".In Matlab and in any programming language,
the statement "a = b" does not mean "a equals b". Instead, it prompts the
action of replacing the content of a by the content of b.
a = 3;
b = a;
Output:
Ex. 3 Basic math operations
a = 3;
b = 9;
c = 2*a+b^2-a*b+b/a-10
Output:
53
Ex. 4 The meaning of "a = b", continued
a = 3;
a = a+1;
Output:
Ex. 5 Formatted output
fprintf('Hello')
Output:
Hello
Ex. 6 Formatted output
a = 3;
b = a*a;
c = a*a*a;
d = sqrt(a);
fprintf('%4u square equals %4u \r', a, b)
fprintf('%4u cube equals %4u \r', a, c)
fprintf('The square root of %2u is %6.4f \r', a, d)
Output:
3 square equals 9
3 cube equals 27
The square root of 3 is 1.7321
Ex. 7 Arrays
a = [3 6 7];
b = [1 9 4];
c=a+b
Output:
4 15 11
Ex. 8 Extracting an individual element of an array
a = [3 6 7];
b = [1 9 4 5];
c = a(2) + b(4)
Output:
c = 11
Ex. 9 Comment
%This program demonstrates how to "comment out"
% a segment of code
A=3;
B = A*A;
% B = 2*B <--- This statement is not executed
C= A+B
Output:
c = 12
Ex. 10 Continuation to next line
summation1 = 1 + 3 + 5 + 7 ...
+ 9 + 11
Note: The three periods (...) allow continuation to the next line of commands. The
two lines in the above example are essentially one line of "summation1 =
1+3+5+7+9+11".
Ex. 11 "Clear" a variable
c1 = 3;
c2 = c1+5;
clear c1
c1
Output:
??? Undefined function or variable 'c1'.
Ex. 12 Intrinsic math functions and constants
x = pi;
y = sin(pi/2)
z = exp(-sin(pi/2))
Output:
y=1
z = 0.3679
Remarks: "pi" (= 3.14159...) is a reserved intrinsic constant. A function within a
function is allowed. The innermost function will be evaluated first, so the 3rd
statement leads to z = exp(-1) = 0.3679.
Ex. 13 Making a quick plot
x = [0:0.1:20];
y = sin(x);
plot(x,y)
The outcome will be the following plot:
The for loop
Ex. 14 Loop: Using for command
b = 3;
for k = 1:5
b
end
Output:
3
3
3
3
3
Ex. 15 For loop: Utility of the dummy index
b = 3;
for k = 1:5
b^k
end
Output:
39
27
81
243
Ex. 16 For loop: More on the dummy index
sum1 = 0;
for k = 1:9
sum1 = sum1+k;
end
sum1
Output:
45
Ex. 17 For loop: More on the dummy index
sum1 = 0;
for k = 1:2:9
sum1 = sum1+k;
end
sum1
Output:
25
Ex. 18 Treatment of array within a loop
b = [3 8 9 4 7 5];
sum1 = 0;
for k = 1:4
sum1 = sum1+b(k);
end
sum1
Output:
24
Ex. 19 Treatment of array within a loop
b = [3 8 9 4 7 5];
sum1 = 0;
for k = 1:2:5
sum1 = sum1+b(k);
end
sum1
Output:
19
Ex. 20 Double loop
sum1 = 0;
for n = 1:2
for m = 1:3
sum1 = sum1+n*m;
end
end
sum1
Output:
18
Ex. 21 Double loop
for n = 1:2
for m = 1:3
fprintf('n = %3u m = %3u \r', n, m)
end
end
Output:
n=1m=1
n=1m=2
n=1m=3
n=2m=1
n=2m=2
n=2m=3
Ex. 22 More complicated use of loop and index
b = [2 5 7 4 9 8 3];
c = [2 3 5 7];
sum1 = 0;
for k = 1:4
sum1 = sum1+b(c(k));
end
sum1
Output:
24
Remark: This program performs the summation of
sum1 = b(c(1))+b(c(2))+b(c(3))+b(c(4))
= b(2)+b(3)+b(5)+b(7)
= 5+7+9+3
= 24
Part3. Basic branching
Ex. 23 The if command
num1 = 7;
if (num1 > 5)
fprintf('%4u is greater than 5 \r', num1)
else
fprintf('%4u is less than or equal to 5 \r', num1)
end
Output:
7 is greater than 5
Ex 24 if - elseif - else (This example is self-explanatory. Try to change the given
value
of num1 and observe the outcome.)
num1 = 4;
if (num1 >= 5)
fprintf('%4i is greater than or equal to 5 \r', num1)
elseif (num1 > 1)
fprintf('%4i is less than 5 but greater than 1 \r', num1)
elseif (num1 == 1)
fprintf('%4i equals 1 \r', num1)
elseif (num1 > -3)
fprintf('%4i is less than 1 but greater than -3 \r', num1)
else
fprintf('%4i is less than or equal to -3 \r', num1)
end
Ex 25 An application - determine whether a given year is a leap year (try to
change the
given value of nyear and observe the outcome)
nyear = 1975;
if (mod(nyear, 400) == 0)
fprintf('%6u is a leap year', nyear)
elseif (mod(nyear,4) == 0) & (mod(nyear,100) ~= 0)
fprintf('%6u is a leap year', nyear)
else
fprintf('%6u is not a leap year', nyear)
end
Output:
1975 is not a leap year
Ex 26 Combine looping and branching
sum1 = 0;
sum2 = 0;
N=9
for k = 1:N
sum1 = sum1+k;
if (mod(k,3) == 0)
sum2 = sum2+k;
end
end
sum1
sum2
Output:
sum1 = 45
sum2 = 18
Ex 27 The while loop
x = 3;
while (x < 100)
x = x*3;
end
x
Output:
x = 243
Part 4. Array and Matrix
Ex. 28 Assign the content of a (one-dimensional) array; Addition of two arrays
a = [2 12 25];
b = [3 7 4];
c = a+b
Output:
c = 5 19 29
Ex. 29 Assign the content of a matrix; Addition of two matrices
a = [3 4; 1 6];
b = [5 2; 11 7];
c = a+b
Output:
c=86
12 13
Ex. 30 Multiplication involving a scalar and an array (or a matrix)
a = [3 5; 1 4];
b = 2*a
Output:
b = 6 10
28
Ex. 31 Element-by-element multiplication involving two 1-D arrays or two
matrices of
the same dimension
a = [2 3 5];
b = [2 4 9];
c = a.*b
Output:
c = 4 12 45
Ex. 32 Element-by-element multiplication of two matrices
a = [2 3; 1 4];
b = [5 1; 7 2];
c = a.*b
Output:
c = 10 3
7 8
Ex. 33 Direct (not element-by-element) multiplication of two matrices
a = [2 3; 1 4];
b = [5 1; 7 2];
c = a*b
Output:
c = 31 8
33 9
Ex. 34 Elementary functions with a vectorial variable
a = [2 3 5];
b = sin(a)
Output:
b = 0.9092 0.1411 -0.9589
Ex. 35 Another example of elementary functions with a vectorial variable
a = [2 3 5];
b = 2*a.^2+3*a+4
Output:
b = 18 31 69
Ex. 36 An efficient way to assign the content of an array
a = [0:0.5:4];
a
Output:
a = 0 0.5 1 1.5 2 2.5 3 3.5 4
Ex 37. Extracting the individual element(s) of a matrix
A = [3 5; 2 4];
c = A(2,2)+A(1,2)
Output:
c=9
Ex. 38 Another example for the usage of index for a matrix
A = [3 5; 2 4];
norm1 = 0;
for m = 1:2
for n = 1:2
norm1 = norm1+A(m,n)^2;
end
end
norm1 = sqrt(norm1)
Output:
norm1 = 7.348
Ex. 39 Solving a system of linear equation
A = [4 1 2; 0 3 1; 0 1 2];
b = [17 ; 19 ; 13];
x = inv(A)*b
Output:
x=1
5
4
Ex. 40 An alternative to Ex. 39
A = [4 1 2; 0 3 1; 0 1 2];
b = [17 ; 19 ; 13];
x = A\b
The output is the same as Ex. 39. Here, A\b is essentially inv(A)*b. (The "\" is
called
"back divide" in Matlab documentations.)
Part 5. Basic graphic applications
Ex. 41 A quick example of plot command: Draw a curve
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
plot(a,b)
Ex. 42 Refine the plot: Line pattern, color, and thickness
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Test','Location','NorthWest')
Ex. 43 Draw multiple curves
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
c = 1.2*a.^2+4*a-3;
hold on
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2)
plot(a,c,'--ok','MarkerFaceColor','c','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Curve 1','Curve 2','Location','NorthWest')
Ex. 44 Draw symbols
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
c = 1.2*a.^2+4*a-3;
hold on
plot(a,b,'or','MarkerFaceColor','g','LineWidth',2)
plot(a,c,'ok','MarkerFaceColor','c','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Curve 1','Curve 2','Location','NorthWest')
Ex. 45 Plot with multiple panels
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
c = 1.2*a.^2+4*a-3;
subplot(1,2,1)
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Curve ','Location','NorthWest')
subplot(1,2,2)
plot(a,c,'--ok','MarkerFaceColor','c','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Curve 2','Location','NorthWest')