Can't get a basic C program to run [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to run a simple code, that gets two numbers from the user and prints their sum
there it is:
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 0, sum = 0;
printf("Insert two numbers:\n");
scanf("%d %d", n1, n2);
sum = n1 + n2;
printf("The sum of %d and %d is - %d\n", n1, n2, sum);
printf("%d + %d = %d", n1, n2, sum);
getch();
}
I'm trying to run it, when i do the first print command gets executed and when i press enter for it to scan it crashes, I'm using code::blocks 13.12 to compile and run
thank you! :)

Your program crashes because you are scanning numbers to wrong memory addresses. Change
scanf("%d %d", n1, n2);
to
scanf("%d %d", &n1, &n2);

Your program doesn't work because scanf takes a pointer to int, for each "%d" specifier, so pass the address of n1 and n2 instead of n1 and n2.
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 0, sum = 0;
printf("Insert two numbers:\n");
scanf("%d %d", &n1, &n2);
/* ^ ^ address of operater, take the addreess of n2 and pass it */
/* | address of operater, take the addreess of n1 and pass it */
sum = n1 + n2;
printf("The sum of %d and %d is - %d\n", n1, n2, sum);
printf("%d + %d = %d", n1, n2, sum);
getch();
}
but not just that, you should check that in fact the to numbers where read, otherwise your program will invoke undefined behavior, to make sure you read the integers successfuly you should check the return value of scanf it returns the number of arguments matched, so
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 0, sum = 0;
printf("Insert two numbers:\n");
/* if it equals two it succeeded, since we requested two integers */
if (scanf("%d %d", &n1, &n2) == 2)
{
sum = n1 + n2;
printf("The sum of %d and %d is - %d\n", n1, n2, sum);
printf("%d + %d = %d", n1, n2, sum);
}
else
{
printf("error reading 2 integers\n");
return 1; /* non zero means error */
}
getch();
/* return success from main() */
return 0;
}

Related

Why can't I properly do subtraction in this code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter Two Numbers: ");
scanf("%i%i",&n1,&n2);
subtractNumbers(n1,n2);
printf("The Difference is %i", n1,n2);
}
int subtractNumbers(int n1,int n2)
{
int result;
result =n1-n2;
return result;
}
Why can't this code properly do a subtraction?
Have to store the return value in 'difference' variable. (As I guess the 'difference' variable is meant for that)
In case of 'printf("The Difference is %i", n1,n2);' I guess the intention is to print the 'difference'. So change to 'printf("The Difference is %i", difference);'
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter Two Numbers: ");
scanf("%i%i",&n1,&n2);
difference = subtractNumbers(n1,n2); // stored the returend result
printf("The Difference is %i", difference); //printing only the result
}
int subtractNumbers(int n1,int n2)
{
int result;
result =n1-n2;
return result;
}
So basically you've mixed up the syntax. It's all right, but when you call your function, it's ok that you return a value, but the in your main you don't read that value.
This fixes it:
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter the first number : ");
scanf("%d",&n1);
printf("Enter the second number: ");
scanf("%d", &n2);
int result = subtractNumbers(n1,n2);
printf("The Difference is %d", result);
}
int subtractNumbers(int n1,int n2)
{
int result;
result = n1-n2;
return result;
}
Primary problem corrected like other answers, but with more commentary and a few "upgrades"...
#include <stdio.h>
// Please spread things out for your readers. Whitespace is free!
// if function PRECEEDS its use, it is its own "function prototype"
int subtractNumbers( int n1, int n2 )
{
int result = n1 - n2; // declare and assign together
return result;
}
int main() {
printf( "Enter Two Numbers: " );
// define variables "close" to when they are first used
int n1, n2;
// ALWAYS check return codes from system functions. Things go wrong
if( scanf( "%i%i", &n1, &n2 ) != 2 )
printf( "Something went wrong\n" );
else
{
// here was where the returned value was not being received. fixed.
int difference = subtractNumbers( n1, n2 );
printf( "The Difference is %i", difference );
}
return 0; // optional with some compilers
}

The execution of the code takes input but doesnot return anything

How can I get output in this code? As you can see the question for the code which I have written in comments. This is written in C.
/*To print prime numbers from n1 to n2 where n1 and n2 are input by user and n1<n2*/
//please solve this code, i am stuck
#include <stdio.h>
#include <conio.h>
void main()
{
int n1, n2, i, j;
printf("Enter two numbers");
scanf("%d\t%d\n", &n1, &n2);
printf("The prime numbers in betwn %d and %d are:\n", n1, n2);
for (i = n1; i <= n2; i++)
{
for (j = 2; j < i; j++)
{
if (i % 2 == 0)
{
continue;
}
}
if (i = j)
{
printf("%d\t", i);
}
}
getch();
}
scanf("%d\t%d\n", &n1, &n2);
Issue seems here. Input needs to be provided in the same way as specified in the format of scanf(). I suggest changing it to the following for simplicity:
scanf("%d %d", &n1, &n2);
Another thing: if (i = j) , this should be comparison operator instead of assignment.
Since you are trying to print prime numbers in a range, the logic/approach seems incorrect as well.
To add more detail:
First, l agree with dev7060 and thanks to him. There are two aspects which you should pay attention to:
1) %d\t%d\n means you should input three parameters into the data flow. You can see this which l have tested in my side.
So please try this like dev7060 said:
scanf("%d%d", &n1, &n2);
And note when you input one parameter, please first hit a space bar and then input the second parameter. %d%d can grab two digits into the input stream before the entry by any space character, such as the TAB key or space bar, or the enter key. So when you enter parameters, you can get them by typing an interval character between them.
2) i=j means an assignment operation rather than a comparison operation and it always returns true.
You should use if(i==j).
Please refer to this:
#include <iostream>
#include <stdio.h>
#include <conio.h>
int main()
{
int n1, n2, i, j;
printf("Enter two numbers");
scanf("%d%d", &n1, &n2);
printf("The prime numbers in betwn %d and %d are:\n", n1, n2);
for (i = n1; i <= n2; i++)
{
for (j = 2; j < i; j++)
{
if (i % 2 == 0)
{
continue;
}
}
if (i == j)
{
printf("%d\t", i);
}
}
getch();
}
In addition, when you face these issues in VS:
error C4996: 'scanf': This function or variable may be unsafe. error
error C4996: 'getch': The POSIX name for this item is deprecated.
These are compiler errors and you should use scanf_s() instead of scanf() and use _getch() instead of getch(). You can refer to this.

for loop resulting in undefined behaviour c

#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int mult;
int n;
int ans;
ans = mult * i;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 0; i<n; i++) {
printf("%d x %d = %d \n", &mult, &i , &ans);
}
return 0;
}
Hi guys, this is a simple code which is supposed to help the user to list the times table for n times. However, i am receiving undefined behaviour and I am quite stumped as to what is wrong with my implementation of my "for" loop.
I am receiving this as my output.
6356744 x 6356748 = 6356736
for n times in my consoles.
I want to ask
Is anything wrong with the logic of my code? (i assume i do have a problem with my code so please do enlighten me)
Would it be better(or even possible) to use pointers to point to the memory addresses of the mentioned variables when i have to change the value of the variables constantly? If yes, how do i go around doing it?
Thanks!
In printf you must provide integers. You are now giving the addresses of integers. So change
printf("%d x %d = %d \n", &mult, &i , &ans);
to
printf("%d x %d = %d \n", mult, i, ans);
and to make the table, replace ans with just mult*i, so:
printf("%d x %d = %d \n", mult, i, mult*i);
You should also check the return value of scanf to check if it has succeeded reading your input:
do {
printf("Please enter a multiple you want to explore.");
} while (scanf("%d", &mult)!=1);
do {
printf("Please enter the number which you would want to multiply this number till.");
} while (scanf("%d", &n)!=1);
The things you see are the values of the variables memory location.
Change your lines inside for loop as below
ans = mult * i;
printf("%d x %d = %d \n", mult, i, ans);
There are some mistakes in your code .
you are using the & operator in print statement which is used to print the address of the variable.
Initiate the loop with the value '1' instead of '0' & execute the loop till 'i' less than equal to 'n'.
instead of using the ans variable outside the loop , use it inside the loop as it evaluate the multiplication result in each iteration of the loop.
#include <stdio.h>
int main()
{
int i;
int mult;
int n;
int ans;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 1; i<=n; i++) {
ans = mult*i ;
printf("%d x %d = %d \n", mult, i , ans);
}
return 0;
}

C Program That Counts Even Integers from a Set of 5 Integers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to write a C program where the user enters five different integers and determines the amount of even integers from the input of those five integers. Here is my current code:
#include <stdio.h>
int main()
{
int n1, n2, n3, n4, n5, sum;
//user enters 5 integers
printf("Enter five different positive integers: \n");
//program scans for user input
scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5);
//if statement to determine what integers are even
if(((n1,n2,n3,n4,n5)%2)==0)
//sum of even integers
sum = n1 + n2 + n3 + n4 + n5;
//program prints sum of even integers
printf("There are %d even integers in the input. \n", sum);
//program prints if there are no even integers for the inputs
else
printf("There are no even integers in the input. \n");
return(0);
}
Any ideas on what to do?
Your goal is not stated as clearly as needed:
Do you want to sum all even integers an ignore odd integers typed?
Do you want all integers to be even and refuse the input if it contains none?
Either way, your program fails for multiple reasons:
if(((n1,n2,n3,n4,n5)%2)==0) does nothing useful: it only checks if the last integer is even. You could check if all integers are even with this
if ((n1 | n2 | n3 | n4 | n5) % 2) == 0)
You did not use braces to group the instructions in the if body. Unlike Python, indentation play no role in C, you must use braces ({ and }) around multiple instructions to form a block after if, else, while, for etc.
Here is a modified version of your code that ignores the odd numbers:
#include <stdio.h>
int main(void) {
int n1, n2, n3, n4, n5, sum, count;
// user enters 5 integers
printf("Enter five different positive integers:\n");
// program scans for user input
if (scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5) != 5) {
printf("Invalid input\n");
return 1;
}
// for each integer, add it if it is even
count = 0;
sum = 0;
if (n1 % 2 == 0) {
sum += n1;
count++;
}
if (n2 % 2 == 0) {
sum += n2;
count++;
}
if (n3 % 2 == 0) {
sum += n3;
count++;
}
if (n4 % 2 == 0) {
sum += n4;
count++;
}
if (n5 % 2 == 0) {
sum += n5;
count++;
}
if (count > 0) {
printf("There are %d even integers in the input, their sum is %d.\n",
count, sum);
} else {
//program prints if there are no even integers for the inputs
printf("There are no even integers in the input.\n");
}
return 0;
}
Using some more advanced knowledge of C, you could simplify the code into this:
#include <stdio.h>
int main(void) {
int n1, n2, n3, n4, n5, sum, count;
printf("Enter five different positive integers:\n");
if (scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5) != 5) {
printf("Invalid input\n");
return 1;
}
// use the low order bit to test oddness
count = 5 - ((n1 & 1) + (n2 & 1) + (n3 & 1) + (n4 & 1) + (n5 & 1));
sum = n1 * !(n1 & 1) + n2 * !(n2 & 1) + n3 * !(n3 & 1) +
n4 * !(n4 & 1) + n4 * !(n4 & 1);
if (count > 0) {
printf("There are %d even integers in the input, their sum is %d.\n",
count, sum);
} else {
printf("There are no even integers in the input.\n");
}
return 0;
}
But it is actually more complicated, less readable and not provably more efficient.
The real improvement would be to use a loop:
#include <stdio.h>
int main(void) {
int i, n, sum = 0, count = 0;
printf("Enter five different positive integers:\n");
for (i = 0; i < 5; i++) {
if (scanf("%d, &n) != 1) {
printf("Invalid input\n");
return 1;
}
if (n % 2 == 0) {
sum += n;
count++;
}
}
if (count > 0) {
printf("There are %d even integers in the input, their sum is %d.\n",
count, sum);
} else {
printf("There are no even integers in the input.\n");
}
return 0;
}
(n1,n2,n3,n4,n5) is a sequence of expressions, separated by commas, which evaluates to the last expression, so this:
//if statement to determine what integers are even
if(((n1,n2,n3,n4,n5)%2)==0)
only determines the divisibility of n5
You can use an array:
int n[5];
And then, in a loop:
for (int i = 0; i < 5; i++) {
if ((n[i] % 2) == 0) {
sum += n[i];
}
}
a simple test of even numbers is divide by 2 and test for a 0 remainder
if ( N % 2 == 0 ) { // This is even, inc your even counter here}
just use a for loop and step thru adding up all that prove even
this works for float or int just fine
Ques. What you want to do?
Ans. You want to write a C program That Counts Even Integers from a Set of 5 Integers. that gives ouput as no. of even integers in the set of 5 integers.
I think you don't have learn the concept of array. Array make this problem to solve easily. but, don't worry I understand you according to you ques.
but,
It's your task to learn what is array why and when we use it ?
-----------------xxx--------xxx----------xxx-----------xxx-----------------------------------------------------------------------
Requirements are:
5 variable (i.e. num) that stores the values given by user
A variable i.e. count that counts the no. of even no's. it's initializes with 0 because initially no even no's are there before input is given.
-----------------xxx--------xxx----------xxx-----------xxx----------------------------------------------------------------------
1 more problem in your code :-
if(((n1,n2,n3,n4,n5)%2)==0) //it only check for n5 because comma operator seperates the values and gives only last value(i.e. n5) for computaion.
Solution:-
#include <stdio.h>
int main()
{
int n1, n2, n3, n4, n5, count=0; //var count works as a counter variable and it's value will update by 1 when any even no. encounters.
printf("Enter five different positive integers: \n");
scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5);
if(n1%2==0)
{
count=count+1; //now, count is set by 1 if the first input(n1) found even
}
if(n2%2==0)
{
count=count+1; //now, count is set by 2 if the second input(n2) found even
}
if(n3%2==0)
{
count=count+1; //now, count is set by 3 if the third input(n3) found even
}
if(n4%2==0)
{
count=count+1; //now, count is set by 4 if the fourth input(n4) found even
}
if(n5%2==0)
{
count=count+1; //now, count is set by 5 if the fifth input(n5) found even
}
printf("There are %d even integers in the input. \n", count); //count holds no. of even integers enconteres among 5
//if count prints 0 it indicates no even no occured.
you have to write same code using array and using loop. it's becomes your code small and fast to execute :)

Find even numbers between N number of cases in C

I want to make a code were you find all the even numbers between N number of cases(y). The next y lines will contain a n number (1<=n1<=100). For each line I want to find the even numbers between these. So for example:
input:
2 (number of cases; 1<=y<=10)
1
7
Output:
No even numbers
2 4 6
If there is no even numbers between them then print "No even numbers" for example:
So what I've made up until now is this:
#include <stdio.h>
int main()
{
int n1, n2, i, j, p, y;
printf("number of intervals: ");
scanf("%d", &y);
for(j=1; j<=y; j++)
{
scanf("%d", &n1);
for(i=1;i<=n1; i++)
{
p=i%2;
if(p==0)
printf(" %d", i);
}
return 0;
}
}
The thing is that I dont know how to implement the number of intervals to the code, it only works with two intervals.
Its always a good idea to make a function if you want to do similar thing with different values.You can make a function and call that function as many time as you need.For example
void printInterval(int n1, int n2){ //function will print all even
for(i=n1;i<=n2; i++)//number between n1 and n2 (both inclusive)
{
p=i%2;
if(p==0)
printf(" %d", i);
}
}

Resources