Knowee
Questions
Features
Study Tools

1 Explain switch statement with example. 2 Explain algorithm with their benefits. Write a C program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7. 4 Distinguish between exit controlled loop and entry controlled loop. 5 Explain logical operators with an example.

Question

1 Explain switch statement with example. 2 Explain algorithm with their benefits. Write a C program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7. 4 Distinguish between exit controlled loop and entry controlled loop. 5 Explain logical operators with an example.

🧐 Not the exact question you are looking for?Go ask a question

Solution

  1. A switch statement in programming is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.

Example in C:

int day = 3;
switch (day) {
  case 1:
    printf("Monday");
    break;
  case 2:
    printf("Tuesday");
    break;
  case 3:
    printf("Wednesday");
    break;
  default:
    printf("Invalid day");
}

In this example, the switch statement checks the value of 'day'. If it's 1, it prints "Monday", if it's 2, it prints "Tuesday", if it's 3, it prints "Wednesday". If the value of 'day' is none of these, it goes to 'default' and prints "Invalid day".

  1. An algorithm is a step-by-step procedure to solve a problem. Benefits of algorithms include:

    • They provide a clear understanding of the problem.
    • They help in identifying the most efficient method to solve a problem.
    • They make it easier to debug and maintain the code.
  2. C program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7:

#include <stdio.h>
int main() {
    int i, sum = 0, count = 0;
    for(i = 101; i < 200; i++) {
        if(i % 7 == 0) {
            sum += i;
            count++;
        }
    }
    printf("Sum = %d\n", sum);
    printf("Count = %d\n", count);
    return 0;
}

This program uses a for loop to iterate through numbers 101 to 199. If a number is divisible by 7 (i % 7 == 0), it adds the number to the sum and increments the count.

  1. Entry Controlled Loop: In this type of loop, the test condition is checked before entering the loop body. For example, 'for' and 'while' loops. Exit Controlled Loop: In this type of loop, the test condition is checked or evaluated at the end of loop body. Therefore, the loop body will execute at least once, regardless of whether the condition is true or false. For example, 'do-while' loop.

  2. Logical operators are used to determine the logic between variables or values. In C, there are three logical operators: && (Logical AND), || (Logical OR), and ! (Logical NOT).

Example:

int a = 5;
int b = 20;
if (a && b) {
   printf("Line 1 - Condition is true\n" );
}
if (a || b) {
   printf("Line 2 - Condition is true\n" );
}
/* lets change the value of  a and b */
a = 0;
b = 10;
if (a && b) {
   printf("Line 3 - Condition is true\n" );
} else {
   printf("Line 3 - Condition is not true\n" );
}
if (!(a && b)) {
   printf("Line 4 - Condition is true\n" );
}

In this example, Line 1 and Line 2 are true because both a and b are non-zero. Line 3 is not true because a is zero. Line 4 is true because !(a && b) is equivalent to (!a || !b), and since a is zero, !a is 1 (true).

This problem has been solved

Similar Questions

.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.

Write down the C code for the following questions.a) Write a program in C to check if a given number is even or odd using the function.b) Write a program in C to swap two numbers using a function.c) Write a program in C to find the square of a number using the function.d) Write a program in C to find the factorial of a number using the function.e) Write a program in C to calculate the area of circle using the function.f) Write a C program to compute the sum of the first “N” natural numbers using thefunction.g) Write a program in C to print the Fibonacci series using the function.

Analyze the following pieces of code carefully.                    Code 1:int number = 45;boolean even;if (number % 2 == 0)   even = true;else   even = false;Code 2:int number = 45;boolean even = (number % 2 == 0);  Code 2 has compile errors.   Both Code 1 and Code 2 have compile errors.   Code 1 has compile errors. Correct!  Both Code 1 and Code 2 are correct, but Code 2 is better.  Question 8Tips0 / 1 ptsWhat is y after the following switch statement is executed?            int x = 3; int y = 4;switch (x + 3) {  case 6 -> y = 0;  case 7 -> y = 1;  default -> y += 1;}   2 You Answered  5   1 Correct answer  0  Question 9Tips1 / 1 ptsCheck the following Java program carefully. What should be written in the blank? Choose from the given options.                                        public class Test {  public static void main(String[] args) {    System.out.print("The grade is " + getGrade(78.5));    System.out.print("\nThe grade is " + getGrade(59.5));  }  public static ________ getGrade(double score) {    if (score >= 90.0)      return 'A';    else if (score >= 80.0)      return 'B';    else if (score >= 70.0)      return 'C';    else if (score >= 60.0)      return 'D';    else      return 'F';  }}  int Correct!  char   void   String  Question 10Tips1 / 1 ptsConsider the following incomplete code. The Missing body indicated by the comment in the method ‘display’ should be ___.                                        public class Test {  public static void main(String[] args) {    System.out.println(display(5));  }    public static int display(int number) {    // Missing body  }}   System.out.println(number);   System.out.println("number");   return "number"; Correct!  return number;  Question 11Tips1 / 1 pts Given the following method and the statements below, what is k after invoking nPrint("A message", k)?                            static void nPrint(String message, int n) {  while (n > 0) {    System.out.print(message);    n--;  }}int k = 2;nPrint("A message", k);  1   0 Correct!  2   3  Question 12Tips0 / 1 ptsThe statement System.out.printf("%3.1f", 1234.56) outputs __.                                        A) C) D) E) B) Invalid statement. Gives error.  1234.56   123.4 Correct answer  1234.6 You Answered  1234.5  Question 13Tips0 / 1 ptsWhich of the following is correct to obtain a random integer between 5 and 10 (inclusive)?You Answered  5 + Math.random() * 6   5 + (int)(Math.random() * 5)   5 + Math.random() * 5 Correct answer  5 + (int)(Math.random() * 6)  Question 14Tips1 / 1 ptsWhat is the output of the programme below?public static void main(String[] args) {     int x = 0;     while (x < 10) {          if (x & 2 == 0) {               System.out.print(x + “ “);          }          x++;     }}  0 1 2 3 4 Correct!  0 2 4 6 8   2 4 6 8 10   1 3 5 7 9  Question 15Tips1 / 1 ptsConsider the program below:     public static void main(String[] args) {          int numThings = 0;          for (int i = 0; i <= 10; i+=3) {                numThings++;          }     }How many times does the body of the for loop run?  9   10   0 Correct!  4 Quiz score: 11 out of 15Submission details:

Explain the structure of C program. Explain type conversion in C. Write a C program to find prime numbers between a range(using function). Explain any 5 string handling functions available in C. Write a C program to print the address of a variable along with its value.

GIVE ME SOME QUTIONS TO PRACTICE LOOPS IN C

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.