C Wrong Answer when squaring - c

I'm learning C using C by Example. There is a question where we need to take an input integer and square it and print it to the screen. I'm having a strange error, when no matter what Int I enter it give me this output:
Please enter a number.
5
Number = 2686764 Square of Number = 2686760
Here is the program I wrote.
#include <stdio.h>
#include <conio.h> //for getch()
main(){
int number, square;
printf("Please enter a number. \n");
scanf("%d", &number);
square = number*number;
printf("\n Number = %d ", &number);
printf("\t Square of Number = %d", &square);
getch();
}

You are printing the memory address of the number and square variables, not their values.
Try this instead:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);

Your printf statements are incorrect. Do not use & in this case:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);

To add to the above answers ... look at this example:
int number = 5;
printf(" %d\n", number); // Prints value of 'number'.
printf(" %p\n", &number); // Prints address of 'number'.
printf(" %d\n", *(&number)); // Prints value at address of 'number' ...

Related

How can I take a variable multiple times and implement counters in while loop?

I want this program to determine if given integers are positive/negative & odd/even then increment the counters. But it doesn't work properly.
include <stdio.h>
int main() {
int evenCount=0, oddCount=0, posCount=0, negCount=0, zeroCount=0, m;
char x='x';
while(x!='y') {
printf("enter an integer\n");
scanf("%d", &m);
if((m>0)&&(m%2==0)){
posCount+=1;
evenCount+=1;
}
else if((m>0)&&(m%2!=0)){
posCount+=1;
oddCount+=1;
}
else if((m<0)&&(m%2==0)){
negCount+=1;
evenCount+=1;
}
else if((m<0)&&(m%2!=0)){
oddCount+=1;
negCount+=1;
}
else {
zeroCount+=1;
}
printf("if you want to end the loop write 'y'\n");
scanf("%c", &x);
}
printf("odd %d \n", &oddCount);
printf("even %d \n", &evenCount);
printf("positive %d \n", &posCount);
printf("negative %d \n", &negCount);
return(0);
}
When I run it and give some numbers counts are at millions.
enter an integer
123
if you want to end the loop write 'y'
enter an integer
2
if you want to end the loop write 'y'
enter an integer
5
if you want to end the loop write 'y'
enter an integer
y
if you want to end the loop write 'y'
odd 6487572
even 6487576
positive 6487568
negative 6487564
The second example.
enter an integer
12
if you want to end the loop write 'y'
enter an integer
y
if you want to end the loop write 'y'
odd 6487572
even 6487576
positive 6487568
negative 6487564
I'm new to coding and this is my first post on this site also english is not my main language. I'm sorry if made any mistakes.
For starters you need to change this call
scanf("%c", &x);
to
scanf(" %c", &x);
Pay attention to the space before the conversion specifier.
And instead of trying to output addresses
printf("odd %d \n", &oddCount);
printf("even %d \n", &evenCount);
printf("positive %d \n", &posCount);
printf("negative %d \n", &negCount);
you have to write
printf("odd %d \n", oddCount);
printf("even %d \n", evenCount);
printf("positive %d \n", posCount);
printf("negative %d \n", negCount);
And it is better to substitute the while loop
while(x!='y') {
//...
printf("if you want to end the loop write 'y'\n");
scanf("%c", &x);
}
for do-while loop
do {
//...
x = '\0';
printf("if you want to end the loop write 'y'\n");
scanf(" %c", &x);
} while ( x != 'y' && x != 'Y' );

How to evaluate an expression in a for loop in C programming?

I am trying to print cube of all the numbers from 1 to 20 (n3). I was wondering in my code if
printf("Enter an integer value\n");
is necessary. What's the purpose of that line? Can someone explain? Thanks. It's my first day learning C.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv)
{
int num;
printf("Enter an integer value\n");
scanf("%d", &num);
for ( num=1; num<21; num++){
printf("The cube of %d is %d\n", num, num*num*num);
}
getchar();
}
By using printf() you are printing msg "Enter an integer value" on the screen.
By using scanf() you are taking the value which you have typed on screen into num variable(i.e. initializing num).But in forloop you are reinitializing your num variable to "1" to "21".So printf() and scanf() are just waste of time , here .
if you want a specific numbers cube then just do as follows,
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
printf("Enter an integer value\n");
scanf("%d", &num);
printf("The cube of %d is %d\n", num, num*num*num);
getchar();
}
It is a meaningless
printf("Enter an integer value\n");
scanf("%d", &num);
it just display the message and read the value from keyboard, but in for loop when you assign 1 to the variable num then previous value of num override.
it doesn't serve for anything in your code.
You are getting the variable num but you are neglecting it very soon for ( num=1 ...
If you want it as a useful you can do something similar :
int num, i;
printf("Enter an integer value\n");
scanf("%d", &num);
for ( i=1; i<num; i++){ // print the cube of all numbers less than the entered value
printf("The cube of %d is %d\n", i, i*i*i);
}
getchar();
Otherwise, you can just erase those two lines code:
printf("Enter an integer value\n");
scanf("%d", &num);
the line printf("Enter an integer value\n");
display the words "Enter an integer value" to the user of the program,
in your code the next to lines have no meaning, here you ask the user to enter a integer, then save that number:
printf("Enter an integer value\n");
scanf("%d", &num);
because in the next line you set parameter num to be 1.
this next code scan the user input and then calculate the cube:
int main(int argc, char** argv)
{
int num;
printf("Enter an integer value\n");
scanf("%d", &num);
printf("The cube of %d is %d\n", num, num*num*num);
}

Average of entered numbers in C - sum always 0

Here is a code which evaluates the average of 10 entered numbers. Problem is it doesn't seem to print the sum correctly (it's always equal to 0) after exiting the loop, everything else is working fine.
int count=0, n=10, c;
float sum=0, x;
do{
printf("x=");
scanf("%f", &x);
count++;
sum+=x;
}
while(count<n);
printf("Sum is %d", sum);
printf("\nCount is: %d", count);
printf("\nThe Average of the numbers is : %0.2f", sum/count);
getch();
}
Another question is how to exit the loop after a symbol is reached(i.e. without setting a limit to the number of integers to be entered).
Use the %f format specifier for floating point numbers.
printf("Sum is %f", sum);
To exit the loop on a symbol, you could check the return value from scanf. scanf returns the number of items read. If it returns 0 then the user didn't type a valid number.
while (1) {
printf("x=");
if (scanf("%f", &x) != 1) {
break;
}
...
}
break exits the current loop.
To answer your first question it should be printf("%f",sum) to print the correct sum. Since you are using float you have to use %f, if you use int it is %d. For your second question, you can do something like this (modify it accordingly):
int main(){
// Declare Variables
int count = 0; float sum = 0, currentNum = 0;
// Ask user for input
while(currentNum > -1)
{
printf("Enter integer to be averaged (enter -1 to get avg):");
scanf("%f",&currentNum);
if(currentNum == -1)
break;
// Check the entered number and computed sum
printf("You entered: %0.2f\n", currentNum);
sum += currentNum;
printf("Current sum: %0.2f\n", sum);
count++;
}
// Print Average
printf("Average is: %0.2f\n", sum/count);
return 0;
}
To answer your second question, you could do this:
scanf("%f", &x);
if (x==0) {
break;
}
This will break you out of the loop if you enter 0, then your loop can be infinite:
do {
} while(true)
For the second question, I think EOF may be the better solution:
while(scanf("%f", &x) != EOF)

Write a program that prints sum, product and quotient of two input integers in C

I'm new to the language and all this overflow problems and integer types are getting on my nerves. here is what I have but when I run it I get,
-bash: syntax error near unexpected token `newline'
The code:
#include <stdio.h>
int main(void)
{
int one, two, s, q, m;
s = one+two
q = one/two
m = one*two
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
printf("The addition of %d and %d is %d", one, two, s);
printf("The integer division of %d divided by %d is %d", one, two, q);
printf("the multiplication of %d and %d is %d", &one, &two, m);
return 0;
}
Thank you
You should perform the calculations after you've got the input.
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
s = one+two;
q = one/two;
m = one*two;
try this:
#include <stdio.h>
int main(void)
{
int one, two;
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
printf("The addition of %d and %d is %d", one, two, (one+two));
printf("The integer division of %d divided by %d is %d", one, two, (one/two));
printf("the multiplication of %d and %d is %d", &one, &two, (one*two));
return 0;
}
You are missing the semicolons after
s = one+two
q = one/two
m = one*two
Plus you should perform the calculations after you read the input but that's a different problem.
These lines makes the problem:
s = one+two
q = one/two
m = one*two
You missed the ; (semicolon)
Change it like:
s = one+two;
q = one/two;
m = one*two;
Also read the input from user first before doing the operations.

Getting multiple values with scanf()

I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by doing:
scanf( "%i", &minx);
But I would like the user to be able to do something like:
Enter Four Ints: 123 234 345 456
Is it possible to do this?
You can do this with a single call, like so:
scanf( "%i %i %i %i", &minx, &maxx, &miny, &maxy);
Yes.
int minx, miny, maxx,maxy;
do {
printf("enter four integers: ");
} while (scanf("%d %d %d %d", &minx, &miny, &maxx, &maxy)!=4);
The loop is just to demonstrate that scanf returns the number of fields succesfully read (or EOF).
int a,b,c,d;
if(scanf("%d %d %d %d",&a,&b,&c,&d) == 4) {
//read the 4 integers
} else {
puts("Error. Please supply 4 integers");
}
Just to add, we can use array as well:
int i, array[4];
printf("Enter Four Ints: ");
for(i=0; i<4; i++) {
scanf("%d", &array[i]);
}
Could do this, but then the user has to separate the numbers by a space:
#include "stdio.h"
int main()
{
int minx, x, y, z;
printf("Enter four ints: ");
scanf( "%i %i %i %i", &minx, &x, &y, &z);
printf("You wrote: %i %i %i %i", minx, x, y, z);
}
The question is old, but if someone could help from this with real-life example.
For Single Input data -
int number;
printf("Please enter number : ");
scanf("%d", &number);
For Multiple Input data at a line -
int number1, number2;
printf("Please enter numbers one by one : ");
scanf("%d %d", &number1, &number2);
%d %d is for decimal format. You could use format which is suitable for your data type as many as needs with just a space
&number1, &number2 - Use comma between variable names
If needs More real-life example check this practical example - https://devsenv.com/tutorials/how-to-take-input-and-output-in-c-programming
Hope, this will help someone.
Passable for getting multiple values with scanf()
int r,m,v,i,e,k;
scanf("%d%d%d%d%d%d",&r,&m,&v,&i,&e,&k);
int a[1000] ;
for(int i = 0 ; i <= 3 , i++)
scanf("%d" , &a[i]) ;

Resources