ASSIGNMENT - 5


ASSIGNMENT - 5 



Please follow me for more practicals....



-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


1. State use of loop statement in C Program.

A1: loop statements are used to execute a block of code repeatedly as long as a certain condition is true.

 

2. Explain syntax of for loop.**

A2: for (initialization; condition; increment / decrement;) {

    // code to be repeated

}

Initialization: It indicates the starting point of the loop.

Condition: It determines when loop will stop.

Increment / Decrement: How the value of the counter variable will change.

 

3. Compare while loop and do-while loop.**

A3: In a while loop, if the initial condition is false, the loop block will not be executed at all.

In a do-while loop, the loop block is executed at least once, regardless of the initial condition.

 

4. Explain while loop with example.

The while loop in C is used to repeatedly execute a block of code as long as a specified condition is true.

Example:

    while (i < 5)

    {

        printf("%d\n", i);

        i++;

    }

 

Please follow me for more practicals....


5. Write syntax for nested for loop. Give any one example. OR Explain for loop and nested for loop with example. ***

A5: Syntax:

for (initialization_1; condition_1; increment / decrement;) {

    // code before the inner loop

    for (initialization_2; condition_2; increment / decrement;) {

        // code to be repeated (inner loop)

    }

    // code after the inner loop

}

 

Example:

for (int i = 1; i <= 5; i++)

    {

        for (int j = 1; j <= 10; j++)

        {

            printf("%d * %d = %d\n", i, j, i * j);

        }

        printf("\n");

    }

 

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


Please follow me for more practicals....


6. Explain difference between entry control loop and exit control loop with suitable example.

OR Explain exit control loop with syntax and flowchart.

In entry control loops, the condition is checked before entering the loop.

In exit control loops, the condition is checked after executing the loop body.

 

Entry Control Loops:

for (int i = 0; i < 5; i++) {

        printf("%d\n", i);

    }

int j = 0;

while (j < 5) {

    printf("%d\n", j);

    j++;

}

 

Exit Control Loops:

    do {

        printf("%d\n", i);

        i++;

    } while (i < 5);

 

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


7. Explain difference between while and do while loop.

A7: In a while loop, if the initial condition is false, the loop block will not be executed at all.

In a do-while loop, the loop block is executed at least once, regardless of the initial condition.

 

8. The For loop Continues to execute as long as the condition is True.

9. Which is not a loop structure?

(I) Repeat until (II)for (III) do while (IV) while

 

10. Why loop control statements are used in C?

A10: Loop control statements in C, such as for, while, and do-while, are used to control the flow of execution in a program by repeatedly executing a block of code based on a specified condition.

 

11. WAP to print 1 to n and n to 1 numbers.

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int n;

 

    printf("Enter n = ");

    scanf("%d", &n);

 

    for (int i = 1; i <= n; i++)

    {    printf("%d\t", i);}

    printf("\n");

    for (int i = n; i >= 1; i--)

    {   printf("%d\t", i);}

 

    getch();

}

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


12. WAP to print to print sum of 1 to n numbers.

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int n, sum = 0;

 

    printf("Enter n = ");

    scanf("%d", &n);

 

    for (int i = 1; i <= n; i++)

    {        sum = sum + i;}

    printf("\nSum = %d", sum);

 

    getch();

}

 

13. WAP to print following series using loop 1 3 5 7 9 …..55.*

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

   

    for (int i = 1; i <= 55; i++)

    {   if (i % 2 != 0)

            printf("%d\t", i);}

 

    getch();

}

 

14. WAP to print the Fibonacci series up to given number.**

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int num, a = 1, b = 0, sum = 0;

 

    printf("Enter the num = ");

    scanf("%d", &num);

 

    for (int i = 1; i <= num; i++)

    {

        sum = a + b;

        printf("%d\t", sum);

        a = b;  b = sum;

    }

 

    getch();

}

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


15. WAP to print sum of digits of a given number

(i.e. 123 = 1 + 2 + 3 = 6 )*

 

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int num, r, sum = 0;

 

    printf("Enter the num = ");

    scanf("%d", &num);

 

    while (num > 0)

    {

        r = num % 10;

        sum = sum + r;

        num = num / 10;

    }

 

    printf("\nSum of digits = %d", sum);

 

    getch();

}

 

16. WAP to print even number from 1 to 100.*

#include <stdio.h>

#include <conio.h>

 

void main()

{

    for (int i = 1; i <= 100; i++)

    {

        if (i % 2 == 0)

            printf("%d\t", i);

    }

 

    getch();

}

 

17. WAP to print prime number from 1 to 100.**

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int i, j;

    for (i = 1; i <= 100; i++) {

        for (j = 2; j < i; j++) {

            if (i % j == 0)

                break;

        }

        if (i == j)

            printf("%d\t", i);

    }

 

    getch();

}

 

 

18. WAP to find given number is prime or not.**

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int num, i;

 

    printf("Enter num = ");

    scanf("%d", &num);

 

    for (i = 2; i < num; i++)

    {

        if (num % i == 0)

            break;

    }

    if (i == num)

        printf("%d is prime.", num);

    else

        printf("%d is not prime.", num);

 

    getch();

}

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


19. WAP to reverse the given number (i.e. 123 = 321 ).***

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int num, r;

 

    printf("Enter the num = ");

    scanf("%d", &num);

 

    while (num > 0) {

        r = num % 10;

        printf("%d", r);

        num = num / 10;

    }

 

    getch();

}

 

 

20. WAP to read two integer numbers and the arithmetic operation performed between two numbers as character. Calculate and print the result as for e.g. 40,50 * given as input the output should be 40*50=200.*

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int num1, num2;

    char operation;

 

    printf("Enter the num1 = ");

    scanf("%d", &num1);

    printf("Enter the num2 = ");

    scanf("%d", &num2);

    printf("Enter the Operation = ");

    scanf(" %c", &operation);

 

    switch (operation)

    {

    case '+':

        printf("%d + %d = %d\n", num1, num2, num1 + num2);

        break;

    case '-':

        printf("%d - %d = %d\n", num1, num2, num1 - num2);

        break;

    case '*':

        printf("%d * %d = %d\n", num1, num2, num1 * num2);

        break;

    case '/':

        printf("%d / %d = %.2f\n", num1, num2, (float)num1 / num2);

        break;

    default:

        printf("Invalid operation");

    }

    getch();

}

 

 

21. WAP to find given number is Armstrong or not.*****

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int num, r, temp, sum = 0;

 

    printf("Enter the num = ");

    scanf("%d", &num);

 

    temp = num;

 

    while (num > 0)

    {

        r = num % 10;

        sum = sum + r * r * r;

        num = num / 10;

    }

 

    if (temp == sum)

        printf("\n%d is armstrong.", temp);

    else

        printf("\n%d is not armstrong.", temp);

 

    getch();

}

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


22. WAP to print given pattern





Pattern (A):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    for (int i = 1; i <= 4; i++) {

        for (int j = 1; j <= i; j++) {

            printf("%d ", i);

        }

        printf("\n");

    }

    getch();

}

 

Pattern (B):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    for (int i = 1; i <= 4; i++) {

        for (int j = 1; j <= i; j++) {

            printf("%d ", j);

        }

        printf("\n");

    }

    getch();

}

 

Pattern (c):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int k = 1;

    for (int i = 1; i <= 4; i++) {

        for (int j = 1; j <= i; j++) {

            printf("%d ", k);

            k++;

        }

        printf("\n");

    }

    getch();

}

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


Pattern (D):

#include <stdio.h>

#include <conio.h>

 

void main()

{

                clrscr();

    for (int i = 1; i <= 4; i++) {

        for (int j = 1; j <= i; j++) {

            printf("*");

        }

        printf("\n");

    }

    getch();

}

 

Pattern (E):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int k = 1;

    for (int i = 1; i <= 4; i++) {

        for (int j = 1; j <= i; j++) {

            printf("%c ", 64 + j);

            k++;

        }

        printf("\n");

    }

    getch();

}

 

 

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


Q23:

 

 


Pattern (A):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    for (int i = 1; i <= 5; i++) {

        for (int j = 5; j >= i; j--) {

            printf(" ");

        }

        for (int j = 1; j <= i; j++) {

            printf(" %d", j);

        }

        printf("\n");

    }

    getch();

}

 

Pattern (B):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    for (int i = 1; i <= 5; i++) {

        for (int j = 5; j >= i; j--) {

            printf(" ");

        }

        for (int j = 1; j <= i; j++) {

            printf(" *");

        }

        printf("\n");

    }

    getch();

}

 

  

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


Pattern (C):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int k = 1;

    for (int i = 1; i <= 5; i++) {

        for (int j = 5; j >= i; j--) {

            printf(" ");

        }

        for (int j = 1; j <= i; j++) {

            printf(" %d", k);

            k++;

        }

        printf("\n");

    }

    getch();

}

 

 

Pattern (D):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    for (int i = 5; i >= 1; i--) {

        for (int j = 1; j <= i; j++) {

            printf("* ");

        }

        printf("\n");

    }

    getch();

}

  

-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


Program (E):

#include <stdio.h>

#include <conio.h>

 

void main()

{

    for (int i = 1; i <= 5; i++) {

        for (int j = 1; j <= i; j++) {

            (j % 2 == 0) ? printf("0") : printf("1");

        }

        printf("\n");

    }

    getch();

}

  

Please follow me for more practicals....


-- Please Subscribe, Like, & Share --
-- Following Youtube Channel --

BINDAS - MAN


 


Please follow me for more practicals....


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.