Professional Documents
Culture Documents
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
1|Page
CS114-Fundamentals Of Programming
if (num != guess) {
if (num == guess) {
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
scanf_s("%d", &guess);
if (guess != 6) {
printf("That is incorrect. Guess again.\n");
}
}
if (guess == 6) {
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) {
x = num1;
y = num2;
r = num1 % num2;
num1 = num2;
4|Page
CS114-Fundamentals Of Programming
num2 = r;
}
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;
if (remainder == 0)
prime++;
}
if (prime == 2)
return 0;
}
Output:
Case 1: Number is 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;
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)
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;
scanf_s("%d", &x);
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