Humanities/Arts Exam  >  Humanities/Arts Notes  >  Computer Science for Class 11  >  NCERT Solutions: Flow of Control

Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts PDF Download

Exercises

Q1: What is the difference between else and elif construct of if statement?
Ans: if..else statement allows us to write two alternative paths and the control condition determines which path gets executed. If the condition is incorrect/False then else statement will execute. The syntax for if..else statement is as follows.

if condition:

       statement(s)

else:

       statement(s)

Many a times there are situations that require multiple conditions to be checked and it may lead to many alternatives. In such cases we can make a chain of conditions using elif. Syntax of elif is shown below:

if condition : 

    statement

elif condition :

    statement

elif condition :

    statement

|

|

|

|

else :

   statement

Q2: What is the purpose of range() function? Give one example.
Ans:
range( ) function simply generates/return a sequence of number from starting number(by default 0) to one less than end number. This function is commonly used in for loop. for example

for i in range(3, 7):

          print(i)

OUTPUT :

3

4

5

6

Q3: Differentiate between break and continue statements using examples.
Ans:
break statement is used for immediate termination of loop for example

for i in range(7):

     if i==3:

          break

     print(i)

print("Loop Terminate")

OUTPUT :

0

1

2

Loop Terminate

continue statement is used to skip the current iteration of the loop and pass the control to the next iteration. for example :

for i in range(7):

     if i==3:

          continue

     print(i)

print("Loop Terminate")

OUTPUT :

0

1

2

4

5

6

Loop Terminate

Q4: What is an infinite loop? Give one example.
Ans:
A never ending loop is called infinite loop. for example the following loop will print ‘A’ infinite times

i=0

while(i<=0):

     print("A")

Q5: Find the output of the following program segments:

(i) a = 110

    while a > 100:

         print(a)

         a -= 2

Ans:

110
108
106
104
102

(ii) for i in range(20, 30, 2):

         print(i)

Ans:
20
22
24
26
28

(iii) country = 'INDIA' 

    for i in country:

          print (i)

Ans:
I
N
D
I
A

(iv) i = 0; sum = 0

    while i < 9:

         if i % 4 == 0:

              sum = sum + i

         i = i + 2

     print (sum)

Ans: 12

(v) for x in range(1,4):

         for y in range(2,5):

              if x * y > 10:

                   break

              print (x * y)

Ans:
2
3
4
4
6
8
6
9

(vi) var = 7

   while var > 0:

        print ('Current variable value: ', var)

        var = var -1

        if var == 3:

             break

        else:

             if var == 6:

                  var = var -1

                  continue

         print ("Good bye!")

Ans: 
OUTPUT: 
Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4

Programming Exercises

Q1: Write a program that takes the name and age of the user as input and displays a message whether the user is eligible to apply for a driving license or not.(the eligible age is 18 years).
Ans:

nm = input("Enter your name")

age = int(input("Enter your age"))

if age >= 18:

     print("You are eligible for driving licence")

else:

     print("You are not eligible for driving licence")

Q2: Write a function to print the table of a given number. The number has to be entered by the user.
Ans: 

def table(n):

   for i in range(1, 11):

     print("7 * ", i," =", n*i)

num = int(input("Enter any number"))

table(num)

OUTPUT : (If number entered by user is 7)

7 *  1  = 7

7 *  2  = 14

7 *  3  = 21

7 *  4  = 28

7 *  5  = 35

7 *  6  = 42

7 *  7  = 49

7 *  8  = 56

7 *  9  = 63

7 *  10  = 70

Q3: Write a program that prints minimum and maximum of five numbers entered by the user.
Ans: 

num1 = int(input("Enter any number"))

 max=num1

 min=num1

 for i in range(4):

   num = int(input("Enter any number"))

   if max < num:     max = num   if min > num:

     min=num

 print("Minimum number is :",min)    

 print("Maximum number is :",max)

Q4: Write a program to check if the year entered by the user is a leap year or not.
Ans: 

yr = int(input("Enter any year"))

    if yr0 == 0:

       if yr@0 == 0:

           print("Leap Year")

       else:

           print("Not a Leap Year")

    elif yr%4 == 0:

       print("Leap Year")

    else:

       print("Not a Leap Year")

Q5: Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer input by the user.
Ans: 

num = int(input("Enter any number"))

    for i in range(1,num + 1):

        if i%2 == 0: 

            print(5 *i, end = " , ")   

        else:     

            print(5*i*(-1), end = " , ")

Q6: Write a program to find the sum of 1+ 1/8 +1/27……1/n3, where n is the number input by the user.
Ans: 

num = int(input("Enter any number"))

sum = 0

for i in range(1,num + 1):

   sum = sum + 1/i**3

print("Sum of series is : ", sum)

Q7: Write a program to find the sum of digits of an integer number, input by the user.
Ans: 

num = int(input("Enter any number"))

sum = 0

while(num):

   r = num % 10

   sum = sum + r

   num = num // 10

print("Sum of the digit is : ",sum)

Q8: Write a function that checks whether an input number is a palindrome or not.
Ans: 

num = input("Enter any number")

rnum=num[: : -1]

if num == rnum:

     print("Palindrome")

else:

     print("Not a Palindrome")

OR

num = int(input("Enter any number"))

rnum = 0

r = 0

onum = num

while(num):

   r = num % 10

   rnum = rnum * 10 + r

   num = num // 10

if onum == rnum :

   print("Palindrome")

else :

   print("Not Palindrome")

Q9: Write a program to print the following patterns:
Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

Ans:

i)
n = 3

for i in range (1, n + 1):

     sp = (n - i) * " "

     sym =  (2 * i - 1) * "*"

     print(sp, sym)

for j in range(n - 1, 0, -1):

     sp = (n - j) * " "

     sym = (2 * j - 1)* "*"

     print(sp, sym)

ii)

num = 5

for i in range (1, num + 1):

     sp = (num - i) * "  "

     print(sp, end = " ")

     for k in range(i, 1, -1):

         print(k, end = " ")

     for j in range(1, i + 1):

         print(j, end = " ")

     print()

iii)

str = "123456"

for i in (str):

   print(" " * int(i) + str[0 : len(str) - int(i)])

OR

num = 5

for i in range (num, 0, -1):

     sp = (num - i) * "  "

     print(sp, end=" ")

     for k in range(1, i + 1):

           print(k, end=" ")

     print()

iv)

num = 3

k = 0

for i in range (1, num + 1):

     sp = (num - i)*" "

     print(sp, end=' ')

     while (k != (2 * i - 1)) :

         if (k == 0 or k == 2 * i - 2) : 

             print("*", end= "")          

         else :              

             print(" ", end = "")          

         k = k + 1     

     k = 0     

     print( ) 

for j in range (num - 1, 0, -1):     

     sp = (num - j) * " "

     print(sp, end=" ")

     k = (2 * j - 1)

     while (k > 0) :

         if (k==1 or k == 2 * j - 1):             

             print("*",end="")

         else:

             print(" ",end="")

         k = k - 1

     print( ) 

Q10: Write a program to find the grade of a student when grades are allocated as given in the table below. Percentage of the marks obtained by the student is input to the program.
Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/ArtsPercentage of the marks obtained by the student is input to the program.
Ans:

per = int(input("Enter the Percentage"))

if per > 90:

   gr="A"

elif per > 80 and per <= 90:   

   gr = "B" 

elif per > 70 and per <= 80:   

   gr = "C" 

elif per >= 60 and per <= 70:

   gr = "D"

elif per < 60:

   gr = "E"

print("Grade is : ", gr)

Case Study-based Questions

Q1: Write a menu driven program that has options to

  • Accept the marks of the student in five major subjects in Class X and display the same.
  • Calculate the sum of the marks of all subjects. Divide the total marks by number of subjects(i.e. 5), calculate percentage = total marks/5 and display the percentage.
  • Find the grade of the student as per the following criteria:
    Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

Ans:

ch='y'

while (ch=='y'):

  print("Menu Driven Program")

  print("1. Enter Marks of Five Subjects")

  print("2. Calculate Percentage")

  print("3. Calculate Grade")

  print("4. Exit")

  num = int(input("Enter Your Choice"))

  if num == 1 :

     en = int(input("Enter marks of English"))

     m = int(input("Enter marks of Mathematics"))

     sc = int(input("Enter marks of Science"))

     h = int(input("Enter marks of Hindi"))

     sst = int(input("Enter marks of SST"))

     print("Marks in English is :",en)

     print("Marks in Hindi is :",h)

     print("Marks in Mathematics is :",m)

     print("Marks in Science is :",sc)

     print("Marks in Social Studies is :",sst)

  if num == 2 :

     sum = en + m + sc + h + sst 

     per = sum/5 

     print("Percentage is ,", per)

  if num == 3 :

     if per >= 85 :

       print("Grade is A")

     elif per >= 75 and per < 85:       

       print("Grade is B")     

     elif per >= 50 and per < 75 :       

       print("Grade is C")     

     elif per > 30 and per < 50:

       print("Grade is D")

     elif per < 30:

       print("Reappear")

   if num == 4:

     exit()

The document Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts is a part of the Humanities/Arts Course Computer Science for Class 11.
All you need of Humanities/Arts at this link: Humanities/Arts
33 docs|11 tests

FAQs on Flow of Control NCERT Solutions - Computer Science for Class 11 - Humanities/Arts

1. What is the significance of the flow of control in programming?
Ans. The flow of control in programming is significant because it determines the order in which statements, instructions, or function calls are executed in a program. This concept allows programmers to create complex decision-making processes, loops, and branching paths, enabling the development of dynamic and interactive applications. Understanding the flow of control is essential for writing efficient and effective code.
2. What are the different types of control structures used in programming?
Ans. The different types of control structures used in programming include sequential, selection (or conditional), and repetition (or loop) control structures. Sequential control structures execute statements in a linear fashion, selection control structures allow for decisions to be made (such as if-else statements), and repetition control structures enable the execution of a block of code multiple times (like for and while loops).
3. How do conditional statements work in programming?
Ans. Conditional statements in programming work by evaluating a boolean expression and executing specific code blocks based on whether the expression is true or false. Common examples of conditional statements include "if," "else if," and "else." When the condition evaluates to true, the corresponding block of code is executed; otherwise, the program continues with the next condition or statement.
4. Can you explain the role of loops in controlling the flow of a program?
Ans. Loops play a crucial role in controlling the flow of a program by allowing a set of instructions to be executed repeatedly until a specified condition is met. There are various types of loops, such as "for" loops, "while" loops, and "do-while" loops. They help in automating repetitive tasks, managing collections of data, and enhancing the efficiency of the program by reducing the need for redundant code.
5. What are the common errors associated with flow control in programming?
Ans. Common errors associated with flow control in programming include infinite loops, where a loop continues to execute indefinitely due to incorrect conditions, and off-by-one errors, which occur when a loop iterates one time too few or too many. Additionally, improper use of conditional statements can lead to logic errors, where the expected outcome doesn't match the actual execution of the program. Debugging these errors is essential for ensuring the program runs as intended.
Related Searches

Exam

,

Semester Notes

,

Summary

,

mock tests for examination

,

ppt

,

Sample Paper

,

pdf

,

MCQs

,

Objective type Questions

,

Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

,

past year papers

,

Previous Year Questions with Solutions

,

Extra Questions

,

Important questions

,

Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

,

Viva Questions

,

Free

,

practice quizzes

,

shortcuts and tricks

,

study material

,

Flow of Control NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

,

video lectures

;