Create a function in C [closed] - c

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 3 years ago.
Improve this question
Write a program that prompts the user to enter in two long values. Implements a function called negative_count() that takes two arguments of data type long and returns an integer that is the number of arguments that were negative. The function main() then displays the result.
For example:
Enter two integers of data type "long": -1264364007 -2012334695
Number of negative number entered 2
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
long num1,num2;
void Negative_Count(long int,long int);
int main(void) {
printf("Enter two number: ");
scanf("%ld%ld",&num1,num2);
("you entered %ld%ld",&num1,&num2);
Negative_Count(num1,num2);
return (0);
}
void Negative_Count(long int num1,long int num2)
{
if (num1,num2<0)
printf("%ld%ld is negative.",&num1,&num2);
else if (num1,num2>0)
printf("%ld%ld is positive.",&num1,&num2);
else if (num1>0, num2<0)
printf("%ld is negative",&num2);
else if (num1<0, num2>0)
printf("%ld is negative",&num1);
}
Can I use a loop with two variables?

scanf("%ld%ld",&num1,num2);
should be
scanf("%ld%ld", &num1, &num2);
("you entered %ld%ld",&num1,&num2);
should be
printf("you entered %ld %ld", num1, num2);
if (num1,num2<0)
printf("%ld%ld is negative.",&num1,&num2);
else if (num1,num2>0)
printf("%ld%ld is positive.",&num1,&num2);
else if (num1>0, num2<0)
printf("%ld is negative",&num2);
else if (num1<0, num2>0)
printf("%ld is negative",&num1);
should be
if (num1 < 0 && num2 < 0)
printf("%ld and %ld are negative.", num1, num2);
else if (num1 > 0 && num2 > 0)
printf("%ld and %ld are positive.", num1, num2);
else if (num1 > 0 && num2 < 0)
printf("%ld is negative", num2);
else if (num1 < 0 && num2 > 0)
printf("%ld is negative", num1);
Use the and operator (&&): If both the operands are non-zero, then condition becomes true.
And note that printf (as opposed to scanf) doesn't need the address of the variable, just pass the value.

Your scanf and printf formatting had issues. Also the conditional check in the if statements were also wrong.
You can modify the code as below for proper working...
#include <stdio.h>
#include <stdlib.h>
void Negative_Count(long int,long int);
int main(void) {
long num1,num2;
printf("Enter two number: ");
scanf("%ld%ld",&num1,&num2);
printf("you entered %ld%ld \n",num1,num2);
Negative_Count(num1,num2);
return 0;
}
void Negative_Count(long int num1,long int num2)
{
if (num1<0 && num2<0) {
printf("%ld , %ld are negative.",num1,num2);
}
else if (num1>0 && num2>0) {
printf("%ld , %ld are positive.",num1,num2);
}
else if (num1>0 && num2<0){
printf("%ld is negative",num2);
}
else if (num1<0 && num2>0){
printf("%ld is negative",num1);
}
}

Related

Who is the winner? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm new to C. I've made the code, but I can't seem to find why the output doesn't show.
This is the question
Problem 2: Who is the winner? (Level 2)
• Problem description
Alice and Bob are playing a game. Both of them write down an integer number. If the sum of two integers is a square of an integer, Alice wins the game. If the reversed number of the sum is a square of an integer, Bob wins. If neither of them wins, or both of them win, it is a tie.
For example, Alice wrote an 8, Bob wrote a 10. 18 is not a square of any integer. But 81 is a square of 9. So Bob is the winner.
Write a program to take two integers numbers and output the winner of the game.
• Input & output requirements
Input two positive integer numbers. The output should follow the format as shown in sample results:
Sample 1
8 10
Bobs wins
Sample 2
2 2
Tie
Code:
#include <stdio.h>
int reverse(int numOne, int numTwo)
{
int rev;
rev = numTwo*10 + numOne;
return rev;
}
int issquare(int num)
{
int i, valid;
valid = 0;
for(i = 0; i < num; i++)
{
if((i*i) == num)
{
valid = 1;
}
}
return valid;
}
int main()
{
int num1, num2, normal, rev, alice, bob;
scanf("%d %d", &num1, &num2);
printf("%d %d", num1, num2);
rev = reverse(num1, num2);
normal = num1*10 + num2;
bob = issquare(normal);
alice = issquare(rev);
if(bob==1)
{
if(alice==1)
{
printf("Tie");
}
}
if(bob==1)
{
if(alice==0)
{
printf("Bob wins.");
}
}
if(alice==1)
{
if(bob==0)
{
printf("Alice wins.");
}
}
return 0;
}
It must be the bob == 0 and alice == 0 case. Put an print stamement in the else statement you will know why. Your execution is making that case somehow.
Also in issquare you should have the loop like this
for(i = 0; i <= num; i++) because for numbers like 1 it would fail.
Also there is a flaw in your logic. You should form the number
8 10 you should check 81 and 18. But in your case you don't do this.(You check 90 and 108).
#include <stdio.h>
#include <stdlib.h>
int reverse(int num)
{
int rev=0,inter = 0;
while(num){
inter = rev*10;
if( inter/10 != rev){
fprintf(stderr, "%s\n", "Overflow\n");
exit(1);
}
rev=inter+num%10;
num/=10;
}
return rev;
}
int issquare(int num)
{
for(int i = 0; i <= num; i++){
long long ii = i;
long long mul = ii*ii;
if( ii!= 0 && mul/ii != ii){
fprintf(stderr, "%s\n", "Overflow\n");
exit(1);
}
if(mul == num)
return 1;
else if(mul > num){
break;
}
}
return 0;
}
int main()
{
int num1, num2, normal, rev, alice, bob;
if( scanf("%d%d", &num1, &num2)!= 2){
fprintf(stderr,"Error in input\n");
exit(1);
}
printf("%d %d", num1, num2);
rev = reverse(num1+num2);
normal = num1+ num2;
bob = issquare(rev);
alice = issquare(normal);
if(bob==alice)
printf("Tie");
else if(bob > alice)
printf("Bob wins.");
else
printf("Alice wins.");
return 0;
}
The way you calculate normal is not correct.
normal=num1+num2;
And function you write for reverse is also not correct.
int reverse(int num1,int num2)
{
int sum=num1+num2;
int ans=0;
while(sum>0)
{
ans =ans*10 +(sum%10);
sum/=10;
}
return ans;
}
And you also not checking for condition where both lose.
Add that condition as well.

How to stop the int main(void) printf statements when there is an else statement in another function that gives another result? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So everything works in the program so far how the teacher wants it too. However, when I enter 0 for num2 it executes the printf like I want it too, but it also executes the printf at the end of the main. Is there anyway to stop it from also executing the one at end int main when I enter 0? I apologize if my code sucks. This is my first year(semester actually) coding and the teacher doesn't really teach us and I've been learning from the book
#include <stdio.h>
//Program with 3 functions 1) Divides 2) Multiplies 3) Calls the other functions
float multiply(float num1, float num2);
float divide(float num1, float num2);
void math_call(float num1,float num2);
int main(void){
float num1;
float num2;
//printf/scanf allows you to choose values for the functions
printf("Please enter 2 numbers:\n");
scanf("%f", &num1);
scanf("%f", &num2);
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
return(0);
}
//Calls function 1 and 2
void math_call(float num1,float num2){
divide(num1,num2);
multiply(num1, num2);
}
//Divides the arguments and doesn't allow to divide by zero
float divide(float num1, float num2){
if(num2 != 0){
float divide;
divide = num1/num2;
return divide;
}
else{
printf("You can't divide by zero. So your answer for division is undefined.\n");
return(0);
}
}
//multiplies the arguments
float multiply(float num1, float num2){
if(num2 != 0){
float multiply;
multiply = num1*num2;
return multiply;
}
else{
printf("If you multiply by zero you will always get zero.\n");
return(0);}
}
This is the result
Please enter 2 numbers:
4
0
If you multiply by zero you will always get zero.
You can't divide by zero. So your answer for division is undefined.
Division = 0.000000 Multiplication = 0.000000
RUN SUCCESSFUL (total time: 1s)
Instead of this in your code
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
Rewrite it as below
// call your functions here and fill variables with results
float divResult = divide(num1, num2);
float mulResult = multiply(num1, num2);
// check if num2 is not zero
if(num2 != 0)
{
// if it isn't it will print your message
printf("Division = %f Multiplication = %f", divResult, mulResult);
}

Why is scanf() taking wrong input? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am completely new to C, and was writing a small very simple program. The problem is that the scanf() is taking wrong input:-
#include <stdio.h>
int main(int args, char*argv[])
{
int num1 = scanf("%d",&num1) ;
int num2 =scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
When I give 34 and 23 as input it is outputting:-
Num1 = 1
Num2 = 1
Why is it so?
Don't assign the return value of scanf to num1 and num2. The return value of scanf indicates if the scan is successful, not what you thought it was.
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
This is because scanf returns the number of characters it matches. scanf("%d",&num1) and scanf("%d",&num2) will return 1. You are assigning that number, i.e, 1 to num1 and num2.
Now do it as follows:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1;
int num2;
scanf("%d",&num1) ;
scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
Suggested reading: comp.lang.c FAQ list · Question 3.8.
You're assigning the return value of scanf into your values. scanf returns how many characters it matched; you're already passing a reference to the variable you want scanf to store the result in.
Why are you assigning the return value of scanf into your num1 and num2?
My suggestion:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1, num2;
scanf("%d",&num1);
scanf("%d", &num2);
printf("Num1 = %d\n", num1);
printf("Num2 = %d\n", num2);
return 0;
}

Prime Number Check 1-100 (C) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
The code below is to read input from the user to check if an int [1-100] is a prime number or not. (If out of range, will print "Done). If non prime, will output that to the console and the number divisible.
Right now this program is running correctly for 1-10 except for 3 and 9... Any suggestions?
#include <stdio.h>
int main()
{
int num, i;
printf("Number [1-100]:? \n");
scanf("%d", &num);
while(num>0 && num <101)
{
if (num==1||num==2)
printf("Prime\n");
for (i=2; i<=num/2; ++i)
{
if (num%i==0)
{
printf("Non-prime,divisible by %d\n",i);
break;
}
else {
printf("Prime\n");
break;
}
}
printf("Number[1-100]:? \n");
scanf("%d",&num);
}
printf("Done\n");
}
First, make sure your code has appropriate whitespace. This will help you realize when things aren't lined up like you think they are.
#include <stdio.h>
int main()
{
int num, i;
printf("Number [1-100]:? \n");
scanf("%d", &num);
while(num>0 && num <101){
if (num==1||num==2)
printf("Prime\n");
for(i=2; i<=num/2; ++i)
{
if (num%i==0)
{
printf("Non-prime,divisible by %d\n",i);
break;
}
else {
printf("Prime\n");
break;
}
}
printf("Number[1-100]:? \n");
scanf("%d",&num);
}
printf("Done\n");
}
Now you should realize that your else statement happens on the first check! So when 3 is not divisible by 2, it prints "prime."
And then it breaks out of the loop.
And this happens for EVERY number. All your program is doing is checking to see if numbers are divisible by 2.
If you wrote "Odd" instead of "Prime" it would at least be correct there.
This is the kind of problem where setting a flag might be useful (there are other ways to do this, but this is one way).
So you could set a flag, say int isPrime = 1;
Now, if you find out that the number is not prime, you simply set isPrime = 0;.
Finally, at the end of the for loop (let me repeat: AFTER the for loop finishes), you need to check that variable.
And you can say,
if (isPrime == 1)
{
printf("Prime\n");
} else
{
printf("Non-prime.");
}
I'll let you figure out how to print the divisor :)
(For reference, correctly using the flag would look like this -- and for clarity I removed the 'feature' in which it continuously looped)
#include <stdio.h>
int main()
{
int num, i;
int isPrime = 1;
printf("Number [1-100]:? \n");
scanf("%d", &num);
for(i=2; i<=num/2; ++i)
{
if (num%i==0)
{
isPrime = 0;
break;
}
}
if (isPrime == 1)
{
printf("Prime\n");
} else
{
printf("Non-prime.");
}
printf("Done\n");
}
The reason why 3 is behaving differently is that the for logic never reaches 3. For "(i=2; i <= num/2; ++i)", if num equals 3, then the i (being 2) is no longer less than 3/2, which is 1 (after rounding off). So, you should add "num==3" check to the "if (num==1||num==2)".
You're not checking the entire range between 2 and num/2. You need a while loop and a prime flag.
Something like this.
while(num>0 && num <101)
{
unsigned char prime = 1; // set prime flag
i = 2;
while( i < (num/2)+1)
{
if(num%i == 0)
prime = 0;
i++;
}
if(num == 1)
prime = 0;
if(prime == 0)
printf("%d is nonprime\n", num);
else
printf("%d is prime\n", num);
prime = 1;
printf("Number[1-100]:? \n");
scanf("%d",&num);
}

everything compiles but i dont get any kind of feed back as if the main is not calling the functions does anyone know why? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have tested each part individually and i know they work the problem is when i paste the function roll_dice back in or vise verse.When i compile the two together i get the error C2143 missing ';' before 'type' on line 32 which is the play_game function. can someone tell me why im getting this error when they work separately but do not work when they are put together. This is a craps game and i am a beginner at using c.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_DIE 6
#define true 1
#define false 0
int num1 = 0;
int num2 = 0;
int roll = 0;
int point = 0;
int wins = 0;
int losses = 0;
int keep_rolling = 1;
int main(void)
{
void roll_dice(int num1, int num2, int roll);
{
srand(time(NULL ));
num1 = ("%d", rand() % MAX_DIE + 1);
num2 = ("%d", rand() % MAX_DIE + 1);
roll = (num1 + num2);
printf("this is your number : %d\n", num1);
printf("this is your 2nd number : %d\n", num2);
printf("this is your total : %d\n", roll);
}
void play_game(int wins, int losses, int point, int roll);
{
if ((roll == 7) || (roll == 11))
{
printf("you rolled %d you won \n", roll);
wins += 1;
}
else if ((roll == 2) || (roll == 3) || (roll == 12))
{
printf("you rolled %d you lost \n", roll);
losses += 1;
}
else if ((roll == 1) || (roll == 4) || (roll == 5) || (roll == 6)
|| (roll == 8) || (roll == 9) || (roll == 10))
{
printf("you have pointed : %d\n", roll);
point = roll;
printf("you rolled %d you pointed \n", roll);
while (keep_rolling = 1)
{
void roll_dice(int num1, int num2, int roll);
if (roll == point)
{
printf("you rolled %d you won \n", roll);
wins += 1;
return keep_rolling = false;
}
else if (roll == 7)
{
printf("you rolled %d you lost \n", roll);
losses += 1;
return keep_rolling = false;
}
else
{
printf("you rolled : %d\n", roll);
printf("your point is : %d\n", point);
}
}
}
}
}
Define functions outside main() function and call functions from main()
You should not define functions inside main().
You have added semicolons while defining them. remove semicolons at the end.
Also declare functions above main()
Read functions Here Here and Here
void roll_dice(int num1, int num2, int roll);
You can not define function inside an function. It compiled fine in the above case because it is a declaration.

Resources