I'm new to C programming. I was trying to write a program that accepts an integer from user and displays its multiplication table up to 10 multiples.
This is my program:
#include <stdio.h>
int main ()
{
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf(" %i ", &number);
while (count <=10)
{
sum = number * count;
printf("%i x %i = %i\n", number, count, sum);
count += 1;
}
return 0;
}
Compilation successfully completes, but when I execute the output file, nothing happens, the terminal is stuck at nothing, i've to press ctrl+c to get out..
This is due to the spaces used in your scanf command.
If you replace that with
scanf("%i", &number);
you get an instant response.
With your scanf format " %i ", the scanf function will read (and skip) possible leading spaces because of your leading space in the format.
Then it will read the integer.
Then, due to the trailing space, it will read and discard space until it find a non-space input.
Since there's no non-space input afterward, then scanf will block until you give some non-space input.
Solve simply by not having any spaces in the format. Or by entering some extra dummy input (followed by Enter).
The problem resides with the scanf.
Just replace
scanf(" %i ", &number);
with:
scanf("%i", &number);
and it will work.
#include <stdio.h>
int main (){
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf("%d", &number);
while (count <=10){
sum = number * count;
printf("%d * %d = %d\n", number, count, sum);
count += 1;
}
return 0;
}
Note: You can use both %d or %i where %d specifies signed decimal integer while %i specifies integer.
Problem: The problem of your code was using a whitespace before %i.
Wrong:
scanf(" %i ", &number); //Wrong
Right:
scanf("%i", &number); //Right.
Related
Why is my scanf for my array running before the for loop? Output [enter image description here][1]
#include <stdio.h>
void main()
{
int num,i,arr[10];
printf("The number you want to add with each number is:");
scanf("%d \n",&num);
// printf("%d \n",num);
printf("the 10 numbers are:\n");
for(i=0;i<=9;i++){
scanf("%d \n",&arr[i]);
}
printf("The new array is:");
for(i=0;i<=9;i++){
arr[i] = arr[i]+ num;
printf("%d \n",arr[i]);
}
}
It's because you are using trailing white spaces in your scanf() syntax. In addition, you are using \n in scanf and your scanf here is not only scanning the number %d, but scanning the whole thing in between quotations (" "). and that causes unexpected errors. So you should write your code in another way.
So I'm having a problem about showing what my original variable value is after changing it in the code.
#include <stdio.h>
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
while (n!=0){
n/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
Example input:123
Output of this code "your number 0 has 3 digits"
I want to know how to be able to refer the variable "n" in the printf to the original value of '123' so the output will be "your number 123 has 3 digits"
I would recommend that you use a separate variable to save your value or count with a different variable.
This code would look something like this:
#include <stdio.h>
int main()
{
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
int buffer = n
while (buffer!=0)
{
buffer/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
This way you save your variable in your code and you only used a buffer and not the actual value n.
You can keep a copy of the original variable and use that copy of the variable while printing.
You can do this:
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
printf("your number %d has ", n);
while (n!=0){
n/=10;
count++;
}
printf("%d digits", count);
return 0;
}
Of course, you may have to do some error checks..
My console keeps on crashing after entering a few numbers. I am trying to get an array of 10 numbers from the user thru the console and then taking count of positives, negatives, evens, and odds. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int pos, neg, even, odd;
int nums[10];
printf("Give me 10 numbers: ");
pos = neg = even = odd = 0;
for(int i = 0; i < 10; i++){
scanf(" %d", nums[i]);
if(nums[i] > 0){
pos++;
if(nums[i] % 2 == 0){
even++;
}
else{
odd++;
}
}
else{
neg++;
}
}
printf("Positives: %d, Negatives: %d, Evens: %d, Odds: %d\n", pos, neg, even, odd);
return 0;
}
In your code,
scanf(" %d", nums[i]);
should be
scanf(" %d", &(nums[i]));
or,
scanf(" %d", nums+i);
as you need to pass the pointer to variable as the format specifier's argument in scanf() .
To elaborate, %d expects a pointer to int and what you're supplying is an int variable. it invokes undefined behavior.
That said,
Always check the return value of scanf() to ensure proper scanning.
int main() should be int main(void) to conform to the standard.
Modify scanf like scanf(" %d", &nums[i]);
scanf(" %d", nums[i]);
Scanf expects a pointer to a location to write to, and you're not giving it one.
Change your scanf to:
scanf(" %d", &(nums[i]));
to make your program work.
With this change I tested your program with stdin of
20 10 9 1 39 1 2 2 31 1
And recieved output:
Give me 10 numbers: Positives: 10, Negatives: 0, Evens: 4, Odds: 6
ideone of the thing for your testing purposes.
Change scanf(" %d", nums[i]); to scanf(" %d", &nums[i]);, because scanf() needs addresses. The parentheses around nums[i] isn't necessary, and may effect readability.
Also note that 0 is even, but not negative.
When scanf is usedto convert numbers, it expects a pointer to the corresponding type as argument, in your case int *:
scanf(" %d", &nums[i]);
This should get rid of your crash. scanf has a return value, namely the number of conversions made or the special value EOF to indicate the end of input. Please check it, otherwise you can't be sure that you have read a valid number.
When you look at your code, you'll notice that you don't need an array. Afterreading the number, you don't do aything with the array. You just keep a tally of odd, even and so on numbers. That means you just need a single integer to store the current number. That also extends your program nicely to inputs of any length.
Here's a variant that reads numbers until the end of input is reached (by pressing Ctrl-D or Ctrl-Z) or until a non-number is entered, e.g. "stop":
#include <stdlib.h>
#include <stdio.h>
int main()
{
int count = 0;
int pos = 0;
int neg = 0;
int even = 0;
int odd = 0;
int num;
while (scanf("%d", &num) == 1) {
count++;
if (num > 0) pos++;
if (num < 0) neg++;
if (num % 2 == 0) even++;
if (num % 2 != 0) odd++;
}
printf("%d numbers, of which:\n", count);
printf(" %d positive\n", pos);
printf(" %d negative\n", neg);
printf(" %d even\n", even);
printf(" %d odd\n", odd);
return 0;
}
Change scanf statement after for loop to
scanf(" %d", &nums[i]);
So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d
Basically I have a C program where the user inputs a number (eg. 4). What that is defining is the number of integers that will go into an array (maximum of 10). However I want the user to be able to input them as "1 5 2 6" (for example). I.e. as a white space delimited list.
So far:
#include<stdio.h>;
int main()
{
int no, *noArray[10];
printf("Enter no. of variables for array");
scanf("%d", &no);
printf("Enter the %d values of the array", no);
//this is where I want the scanf to be generated automatically. eg:
scanf("%d %d %d %d", noArray[0], noArray[1], noArray[2], noArray[3]);
return 0;
}
Not sure how I might do this?
Thanks
scanf automatically consumes any whitespace that comes before the format specifier/percentage sign (except in the case of %c, which consumes one character at a time, including whitespace). This means that a line like:
scanf("%d", &no);
actually reads and ignores all the whitespace before the integer you want to read. So you can easily read an arbitrary number of integers separated by whitespace using a for loop:
for(int i = 0; i < no; i++) {
scanf("%d", &noArray[i]);
}
Note that noArray should be an array of ints and you need to pass the address of each element to scanf, as mentioned above. Also you shouldn't have a semicolon after your #include statement. The compiler should give you a warning if not an error for that.
#include <stdio.h>
int main(int argc,char *argv[])
{
int no,noArray[10];
int i = 0;
scanf("%d",&no);
while(no > 10)
{
printf("The no must be smaller than 10,please input again\n");
scanf("%d",&no);
}
for(i = 0;i < no;i++)
{
scanf("%d",&noArray[i]);
}
return 0;
}
You can try it like this.