In: Computer Science
Consider a list called A:
A = [-6, 10, 100, 5, -20, 1000, 9, -15]
Make a for loop that iterates over A and:
HELLO
AS YOU HAVE NOT SPECIFIED ANY LANGUAGE I AM ADDING SOLUTION WITH OUTPUT IN PYTHON AND C
PLEASE GO THROUGH IT AND IF YOU HAVE ANY DOUBT PLEASE FEEL FREE TO ASK IN COMMENT
AND IF THIS HELPS YOU PLEASE GIVE UPVOTE
CODE (C)
#include <stdio.h>
int main()
{
//DECLARING ARRAY WITH GIVEN LIST
int A[8]={-6,10,100,5,-20,1000,9,-15};
int i;
//ITERATING THROUGH LOOP
for(i=0;i<8;i++)
{
int display = 0;
//IF ELEMENT IS NEGATIVE
if(A[i]<0)
display = A[i]*A[i];
//IF ELEMENT IS POSITIVE AND LESS THAN OR EQ TO 100
else if(A[i]<=100)
display = A[i];
//ELSE ELEMENT IS GREATER THAN 100
printf("%d ",display);
}
return 0;
}
OUTPUT (C):
CODE (PYTHON)
def printNum(list):
#ITERATING THROUGH LOOP
for i in list:
display =0
#IF ELEMENT IS NEGATIVE
if(i<0):
display = i*i
#IF ELEMENT IS POSITIVE AND LESS THAN OR EQ TO 100
elif(i<=100):
display = i
#ELSE ELEMENT IS GREATER THAN 100
print(display)
list = [-6,10,100,5,-20,1000,9,-15]
printNum(list)
OUTPUT (PYTHON)