0% found this document useful (0 votes)
37 views10 pages

Looping Programs

Cse

Uploaded by

Atharva Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views10 pages

Looping Programs

Cse

Uploaded by

Atharva Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

int main() {

int i = 0; // Initialize the loop counter

// First while loop: Print numbers from 0 to 10

printf("Numbers from 0 to 10:\n");

while (i <= 10) {

printf("%d ", i); // Print the current value of i

i++; // Increment the loop counter

printf("\n"); // Move to the next line for better formatting

i = 10; // Reset the loop counter to 10

// Second while loop: Print numbers from 10 to 0

printf("\nNumbers from 10 to 0:\n");

while (i >= 0) {

printf("%d ", i); // Print the current value of i

i--; // Decrement the loop counter

return 0; // Indicate successful program execution

Copy

Sample Output:

Numbers from 0 to 10:

0 1 2 3 4 5 6 7 8 9 10
Numbers from 10 to 0:

10 9 8 7 6 5 4 3 2 1 0

#include <stdio.h>

int main() {

int number; // Variable to store the user input

int sum = 0; // Variable to store the sum of positive integers

printf("Input integers (enter 0 to stop):\n");

// Start a while loop to continue until the user enters 0

while (1) {

printf("Input a number: ");

scanf("%d", &number); // Read the user input

if (number == 0) {

break; // Exit the loop if the user enters 0

if (number > 0) {

sum += number; // Add positive numbers to the sum

// Print the sum of positive integers

printf("Sum of positive integers: %d\n", sum);

return 0; // Indicate successful program execution

}
Sample Output:

Input integers (enter 0 to stop):

Input a number: 5

Input a number: 9

Input a number: -3

Input a number: 2

Input a number: 5

Input a number: 6

Input a number: -20

Input a number: 0

Sum of positive integers: 27

#include <stdio.h>

int main() {

int number = 1; // Initialize the variable to store the current number

int product = 1; // Initialize the variable to store the product

// Start a while loop to iterate from 1 to 5

while (number <= 5) {

// Update the product by multiplying it with the current number

product *= number;

// Print the current number and product in each iteration

printf("Current number: %d, Current product: %d\n", number, product);

// Increment the number for the next iteration

number++;

}
return 0; // Indicate successful program execution

Copy

Sample Output:

Current number: 1, Current product: 1

Current number: 2, Current product: 2

Current number: 3, Current product: 6

Current number: 4, Current product: 24

Current number: 5, Current product: 120

#include <stdio.h>

int main() {

int userInput; // Variable to store user input

int previousNumbers[100]; // Array to store previous entered numbers

int hasDuplicate = 0; // Flag to track if a duplicate is found

int index = 0; // Index to keep track of the current position in the array

printf("Input numbers (stop if you input a duplicate):\n");

// Start a while loop to continue until a duplicate is entered

while (!hasDuplicate) {

printf("Input a number: ");

int readStatus = scanf("%d", &userInput); // Read the user input

// Check if the input is a valid integer

if (readStatus != 1) {

printf("Invalid input. Please enter a valid integer.\n");

// Clear the input buffer

while (getchar() != '\n');


continue;

// Check if the current input is equal to any of the previous numbers

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

if (userInput == previousNumbers[i]) {

hasDuplicate = 1; // Set the flag to indicate a duplicate is found

printf("Duplicate number entered. Program will stop.\n");

break; // Exit the loop when a duplicate is found

previousNumbers[index++] = userInput; // Store the current input in the array

return 0; // Indicate successful program execution

Sample Output:

Input numbers (stop if you input a duplicate):

Input a number: 2

Input a number: 3

Input a number: 1

Input a number: 4

Input a number: 5

Input a number: 3

Duplicate number entered. Program will stop.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>
int main() {

srand(time(NULL)); // Seed the random number generator with the current time

int targetNumber = rand() % 20 + 1; // Generate a random number between 1 and 20

int userGuess; // Variable to store the user's guess

int attempts = 0; // Variable to count the number of attempts

printf("Guess the number between 1 and 20:\n");

// Start a while loop to give the user multiple chances

while (1) {

printf("Input your guess: ");

scanf("%d", &userGuess); // Read the user's guess

attempts++; // Increment the number of attempts

// Check if the guess is correct

if (userGuess == targetNumber) {

printf("Congratulations! You guessed the correct number in %d attempts.\n", attempts);

break; // Exit the loop if the guess is correct

} else {

printf("Incorrect guess. Try again!\n");

return 0; // Indicate successful program execution

Sample Output:

Guess the number between 1 and 20:


Input your guess: 20

Incorrect guess. Try again!

Input your guess: 11

Incorrect guess. Try again!

Input your guess: 12

Incorrect guess. Try again!

Input your guess: 13

Incorrect guess. Try again!

Input your guess: 17

Incorrect guess. Try again!

Input your guess: 18

Incorrect guess. Try again!

Input your guess: 19

Incorrect guess. Try again!

Input your guess: 14

Incorrect guess. Try again!

Input your guess: 1

Incorrect guess. Try again!

Input your guess: 2

Incorrect guess. Try again!

Input your guess: 5

Incorrect guess. Try again!

Input your guess: 3

Incorrect guess. Try again!

Input your guess: 7

Incorrect guess. Try again!

Input your guess: 8

Incorrect guess. Try again!

Input your guess: 6

Incorrect guess. Try again!

Input your guess: 9


Incorrect guess. Try again!

Input your guess: 10

Incorrect guess. Try again!

Input your guess: 11

Incorrect guess. Try again!

Input your guess: 12

Incorrect guess. Try again!

Input your guess: 13

Incorrect guess. Try again!

Input your guess: 14

Incorrect guess. Try again!

Input your guess: 15

Incorrect guess. Try again!

Input your guess: 16

Congratulations! You guessed the correct number in 23 attempts.

#include <stdio.h>

int main() {

int number; // Variable to store the user input

long long factorial = 1; // Variable to store the factorial, initialized to 1

// Prompt the user to enter a positive integer

printf("Input a positive integer: ");

scanf("%d", &number); // Read the user input

// Check if the entered number is negative

if (number < 0) {

printf("Error: Input enter a non-negative integer.\n");

return 1; // Exit the program with an error code

}
// Calculate the factorial using a while loop

while (number > 0) {

factorial *= number; // Multiply the current factorial by the current number

number--; // Decrement the number for the next iteration

// Print the calculated factorial

printf("Factorial: %lld\n", factorial);

return 0; // Indicate successful program execution

Sample Output:

Input a positive integer: 5

Factorial: 120

Input a positive integer: -6

Error: Input enter a non-negative integer.

#include <stdio.h>

#include <string.h>

int main() {

char username[20]; // Array to store the user input for the username

// Start a while loop to keep asking for a valid username

while (1) {

// Prompt the user to input a username

printf("Input a username (at least 8 characters): ");

scanf("%s", username); // Read the user input and store it in the array

// Check if the entered username is at least 8 characters long


if (strlen(username) >= 8) {

break; // Exit the loop if the username is valid

} else {

// Print an error message if the username is too short

printf("Error: Username must be at least 8 characters long.\n");

// Clear the input buffer to handle any remaining characters

while (getchar() != '\n');

// Print a message indicating a valid username was entered

printf("Valid username entered: %s\n", username);

return 0; // Indicate successful program execution

Sample Output:

Input a username (at least 8 characters): Sdk34%k

Error: Username must be at least 8 characters long.

Input a username (at least 8 characters): Akdsui234#$

Valid username entered: Akdsui234#$

You might also like