thanks Write a function that takes a positive integer as inp
thanks
Write a function that takes a positive integer as input and returns the leading digit in its decimal representation. For example, the leading digit of 234567 is 2. Write a program that picks a random number between 1 and 100 inclusive. It then prompts the user to guess the number. If the user guesses correctly, print out a message of congratulations and exit. Otherwise, tell the user whether their guess was low or high and ask them to guess again. Repeat the process. If the user takes more than 7 guesses, tell them they need to think harder and exit. You should use library functions stand and rand to seed and generate a random number. The time function provides a good starting point for stand.Solution
#include <stdio.h>
int main(void)
{
int num;
printf(\"Enter any number: \");
scanf(\"%d\", &num);
while(num>=10) // reduce the number by dividing it by 10 until number <10
{
num = num / 10;
}
printf(\"\ First digit = %d\", num);
return 0;
}
output:
2.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand( (unsigned) time(NULL) );
int secretNumber,X,guess;
secretNumber = 1 + rand() % 100;
int games =0;
char option =\'Y\';
while(option == \'Y\' || option == \'y\')
{
for(X=0;X<7;X++)
{
printf(\"\ Guess the secret number\");
scanf(\"%d\",&guess);
if(guess == secretNumber)
{
printf(\"\ Congratulations!!!! You win!\");
break;
}
else if(secretNumber - guess >0)
printf(\"\ Sorry, your guess is too low.\");
else if(guess - secretNumber >0)
printf(\"\ Sorry, your guess is too high.\");
}
printf(\"\ Sorry, you lose. Think harder. The secret number was %d\",secretNumber);
games++;
printf(\"\ Do you want to play again? (y/n)\");
scanf(\"%c\",&option);
if(option == \'y\' || option == \'Y\')
{
break;
exit(0);
}
else
secretNumber = 1 + rand() % 100;
}
printf(\"\ Thank you, and Come Again\");
return 0;
}
output:

