You are on page 1of 10

Department Of Electrical Engineering

CS-114 Fundamentals Of Programming

LAB 9: While & For Loop

TASK 1:
Write a program that plays an incredibly stupid number-guessing game.
The user will try to guess the secret number until they get it right. That means it will
keep looping as long as the guess is different from the secret number. You must store
the secret number in a variable, and use that variable throughout. The secret number
itself must not appear in the program at all, except in the one line where you store it into
a variable.

Code:
Case 1: The number is randomly chosen by the system
//this program plays an incredibly "stupid" number guessing game

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()

int num;
int guess = 0;
srand(time(0));
num = rand() % 10 + 1;

printf("I have chosen a number between 1 and 10. Try to guess it.\n");

while (num!=guess) { //this loop will continue until the user guesses the
correct number

printf("Your guess: ");


scanf_s("%d", &guess);

1|Page
CS114-Fundamentals Of Programming
if (num != guess) {

printf("That is incorrect. Guess again.\n");


}
}

if (num == guess) {

printf("That's right! You guessed it.");


}
return 0;

Output:
1.

2.

2|Page
CS114-Fundamentals Of Programming
Case 2: For a fixed number (i.e. 6)
Code:
//this program plays a number-guessing game

#include<stdio.h>

int main()

{
int guess = 0;

printf("I have chosen a number between 1 and 10. Try to guess it.\n"); //prompt

while (guess != 6) { //this loop will continue until the user enters the number 6

printf("Your guess: ");

scanf_s("%d", &guess);

if (guess != 6) {
printf("That is incorrect. Guess again.\n");

}
}
if (guess == 6) {

printf("That's right! You guessed it.\n");

return 0;

3|Page
CS114-Fundamentals Of Programming
Output:

TASK 2:
The greatest common divisor (GCD) of two integers is the largest integer
that evenly divides each of the two numbers. Write function gcd that returns the greatest
common divisor of two integers. Use the gcd function in your program to determine the
GCD of the numbers.

Code:
//program to find the GCD of two integers

#include <stdio.h>

int main(void) {

int num1, num2, r;


int x;
int y;

printf("Enter two integers: ");


scanf_s("%d%d", &num1, &num2);

x = num1;
y = num2;

while (num2 != 0) { //based on Euclid's algorithm

r = num1 % num2;

num1 = num2;

4|Page
CS114-Fundamentals Of Programming
num2 = r;
}

printf("The greatest common divisor of %d and %d is %d\n", x, y, num1); //prints


GCD

return 0;
}

Output:

TASK 3:
An integer is said to be prime if it is divisible only by 1 and itself. For
example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.
a) Write a function that determines if a number is prime.
Code:
//program to check whether a number is prime or not

#include<stdio.h>

5|Page
CS114-Fundamentals Of Programming
int main() {

int i = 1;
int prime = 0;
int n;
int remainder;

printf("Enter the number:");


scanf_s("%d", &n);

while (i <= n) { //loop to check whether a number is prime or not


remainder = n % i;
i++;

if (remainder == 0)
prime++;
}
if (prime == 2)

printf("This is a prime number.\n");


else
printf("This is not a prime number.\n");

return 0;
}

Output:
Case 1: Number is prime

Case 2: Number is not prime

6|Page
CS114-Fundamentals Of Programming
b) Use this function in a program that determines and prints all the prime
numbers between 1 and 10,000.

Code:
//program to display prime numbers between 1 and 10000
#include<stdio.h>

int main() {

int num = 1;
int remainder;

printf("The prime numbers from 1 t0 10,000 are:\n");

while (num <= 10000) { //while loop to the display the numbers

int prime = 0;

int i = 1;

while (i <= num) { //while loop to see whether the umber is prime or
not

7|Page
CS114-Fundamentals Of Programming
remainder = num % i;

i++;

if (remainder == 0)

prime++;
}
if (prime == 2)

printf("%d\t", num); //prints prime numbers


num++;
}

return 0;
}

Output:

TASK 4:
Write a program in C to display the n terms of even natural number and
their sum. (Use for loop)

Code:
//program that asks the user to input the number of terms and then calculates the sum of
even numbers

#include<stdio.h>

int main()

8|Page
CS114-Fundamentals Of Programming
{
int x;

int evenNum = 2;

int sum=0;

printf("Input number of terms: "); //prompt

scanf_s("%d", &x);

printf("The even numbers : ");

for (evenNum=2; evenNum<=x; evenNum+=2) {

printf("%d ", evenNum); //prints even numbers

sum = sum + evenNum;


}

printf("\nThe sum of even numbers upto %d terms: %d", x, sum); //prints sum
return 0;

Output:

9|Page
CS114-Fundamentals Of Programming
10 | P a g e
CS114-Fundamentals Of Programming

You might also like