Page 1 of 1

Convert the following three Python programs to equivalent C program. Q1 #Q1: Convert the following Python program into C

Posted: Fri May 20, 2022 10:12 am
by answerhappygod
Convert
the following three Python programs to equivalent C
program.
Q1
#Q1: Convert the following Python program into C code
a = 1
b = a
print(a,b)
if a==b and b==1:
print("True, a=%d, b=%d." %(a,b))
else:
print("False, a=%d, b=%d." %(a,b))
print("Completed checking")
c = 2
if a==1 or c >= 2:
print("True, a=%d, c=%d." %(a,c))
c -= 1
else:
print("False, a=%d, c=%d." %(a,c))
if a==1 or c >= 2:
print("True, a=%d, c=%d." %(a,c))
c -= 1
else:
print("False, a=%d, c=%d." %(a,c))
if not (a==1) or c >= 2:
print("True, a=%d, c=%d." %(a,c))
c -= 1
else:
print("False, a=%d, c=%d." %(a,c))
Q2
#Q2: Convert the following Python program into C code
a = 100
for i in range(10):
print ("i = %d" %(i))
print ("a = %d" %(a))
a -= 10
print("for loop completed")
print("the final value of i = %d and a = %d" %(i,a))
# point_x and point_y are the (x,y) coordinates of a point in 2D
space
point_x = []
point_y = []
for i in range(5):
for j in range (6):
print("i = %d and j =
%d" %(i,j))
point_x = i
point_y = j
print("point
coordinate is (%d, %d)" %(point_x, point_y))
if(point_x == 4 and
point_y == 5):
print("exit the for loop")
break
print("the final value of i = %d and j = %d" %(i,j))
print("the final value of point_x = %d and point_y = %d"
%(point_x,point_y))
Q3
#Q3: Convert the following Python program into C code
a = 0
while(a<100):
print("a = %d" %(a))
a += 1
if(a >= 50 and a < 60):
print("a is between
[50 and 60)")
elif(a>=60 and a < 70):
print("a is between
[60 and 70)")
elif(a>=70 and a < 80):
print("a is between
[70 and 80)")
elif(a>=80 and a < 90):
print("a is between
[80 and 90)")
elif(a>=90 and a <= 100):
print("a is between
[90 and 100)")
else:
print("a is less than
50")
print("while loop completed")