Loop

  1. Write a program for display your name 50 times in C language.
    #include <stdio.h>
    int main(){
    for(int i=0; i<50; i++;){
    printf(“Suman Adhikari”);
    }

    return 0;}




  2.  "Write a program that uses a “for” loop to compute and prints the sum of a given numbers of squares."
    #include <stdio.h>

    int main() {
        int n, sum = 0;

        // Prompt the user for input
        printf("Enter the number of terms: ");
        scanf("%d", &n);

        // Compute the sum of squares using a for loop
        for(int i = 1; i <= n; i++) {
            sum += i * i;
        }

        // Print the result
        printf("The sum of the squares of the first %d natural numbers is: %d\n", n, sum);

        return 0;
    }




  3. "Write a program that uses a “while” loop to compute and prints the sum of a given numbers of squares. For example, if 4 is input, then the program will print 30, which is equal to 12+22+32+42.
    #include <stdio.h>

    int main() {
        int n, sum = 0, i = 1;

        // Input the number of squares
        printf("Enter the number of squares: ");
        scanf("%d", &n);

        // Compute the sum of squares using a while loop
        while (i <= n) {
            sum += i * i;
            i++;
        }

        // Print the result
        printf("Sum of squares: %d\n", sum);

        return 0;
    }




  4. Write a program that uses a “while” loop to compute and prints the sum of a given numbers of squares.For example, if 4 is input, then the program will print 30,which is equal to 12+22+32+42."
    #include <stdio.h>

    int main() {
        int n, sum = 0, i = 1;

        // Input the number
        printf("Enter the number of squares: ");
        scanf("%d", &n);

        // Compute the sum of squares using a while loop
        while (i <= n) {
            sum += i * i;
            i++;
        }

        // Print the result
        printf("Sum of squares: %d\n", sum);

        return 0;
    }





  5. Write a program to input string and display 20 times.
    #include <stdio.h>

    int main() {
        char str[100];
        int i;

        // Input the string
        printf("Enter a string: ");
        fgets(str, sizeof(str), stdin);

        // Display the string 20 times
        for (i = 0; i < 20; i++) {
            printf("%s", str);
        }

        return 0;
    }






0 Comments had been done yet:

Post a Comment