So I have some code that generates a set of random numbers. Afterwards, it asks the user if they would like to produce another set of random numbers. The problem I'm having is that I want the user to only input y for yes or n for no, and if they don't input those to options, tell them that is an invalid input and to try again. I cannot figure out how to do this. Here is the code:
// C program for generating a
// random number in a given range.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size:\n");
scanf("%d", &lower);
printf("\nMaximum number size:\n");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated:\n");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
scanf(" %c",&c);
printf("\n");
if (c=='n'){
exit(0);
}
do {
printf("Please enter valid input!\n");
scanf(" %c", &c);
if (c=='n'){
exit(0);
}
} while (c!='y'||c!='n');
} while(c=='y');
return 0;
}
Any help would be appreciated.
This answers your question, but on a side note, I usually like putting the conditional in the first question so if the user enters zero then the app will quit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size: ");
scanf("%d", &lower);
printf("\nMaximum number size: ");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated: ");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
do {
printf("\t\t==> ");
scanf(" %c", &c);
c &= 0x5f; // convert to uppercase
if (c=='N') exit(0);
} while (c != 'Y');
} while (1);
return 0;
}
Related
If I input this number: 1234, I want the output to begin with 1 2 3 4 not 4 3 2 1. How do I do this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers, count=0, num;
printf("\nEnter numbers: ");
scanf("%d", &numbers);
while(numbers>0)
{
num = numbers%10;
numbers = numbers/10;
printf("%d", num);
}
printf("The total number of digits is: %d\n", num);
return 0;
}
One of the solutions is to use recursion.
#include <stdio.h>
#include <stdlib.h>
// prints reverse order and returns count of digits
int printrev(int numbers)
{
if (numbers <= 0)
return 1;
int num = numbers % 10;
int count = printrev(numbers / 10);
printf("%d", num);
return count + 1;
}
int main()
{
int numbers, count = 0, num;
printf("\nEnter numbers: ");
scanf("%d", &numbers);
count = printrev(numbers);
printf("\nThe total number of digits is: %d\n", count);
return 0;
}
This topic was already answered in this stackoverflow question. Also, an easier way is to create an array where you can save the digits inside of while loop and then print it backwards. But in this case it would need to declare an array a priori.
How about to display character by character in string order?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers = 0;
printf("Enter numbers: ");
scanf("%d", &numbers);
char szNumber[16] = "";
snprintf(szNumber, sizeof(szNumber), "%d", numbers);
int i = 0;
while (szNumber[i] != '\0')
{
printf("%c ", szNumber[i]);
++i;
}
return 0;
}
I need some help displaying a float array that's partially filled. Not sure what I'm doing wrong, but here's what I have.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#define SIZE 20
int main()
{
float score[SIZE];
float GPA, sum, avg;
int count, ctr, num_of_entries;
sum = 0;
for (count = 0; count < SIZE; count++)
{
printf("Enter a GPA. -1 to stop the data entry: ");
scanf("%f", &GPA);
if (GPA == -1)
break;
score[count] = GPA;
}
printf("Number of GPAs entered = %d", count);
num_of_entries = count;
printf("\n\nContent of the Array:\n=========================\n");
for (ctr = 0; ctr <= num_of_entries; ctr++);
{
printf("%.2f \n", score[ctr]);
}
_getch();
return 0;
}
I'm trying to display the entered GPA values as float variables with 2 decimal places. The printed result is a very large negative number. Any help would be greatly appreciated.
This is the full code, the dig variables are user inputs
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int dig1=0;
int dig2=0;
int dig3=0;
num=dig1,dig2,dig3;
fflush(stdin);
printf("Please enter the first digit of your three digit number:");
scanf("%d", &dig1);
fflush(stdin);
printf("Please enter the second digit of your three digit number:");
scanf("%d", &dig2);
fflush(stdin);
printf("Please enter the third digit of your three digit number:");
scanf("%d", &dig3);
if (num==(dig1*dig1*dig1)+(dig2*dig2*dig2)+(dig3*dig3*dig3))
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
How could I make the variable "num" equal to all of the inputs for "dig1", "dig2", and "dig3". As in if dig 1 was 2 and dig 2 was 4 and dig 3 was 6, num would be 246. Please help!
Your problem can be solved this way:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num;
int dig[3];
int i,j = 100;
int result = 0;
for(i = 0;i<3;i++){
fflush(stdin);
printf("Please enter the %d digit of your three digit number:",i+1);
scanf("%d", &dig[i]);
}
for(i = 0;i<3;i++){
result += pow(dig[i],3);
num += dig[i] * j;
j/=10;
printf("%d * %d = %d\n",dig[i],j,num);
}
if (num == result)
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
You could multiply these numbers according to their significant value:
int num = dig1 + dig2 * 10 + dig3 * 100;
I'm new to coding. I'm now learning C programing. I'm having a problem with counting digits. My objective is to only accept a fixed number of digits in the input. For example I want user to input only 7 digits number, so when they input anything else than a 7 digits number, the program should ask them to input again until it gets 7 digits number. Here is my attempt:
int n, count = 0;
printf("Please enter number:");
scanf("%d", &n);
printf("\n");
while (n != 0)
{
n /= 10;
count++;
}
printf("%d", count);
if (count != 7)
{
printf("You can use only 7 digits numbers");
}
You can try this one:
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000]; //Enter the maximum number of digits you expect as array size
printf("Please enter number: ");
scanf("%s", s);
while(strlen(s)!=7)
{
printf("%d\n", strlen(s));
printf("You can use only 7 digits numbers\n");
scanf("%s", s);
}
return 0;
}
After taking the input, you can easily convert it to int if you need to.
You can try this by creating a small function to check the number of digits in the number entered for you:
#include <stdio.h>
int count_digit(int n){
int count = 0;
if(n == 0) return 1;
while(n != 0)
{
n /= 10;
++count;
}
return count;
}
int main(void) {
int num;
printf("Please enter number:");
scanf("%d", &num);
while (count_digit(num) != 7){
printf("Please enter only 7digits number:\n ");
scanf("%d", &num);
}
printf("The 7digit number you entered is: %d",num);
return 0;
}
Here is the average program I created.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float f1,f2,f3;
/* Program to calculate averages. */
/*Asks for the numbers.*/
printf(" Please enter three numbers.\n");
printf ("\t" "First number please.\n");
scanf("%f", &f1);
printf ("\t" "Second number please.\n");
scanf ("%f", &f2);
printf("\t" "Third number please.\n");
scanf("%f", &f3);
/* Now it averages it.*/
printf(" Thank you, wait one.\n");
printf(" Excellent, your sum is.\n");
printf("%f""\n", f1+f2+f3);
printf("Your average of the sum is now!!!!\n");
printf("%f", (f1+f2+f3)/3);
return 0;
}
Now would I turn this into a do-while? Or an if else?
If you want to repeat the whole entry and averaging process, you can wrap a loop around the code:
#include <stdio.h>
int main(void)
{
float f1,f2,f3;
while (1)
{
printf("Please enter three numbers.\n");
printf("\tFirst number please.\n");
if (scanf("%f", &f1) != 1)
break;
printf("\tSecond number please.\n");
if (scanf("%f", &f2) != 1)
break;
printf("\tThird number please.\n");
if (scanf("%f", &f3) != 1)
break;
printf("Your sum is %f\n", f1+f2+f3);
printf("Your average is %f\n", (f1+f2+f3)/3);
}
return 0;
}
Note that this code checks the return value from scanf() each time it is used, breaking the loop if there's a problem. There's no need for string concatenation, and a single printf() can certainly print a string and a value.
That's a simple first stage; there are more elaborate techniques that could be used. For example, you could create a function to prompt for and read the number:
#include <stdio.h>
static int prompt_and_read(const char *prompt, float *value)
{
printf("%s", prompt);
if (scanf("%f", value) != 1)
return -1;
return 0;
}
int main(void)
{
float f1,f2,f3;
while (printf("Please enter three numbers.\n") > 0 &&
prompt_and_read("\tFirst number please.\n", &f1) == 0 &&
prompt_and_read("\tSecond number please.\n", &f2) == 0 &&
prompt_and_read("\tThird number please.\n", &f3) == 0)
{
printf("Your sum is %f\n", f1+f2+f3);
printf("Your average is %f\n", (f1+f2+f3)/3);
}
return 0;
}
If you want to get away from a fixed set of three values, then you can iterate until you encounter EOF or an error:
#include <stdio.h>
static int prompt_and_read(const char *prompt, float *value)
{
printf("%s", prompt);
if (scanf("%f", value) != 1)
return -1;
return 0;
}
int main(void)
{
float value;
float sum = 0.0;
int num = 0;
printf("Please enter numbers.\n");
while (prompt_and_read("\tNext number please.\n", &value) == 0)
{
sum += value;
num++;
}
if (num > 0)
{
printf("You entered %d numbers\n", num);
printf("Your sum is %f\n", sum);
printf("Your average is %f\n", sum / num);
}
return 0;
}
You might also decide to replace the newline at the ends of the prompt strings with a space so that the value is typed on the same line as the prompt.
If you want to check whether to repeat the calculation, you can use a minor variant on the first or second versions of the code:
#include <stdio.h>
static int prompt_and_read(const char *prompt, float *value)
{
printf("%s", prompt);
if (scanf("%f", value) != 1)
return -1;
return 0;
}
static int prompt_continue(const char *prompt)
{
printf("%s", prompt);
char answer[2];
if (scanf("%1s", answer) != 1)
return 0;
if (answer[0] == 'y' || answer[0] == 'Y')
{
int c;
while ((c = getchar()) != EOF && c != '\n') // Gobble to newline
;
return 1;
}
return 0;
}
int main(void)
{
float f1,f2,f3;
while (printf("Please enter three numbers.\n") > 0 &&
prompt_and_read("\tFirst number please.\n", &f1) == 0 &&
prompt_and_read("\tSecond number please.\n", &f2) == 0 &&
prompt_and_read("\tThird number please.\n", &f3) == 0)
{
printf("Your sum is %f\n", f1+f2+f3);
printf("Your average is %f\n", (f1+f2+f3)/3);
if (prompt_continue("Do you want to try again?") == 0)
break;
}
return 0;
}
You can do this:
int main()
{
float number, sum=0.0f;
int index=0;
do
{
printf ("\t" "Enter number please.\n"); //Asking for a number from user
scanf("%f", &number); //Getting a number from a user
sum+=number; //Add number entered to the sum
i++;
} while (i < 3);
printf("Excellent, your average is %f\n", sum/3);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
float f1,f2,f3;
char c='Y';
/* Program to calculate averages. */
/*Asks for the numbers.*/
do
{
printf(" Please enter three numbers.\n");
printf ("\t" "First number please.\n");
scanf("%f", &f1);
printf ("\t" "Second number please.\n");
scanf ("%f", &f2);
printf("\t" "Third number please.\n");
scanf("%f", &f3);
/* Now it averages it.*/
printf(" Thank you, wait one.\n");
printf(" Excellent, your sum is.\n");
printf("%f""\n", f1+f2+f3);
printf("Your average of the sum is now!!!!\n");
printf("%f", (f1+f2+f3)/3);
printf ("Do you wana continue [Y/N]...\n");
scanf("%c", &c);
}while(c!='N'&&c!='n');
return 0;
}