C Programing-Questions On While Loop : Exercises, Practice, Solution.

WAP in C to print the series 1,2,3,4,5,…………………………n using while loop

int main() {
    int n, i;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print the series using a while loop
    i = 1;
    while (i <= n) {
        printf("%d ", i);
        i++;
    }

    return 0;
}

WAP in C to print the series n,n-1,n-2……………3,2,1 using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print the series using a while loop
    while (n >= 1) {
        printf("%d ", n);
        n--;
    }

    return 0;
}

WAP in C to print all the even numbers between 1 to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print even numbers using a while loop
    int i = 1;
    while (i <= n) {
        if (i % 2 == 0) {
            printf("%d ", i);
        }
        i++;
    }

    return 0;
}

WAP in C to print all the odd numbers between 1 to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print odd numbers using a while loop
    int i = 1;
    while (i <= n) {
        if (i % 2 != 0) {
            printf("%d ", i);
        }
        i++;
    }

    return 0;
}

WAP in C to print the following series 1,4,7,10,……………. to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print the series using a while loop
    int i = 1;
    while (i <= n) {
        printf("%d ", i);
        i += 3; // Increment by 3 to get the next term in the series
    }

    return 0;
}

WAP in C to print the following series 1,5, 9, 13, … up to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print the series using a while loop
    int i = 1;
    while (i <= n) {
        printf("%d ", i);
        i += 4; // Increment by 4 to get the next term in the series
    }

    return 0;
}

WAP in C to print the following series 2, 6, 10, 14, … up to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    // Print the series using a while loop
    int i = 2;
    while (i <= n) {
        printf("%d ", i);
        i += 4; // Increment by 4 to get the next term in the series
    }

    return 0;
}

WAP in C to find sum of all the even numbers between 1 to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    int i = 2; // Start from the first even number
    int sum = 0;

    // Use a while loop to iterate through even numbers up to 'n'
    while (i <= n) {
        sum += i;
        i += 2; // Increment by 2 to get the next even number
    }

    // Print the sum
    printf("Sum of even numbers between 1 and %d: %d\n", n, sum);

    return 0;
}

WAP in C to find sum of all the odd numbers between 1 to n using while loop

int main() {
    int n;

    // Get the value of 'n' from the user
    printf("Enter the value of n: ");
    scanf("%d", &n);

    int i = 1; // Start from the first odd number
    int sum = 0;

    // Use a while loop to iterate through odd numbers up to 'n'
    while (i <= n) {
        sum += i;
        i += 2; // Increment by 2 to get the next odd number
    }

    // Print the sum
    printf("Sum of odd numbers between 1 and %d: %d\n", n, sum);

    return 0;
}

WAP in C to print multilication table of any number using while loop

int main() {
    int number;

    // Get the number for the multiplication table from the user
    printf("Enter the number for the multiplication table: ");
    scanf("%d", &number);

    int i = 1;

    // Use a while loop to print the multiplication table
    while (i <= 10) {
        printf("%d x %d = %d\n", number, i, number * i);
        i++;
    }

    return 0;
}

WAP in C to input a number from user and count the number of digits in the number using while loop

int main() {
    int number;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Initialize the digit count to 0
    int digitCount = 0;

    // Use a while loop to count the number of digits
    while (number != 0) {
        number /= 10; // Remove the last digit
        digitCount++;
    }

    // Print the count of digits
    printf("Number of digits: %d\n", digitCount);

    return 0;
}

WAP in C to input a number from user and reverse the number using while loop

int main() {
    int number, reversedNumber = 0, remainder;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Use a while loop to reverse the number
    while (number != 0) {
        remainder = number % 10; // Get the last digit
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10; // Remove the last digit
    }

    // Print the reversed number
    printf("Reversed Number: %d\n", reversedNumber);

    return 0;
}

WAP in C to input a number from user and check wheather the number is Palindrome or not using while loop

int main() {
    int number, originalNumber, reversedNumber = 0, remainder;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    originalNumber = number; // Save the original number for comparison later

    // Use a while loop to reverse the number
    while (number != 0) {
        remainder = number % 10; // Get the last digit
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10; // Remove the last digit
    }

    // Check if the original number is equal to its reverse
    if (originalNumber == reversedNumber) {
        printf("The number is a palindrome.\n");
    } else {
        printf("The number is not a palindrome.\n");
    }

    return 0;
}

WAP in C to input a number from user and check wheather the number is Armstrong or not using while loop (An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number.)

int main() {
    int number, originalNumber, digitCount = 0, sum = 0, remainder;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    originalNumber = number; // Save the original number for comparison later

    // Count the number of digits in the number
    while (number != 0) {
        number /= 10;
        digitCount++;
    }

    number = originalNumber; // Restore the original number

    // Use a while loop to check if the number is Armstrong
    while (number != 0) {
        remainder = number % 10;
        sum += pow(remainder, digitCount);
        number /= 10;
    }

    // Check if the original number is equal to the sum of its digits raised to the power of digitCount
    if (originalNumber == sum) {
        printf("The number is an Armstrong number.\n");
    } else {
        printf("The number is not an Armstrong number.\n");
    }

    return 0;
}

WAP in C to input a number from user and check wheather the number is Prime number or not using while loop

int main() {
    int i, n = 2, a, count = 0;

    printf("\nEnter the number:");
    scanf("%d", &i);

    while (n < i) {
        if (i % n == 0) {
            count++;
        }
        n++;
    }

    if (count == 0) {
        printf("\nThis number is Prime");
    } else {
        printf("\nThis number is Not Prime");
    }

    return 0;
}

WAP in C to input a number from user and find the sum of first and last digit of the number using while loop.

int main() {
    int number, originalNumber, lastDigit, firstDigit, digitCount = 0;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    originalNumber = number; // Save the original number for later comparison

    // Find the last digit
    lastDigit = number % 10;

    // Count the number of digits in the number
    while (number != 0) {
        number /= 10;
        digitCount++;
    }

    number = originalNumber; // Restore the original number

    // Find the first digit
    firstDigit = number / (int)pow(10, digitCount - 1);

    // Calculate and print the sum of the first and last digits
    printf("Sum of the first and last digits: %d\n", firstDigit + lastDigit);

    return 0;
}

WAP in C to input a number from user and count the total even digits from the number using while loop

int main() {
    int number, originalNumber, digit, evenCount = 0;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    originalNumber = number; // Save the original number for later comparison

    // Use a while loop to iterate through each digit
    while (number != 0) {
        digit = number % 10; // Get the last digit

        if (digit % 2 == 0) {
            evenCount++;
        }

        number /= 10; // Remove the last digit
    }

    // Print the count of even digits
    printf("Total even digits in %d: %d\n", originalNumber, evenCount);

    return 0;
}

WAP in C to input a number from user and find all prime factors of the number using while loop.

int main() {
    int number, divisor = 2;

    // Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    printf("Prime factors of %d are: ", number);

    // Use a while loop to find prime factors
    while (number > 1) {
        while (number % divisor == 0) {
            printf("%d ", divisor);
            number /= divisor;
        }
        divisor++;
    }

    printf("\n");

    return 0;
}

On Key

Related Posts