Blank output window in dev c++ - c

Why is there blank output window?
#include <stdio.h>
main()
{
int x, y, sum, product;
scanf("%d", &x);
scanf("%d", &y);
sum = x + y;
product = x * y;
printf("%d\n", sum);
printf("%d\n", product);
}

I understand your problem what actually happens is that you enter 2 numbers and the result doesn't get displayed right?
So what actually happens is that your result does get displayed but for a split second (remember the output window will close as soon as it finishes executing the code)
You could try this
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,sum,product;
scanf("%d",&x);
scanf("%d",&y);
sum=x+y;
product=x*y;
printf("%d\n",sum);
printf("%d\n",product);
getch();
}
see we are using getch(); what this function do is actually waits for a user to enter a key so the program hasn't ended unless you press a key, hence you are able to see your output.

Related

functions in c programming

I am new to c ,while i am writing a basic program in c ,it is showing two answers ..1)when i declare and intialize variables separately 2)when I declare and initialize variable in a same line.can any one tell me my mistake please?
#include <stdio.h>
#include <stdlib.h>
void sum()
{
printf("enter the numbers to be added\n");
int x=scanf("%d",&x);
int y=scanf("%d",&y);
int sum=(x+y);
printf("the sum of two numbers is %d\n",sum);
}
int main()
{
printf("welcome to addition calculator\n");
sum();
return 0;
}
I am getting 2 as answer when i gave 3 and 4 as inputs
scanf("%d", &x) will store the read number into x. It will return the number of successfully read fields (1 in your case). If you assign that return value to x afterwards, you overwrite whatever the user entered with that 1. And 1 + 1 produce 2.
Solution:
int x;
int y;
scanf("%d",&x);
scanf("%d",&y);
As David reminds in comments, you might want to check that all fields were read successfully. For example, in your case, if you enter a non-digit, scanf will not resolve the %d field as successful, and will return 0. You can test this result to make sure the user did what they were supposed to do:
int x;
int y;
while (scanf("%d", &x) != 1) {
printf("Enter a NUMBER, you illiterate buffoon!\n");
}
while (scanf("%d", &y) != 1) {
printf("Enter a NUMBER! You managed with %d, how is this suddenly hard now?!\n", x);
}
scanf() function returns 1 if it scan successfully otherwise it return 0. That's why when you put an integer to x, scanf() return 1 and assign it to x(x=1). Same for y(y=1).
As x=1 and y=1.
sum = 2

Function run 2 times.?

I made this program, code successfully compiled I thought that the program takes 5 inputs from user but it takes 10 inputs.
#include <stdio.h>
int greatest_number();
int main()
{
greatest_number();
printf("Greatest number is %d", greatest_number());
return(0);
}
int greatest_number()
{
int a[6], x, i, z, y;
I don't know why this loop takes 10 inputs from user
even i programmed it to take 5 inputs
for(x=0; x<5; x++)
{
printf("Enter a number:");
scanf("%d", &a[x]);
}
a[x]=0;
x=0, i=1;
y=0;
z=a[x];
while(a[x])
{
if(z>a[i]){
z=a[y];
}else {
z=a[i];
y=i;
}
x++;
i++;
}
return(z);
}
In this code snippet the function greatest_number is called twice
greatest_number();
^^^^^^^^^^^^^^^^^
printf("Greatest number is %d", greatest_number());
^^^^^^^^^^^^^^^^^
The first call is redundant and its result is discarded.
Also it seems that then all entered elements have negative values (except the last element that has as I have understood a sentinel value) then the function will return the sentinel value.

Finding if a Number if Prime or not In C

I was writing a C Program to find if a number is prime or not. Everytime I run it and enter a number, the value of the input changes. PLease point out the loopholes.
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
int y;
y=getchar();
for(x=2;x<y;++x){
if(y%x != 0 && y!=x)
printf(" THE NUMBER %d is A PRIME \n", y);
else
printf(" \r THE NUMBER %d IS NOT A PRIME", y);
break;
}
}
I use the Code::Blocks IDE with GCC Compiler
As the name implies, getchar() gets a single character from standard input. For example, if you enter 3, the y gets the ASCII code of the character '3', which is obviously not what you want.
Try scanf:
scanf("%d", &y);
getchar returns the ASCII code of a single character. Consequently, your program picks up the ASCII code of the first character of the number you input and checks if it is prime.
Instead, you need to read an integer:
scanf("%d", &y);
The complete program:
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
int y;
scanf("%d", &y);
for(x=2;x<y;++x){
if(y%x != 0 && y!=x)
printf(" THE NUMBER %d is A PRIME \n", y);
else {
printf(" \r THE NUMBER %d IS NOT A PRIME", y);
break;
}
}
}
Note: You can stop when x >= sqrt(y)
Well, you are calling getchar() which is used to input a single character and this is what happens in your case:
getchar() returns a character.
Character is then converted into integer when you store it in variable of type int.
Hence that integer contains the ASCII of input character i.e. 3 will be stored as 51 that is the reason input changes.
What you need to do is to input an integer instead of character. Try this:
scanf("%d", &y);
Hope this helps.
First answers are correct about input for y:
scanf("%d", &y);
Also, please note that you should loop until square root of x, and not more if your want to optimize your algorithm (I won't demonstrate here why, it's a mathematical property).
#include <stdio.h>
#include <math.h>
// ...
int x;
int x_max;
int y;
scanf("%d", &y);
x_max = (int)floor(sqrt(y));
for(x=2;x<=x_max;++x){
// ...

How to generate a new number each time my program loops?

I'm trying to right a program in C where I have to ask the user a certain multiplication question, I'm using rand() to generate the numbers.
If the user gets the answer wrong, then they will be asked to enter it again once they get it right, when they do get it right, the program should loop and ask the user a different question.
I'm using a separate function to generate the answer each time the 2 random values are passed to that function.
My problem is that once I get the answer correct, the program loops and asks the same question, it picks the same number! so how do I make it that everytime it loops, it picks a different number?
#include <stdio.h>
int multiply(int x, int y);
int main()
{
srand (time(NULL));
int x = rand()%20;
int y = rand()%20;
int i, answer;
i = multiply(x,y);
do {
printf("what is %d multiplied by %d?:", x, y);
scanf("%d", &answer);
while(answer != i)
{
printf("wrong try again!");
scanf("%d", &answer);
}
printf("very good!\n");
} while(i==answer);
}
int multiply(int x, int y)
{
int k;
k=x*y;
return k;
}
You need to move the assignments of x and y into the loop. This way they will get a new value in each round. In fact, you can move their whole definition in there.
Moreover, the loop coondition while(i==answer) is superfluous as at that point it is always going to be true. For the sake of clarity, you should replace it with true to make it explicit that it is an infinite loop. (And you may want to extend your program with a way to exit gracefully, e.g. if 'q' or an empty string is entered - but I will leave this as an exercise for you :-).
while(true) {
int x = rand()%20;
int y = rand()%20;
int i, answer;
i = multiply(x,y);
printf("what is %d multiplied by %d?:", x, y);
scanf("%d", &answer);
while(answer != i)
{
printf("wrong try again!");
scanf("%d", &answer);
}
printf("very good!\n");
}
Abstractly, your logic should go like this:
// seed random number generator
while (true) // ask infinitely many questions
{
int x, y, expected_result; // populate randomly
printf("Please compute the result of %d and %d: ", x, y);
while (read_answer() != expected_result)
{
printf("Sorry, wrong answer. Try again: ");
}
}
That is, for each question you generate new parameters.
You just need to implement read_answer() as a helper function that reads one integer from the input, e.g. using fgets and strtol.

The code shows no error on compilation,but it doesn't show any output

The program is very simple , it gives the greatest common divisor as the output.I have verified my algorithm.The compiler issues no error ,but still it wont produce any output.
#include<conio.h>
#include <stdio.h>
int gcd(int ,int );
int main()
{
int a,b,j;
printf("enter two numbers");
scanf("%d\n",&a);
scanf("%d\n",&b);
j=gcd(a,b);
printf("gcd is %d",j);
getch();
return 0;
}
int gcd(int x, int y)
{
int temp,c;
if(x<y)
{
temp=x;
x=y;
y=temp;
}
if(y<=x&&(x%y==0))
return y;
else
{ temp=x%y;
c=gcd(y,temp);
return c;
}
}
This could be due to buffering of the output. Add \n to your printfs and see if it fixes it:
printf("enter two numbers\n");
printf("gcd is %d\n",j);
Alternatively, you can add calls to fflush(stdout) to flush the output buffer:
printf("enter two numbers");
fflush(stdout);
printf("gcd is %d",j);
fflush(stdout);
Other than that, it (almost) works as intended on my setup:
enter two numbers
4783780
354340
1
gcd is 20
The only thing is that the \n forces it to read an extra character. (which I chose to be 1)
The problem is
scanf("%d\n",&a);
scanf("%d\n",&b);
Delete the \n, just
scanf("%d",&a);
scanf("%d",&b);
is OK
This line:
printf("enter two numbers");
doesn't print a newline character (\n), and so the output isn't flushed to the console.
Try adding this after the printf:
fflush(stdout);
scanf("%d\n",&a);
scanf("%d\n",&b);
to
scanf("%d%*c",&a);
scanf("%d%*c",&b);

Resources