Try to guess the number between 1 and 100!
I have picked a number between 1 and 100.
// Number Guessing Game in C
#include
#include
#include
int main() {
srand(time(NULL));
int r = rand() % 10 + 1;
int a,i = 0;
while (i < 10) {
printf("Guess the number between 1 and 10 :\n");
scanf("%d", &a);
if (a == r) {
printf("Correct! You guessed it in %d tries.\n", i + 1);
break;
} else if (a < r) {
printf("Too low.\n");
} else {
printf("Too high.\n");
}
i++;
}
if (i == 10) {
printf("Sorry, you've used all your tries. The correct number was %d.\n", r);
}
return 0;
}
// Number Guessing Game in C++
#include
#include
#include
using namespace std;
int main() {
srand(time(0));
int r = rand() % 10 + 1;
int a;
int i = 0;
while (i < 10) {
i++;
cout << "Guess the number between 1 and 100 (You have 10 tries):" << endl;
cin >> a;
if (a == r) {
cout << "Correct! You guessed the number in " << (i) << " tries." << endl;
break;
} else if (a < r) {
cout << "Too low." << endl;
} else {
cout << "Too high." << endl;
}
}
if (i == 10 && a != r) {
cout << "Sorry, you've used all your tries. The correct number was " << r << "." << endl;
}
return 0;
}
// Number Guessing Game in Java
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random r = new Random();
int r1=r.nextInt(10) + 1;
int i=0;
Scanner scanner = new Scanner(System.in);
while(i<10){
i++;
System.err.println("Enter number between 1 and 10: ");
int no1=scanner.nextInt();
if(no1==r1){
System.out.printf("Correct! The number is %d. You guessed it in %d attempts.%n", r1, i);
break;
} else if (no1 > r1) {
System.out.println("Too high! Try a lower number.");
} else {
System.out.println("Too low! Try a higher number.");
}
}
scanner.close();
}
}
# Number Guessing Game in Python
import random
num = random.randint(0,100)
i=0
while i<10:
i+=1
guess = int(input('Enter your guess: '))
if guess == num:
print(f'Correct! The number is {num}. You guessed it in {i} attempts.')
break
elif guess > num:
print('Too high! Try a lower number.')
elif guess < num:
print('Too low! Try a higher number.')
// Number Guessing Game in JavaScript
let targetNumber = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guess = Number(document.getElementById("guessInput").value);
const feedback = document.getElementById("feedback");
attempts++;
if (guess < 1 || guess > 100) {
feedback.textContent = "Please enter a number between 1 and 100.";
} else if (guess > targetNumber) {
feedback.textContent = "Too high! Try again.";
} else if (guess < targetNumber) {
feedback.textContent = "Too low! Try again.";
} else {
feedback.textContent = 🎉 Correct! You guessed it in ${attempts} tries.;
}
}
function resetGame() {
targetNumber = Math.floor(Math.random() * 100) + 1;
attempts = 0;
document.getElementById("guessInput").value = '';
document.getElementById("feedback").textContent = '';
}
function toggleCode() {
const section = document.getElementById("codeSection");
section.style.display = section.style.display === "none" ? "block" : "none";
}
function showCode(lang) {
document.querySelectorAll("pre").forEach(pre => pre.style.display = "none");
document.getElementById(lang).style.display = "block";
}