Swap number bug - c

I wrote a simple program that accepts 2 number to be swap in swapNum function however, it returns some numbers I don't understand. Can someone explain how this happened and suggests better.
#include <stdio.h>
int swapNum(int num1, int num2){
int tempNumber;
tempNumber = num1;
num1 = num2;
num2 = tempNumber;
return num1;
return num2;
}
int main(){
int firstNum, secondNum;
printf("Enter the first number:");
scanf("%i", &firstNum);
printf("Enter the second number:");
scanf("%i", &secondNum);
printf("Swapped value: first number: %i, second number: %i", swapNum(firstNum,secondNum));
return 0;
}
Here is the image of the result. The bug is at the last line at second number.

As others have pointed out, you can only have one return statement from a function and thus only return on parameter (although it can be a structure).
In this instance I would pass the integer values into the function by using integer pointers and passing their address e.g.:
int swapNum(int *num1, int *num2);
int main()
{
int a = 2;
int b = 3;
printf("a=%d b=%d\n",a,b);
swapNum(&a, &b);
printf("a=%d b=%d\n",a,b);
return 0;
}
int swapNum(int *num1, int *num2)
{
int tempNumber;
tempNumber = *num1;
*num1 = *num2;
*num2 = tempNumber;
return 0;
}
Output
a=2 b=3
a=3 b=2

Related

WSL(ubuntu) printf only outputs the same text

I have a problem. My code:
#include <stdio.h>
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
printf("variable 2 :");
scanf("%d", &y);
return y;
}
int compare (int, int);
int main() {
int x = readInteger(x);
int y = readInteger(y);
printf("%d is greater", compare(x,y));
return 0;
}
int compare(int x , int y) {
if(x > y) return x;
else return y;
}
only outputs
variable 1: ...
variable 1: ...
instead of
variable 1: ...
variable 2: ...
The issue is because return means "end function execution here". So, you have
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
printf("variable 2 :");
scanf("%d", &y);
return y;
}
which, because of the return x line, is equivalent to
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
}
One quick fix is to remove return x; line. Though then it won't do exactly what you want the function to do, since you want to read 2 integers and return them.
A better fix is to pass the string to the function as argument and then call it twice:
int readInteger(const char* prompt) {
int x;
printf(prompt);
scanf("%d", &x);
return x;
}
int main() {
int x = readInteger("variable 1 :");
int y = readInteger("variable 2 :");
printf("%d is greater", compare(x,y));
return 0;
}
PS. If a function has no arguments (readInteger in your original code, you should not call it with arguments (int x = readInteger(x) in main).

Getting started on functions need help inputting numbers (beginner)

I was wondering how I can input the numbers using a function with the code written below, and a bit stuck on how I can input and give it an output I am just starting out on functions level 0 at it basically.
int addTwoInt(int a, int b);
int main(void)
{
printf("Enter a number: ");
scanf("%d", &addTwoInt(<#int a#>, <#int b#>));
// printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
return sum;
printf("The sum of the numbers are %d", sum);
}
int addTwoInt(int a, int b);
int main(void)
{
int x;
int y;
printf("Enter a number: ");
scanf("%d", &x);
scanf("%d", &y);
int z = addTwoInt(x, y);
printf("%d", z);
//printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
printf("The sum of the numbers are %d", sum);
return sum;
}
You asked for cleaner way to add two numbers or other arithmetic operations u can simply do it in return statement just like this:
int addTwoInts(int a, int b){
return a+b
}

Print Menu Function Keeps Printing After Selection

I was supposed to make a program that would be a calculator using functions to provide results. I got it down and everything is working but I'm having a slight issue with the printing function out of everything which is humorous; for some reason that I can't seem to get my head around whenever the program prints a solution it also prints the first selection which is add(num1, num2) and I can't for the life of me figure out why. Any input would be greatly appreciated! Thank you! Here is my code:
//Function to Add
int add (int a, int b);
//Function to Substract
int sub (int a, int b);
//Function to Multiply
int mul (int a, int b);
//Function to Divide
int div (int a, int b);
//Function to get remainder
int rem (int a, int b);
//Function to print menu
int printmenu();
//Begin main
int main()
{
//Initialize Variables
int num1=0;
int num2=0;
unsigned int selection =0;
//Loop
while ((selection=printmenu()) != 6)
{
switch (selection)
{
//Addition Case
case 1:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d added to %d is %d",num1,num2,add(num1,num2));
break;
case 2:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d substracted to %d is %d",num1,num2,sub(num1,num2));
break;
case 3:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d multiplied to %d is %d",num1,num2,mul(num1,num2));
break;
case 4:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d divided to %d is %d",num1,num2,div(num1,num2));
break;
case 5:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d divided to %d has a remainder of %d",num1,num2,rem(num1,num2));
break;
case 6:
return 0;
default:
printf("That is not a valid choice.");
break;
}//End switch
}//End while
return 0;
}//End main
//Function to add
int add (int a, int b)
{
int total=0;
total = a + b;
return total;
}
//Function to substract
int sub (int a, int b)
{
int total=0;
total = a - b;
return total;
}
//Function to multiply
int mul (int a, int b)
{
int total=0;
total = a * b;
return total;
}
//Function to Divide
int div (int a, int b)
{
int total=0;
total = a / b;
return total;
}
//Function to get Remainder
int rem (int a, int b)
{
int total=0;
total = a % b;
return total;
}
//Function to Print Menu
int printmenu()
{
int selection=0;
//Print menu
printf("1.Add\n");
printf("2.Substract\n");
printf("3.Multiply\n");
printf("4.Divide\n");
printf("5.Remainder\n");
printf("6.Exit\n");
printf("Selection: ");
scanf("%d", &selection);
return selection;
}
When you print the result of the operation, you aren't including a newline at the end of the output. So when you print the menu, the first line of the menu gets printed on the same.
Add \n at the end of each result printing statement, ex.:
printf("%d added to %d is %d\n",num1,num2,add(num1,num2));

LCM And GCD in C using recursion or normal method

While trying to do the GCD and LCM program from programming simplified...I am facing problems with the results. I did everything correct(according to me) and even checked word by word but the problem still persists...I am pasting the code of normal method only.
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y)/gcd;
printf("Greatest common divisior of %d and %d = %d\n", x, y, gcd);
printf("Lowest common divisior of %d and %d = %d\n", x, y, lcm);
getch();
}
At least this part is fundamentally wrong:
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
So you're declaring x and y uninitialized, then you're assigning them to a and b - now a and b don't contain the values the user entered, but some garbage. You probably want
x = a;
y = b;
instead.
Better try this. This is simpler to run.
#include<stdio.h>
int GCD(int,int);
void main(){
int p,q;
printf("Enter the two numbers: ");
scanf("%d %d",&p,&q);
printf("\nThe GCD is %d",GCD(p,q));
printf("\nThe LCM is %d",(p*q)/(GCD(p,q)));
}
int GCD(int x,int y){
if(x<y)
GCD(y,x);
if(x%y==0)
return y;
else{
GCD(y,x%y);
}
}
Try it
#include<stdio.h>
int main(){
int a,b,lcm,gcd;
printf("enter two value:\n");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
lcm=LCM(a,b);
printf("LCM=%d and GCD=%d",lcm,gcd);
return 0;
}
int GCD(int a, int b){
while(a!=b){
if(a>b){
a=a-b;
}else{
b=b-a;
}
}
return a;
}
int LCM(int a, int b){
return (a*b)/GCD(a,b);
}

Adding multiple integers using arguments in function

I am learning function in C. I want to sum multiple integers using arguments in function. I managed to write a code for adding two integers, but how if I want to add multiple integers and print the total of them? please guide me. Code which i wrote is;
#include<stdio.h>
#include<conio.h>
int sum(int a, int b, int c);
int main (void){
int x,y,z;
clrscr();
printf("Enter first integer to add.\n");
scanf("%d",&x);
printf("Enter second integer to add.\n");
scanf("%d",&y);
sum(x, y, z);
printf("Total = %d.\n",sum(x, y, z));
getch();
return 0;
}
int sum (int a, int b, int c){
c=a+b;
return c;
}
You can do something like this.
sum = 0;
while (ch == "y")
{
scanf("%d", &a);
sum+=a;
printf("Do you want to continue: ");
scanf("%c\n", &ch);
}
printf("%d", sum);
The idea is to have a variable sum whose initial value is 0.
Have a while loop that takes a integer a as input & add it to sum.
You can mantain a variable ch, which can be used to exit out of the loop. Only if the user enters "y", the user will be asked for integer again.
try this !
int main()
{
int var[100];
int count = 5;
printf("enter number %d number ", count);
for( int i = 0; i < count; i++ )
{
scanf( "%d", &var[i] );
}
printf("sum=%d", sum(var, count) );
return 0;
}
int sum( int var[], int count )
{
int sum = 0;
for( int i = 0; i < count; i++ )
{
sum += var[i];
}
return sum;
}
Currently you're overwriting the third argument to the function with the sum of the first two and return it. This should probably change a bit.
Just think about how you'd write a sum of three numbers in mathematics and you should see the solution.

Resources