Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Name: ___Suraj Chaudhary___________________

TP Number: _____ NP000619___________________

Intake: _________NP1F2103IT__________________

QUE NO 1: Design algorithm using flowchart:

Flowchart

Start

Input code

Switch (code)

True
Print “OFF”
Case 0
Break;

False

True
Print “ON”
Case 1
Break;

False true

Case 2 Print “START”

Break;
False

True
Default Print “FAIL”

Break;

End
Algorithm

Step 1: Start
Step 2: Print “Enter code”
Step 3: Switch (code)
a. Case 0:
i. Print ‘OFF’
ii. break
b. Case 1:
i. Print ‘ON’
ii. break
c. Case 2:
i. Print ‘Start’
ii. break
d. default:
i. Print ‘Fail’
ii. break
Step 4: End
QUE NO 2:

Start

End
Input n
Input Code Increase by 1

I >n
Switch For I = 0 End
(code) to n

Case= Print(“OFF”) Break;


0

False

true
Case= Print(“ON”) Break;
1

false

true
Print(“START)
Case= Break;
2

false

Print(“FAIL”) Break;
Default
QUE NO 3:

#include<stdio.h>

int main()
{
    int code;
    int num;
    printf("How many code you want to exacute: ");
    scanf("%d", &num);
    for (int i = 0; i < num; i++)
    {
        printf("Enter a value: ");
        scanf("%d", &code);

        switch (code)
        {
        case '0':
            printf("OFF");
            break;

        case '1':
            printf("ON");
            break;
        case '2':
            printf("START");
            break;
        
        default:
            printf("FAIL");
            
        }
    }
}
QUE NO 4:

#include <stdio.h>

int main()
{
    int code;
    int num;
    printf("How many code you want to exacute: ");
    scanf("%d", &num);
    for (int i = 0; i < num; i++)
    {
        printf("Enter a value: ");
        scanf("%d", &code);

        if (code = '0')
        {
            printf("OFF");
        }
        else if (code = '1')
        {
            printf("ON");
        }
        else if (code = '2')
        {
            printf("START");
        }
        else
        {
            printf("FAIL");
        }
    }
}

Pseudo code:
Input “Number of times to check the code “
Read n
FOR i=0 to n step 1
Input “Enter the code to check”
READ code
IF code ==0 THEN
//Print “OFF”
ELSE IF code ==1 THEN
// Print “ON”
ELSE IF code ==2 THEN
// Print “START”
ELSE
// Print “FAIL”
END IF
END FOR
Que no 5:
a) In Q2 switch case are used and In Q4 if/else statement are used.
b) Switch case Q2 is more simple than If/else statement Q4.
c) Switch case is more simple or suitable than if /else statement because switch case control
statement that allows a value to change control of execution.

You might also like