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
}
Related
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));
I'm starting to learn the c programming language and I have to write a program that subtracts two greatest numbers out of the three entered numbers. Can anyone help me with this?
Edit: Sorry, I still don't know how this site functions...
I know how to find the greatest number, but I'm not sure how to find the other one.
#include<stdio.h>
int main()
{
int a, b, c, d, max;
printf("Enter three numbers: ");
scanf("%d%d%d%d", &a, &b, &c);
max=a;
if (max<b) {max=b;}
if (max<c) {max=c;}
printf("the greatest number is %d\n", max);
return 0;
}
Don't expect to get answers to questions like yours. Try this. The assumption is that you are dealing with positive integers only. If you need to consider negative integers as well, you can do it yourself. You should note that this is the not the best solution, there can be much more elegant ones.
#include <stdio.h>
int main() {
int numbers[3];
printf("Enter number 1: \n");
scanf("%d", &numbers[0]);
printf("Enter number 2; \n");
scanf("%d", &numbers[1]);
printf("Enter number 3: \n");
scanf("%d", &numbers[2]);
printf("%d %d %d\n", numbers[0], numbers[1], numbers[2]);
int maximum_0 = 0;
int maximum_1 = 0;
int i;
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_0) {
maximum_0 = numbers[i];
}
}
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_1 && numbers[i] < maximum_0) {
maximum_1 = numbers[i];
}
}
printf("Result: %d\n", (maximum_0 - maximum_1));
return 0;
}
//Here it is since you are beginner Without loops
#include<stdio.h>
int main()
{
int a, b, c, d, max,min,result;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if(a>b)
{
if(a>c)
{
max=a;
if(c>b)
min=c;
else
min=b;
}
else
{
max=c;
a=min;
}
}
else
{
if(b>c)
{
max=b;
if(a>c)
min=a;
else
min=c;
}
else
{
max=c;
min=b;
}
}
result=max-min;
printf("the greatest number is %d\n", result);
return 0;
}
Use an array and then use qsort to sort your values. The code below will do this for you. The order of the array will be such that the first element will have your smallest number and the last element your largest.
#include<stdio.h>
void sort(const void* d1, const void* d2)
{
int a = *(int*)d1;
int b = *(int*)d2;
if (a > b)return 1;
if (b > a)return -1;
return 0;
}
int main()
{
int abc[3];
printf("Enter three numbers: ");
scanf("%d%d%d", &abc[0], &abc[1], &abc[2]);
qsort(abc, 3, sizeof(int), sort);
printf("%d\n", abc[2] - abc[1]);
while (1){}
return 0;
}
The question consists of two numbers, a and b, and the answer to it is the sum of digits of a^b.
I have written the below code. It is giving correct result in all cases. But when the input is as such a < b, then after giving the correct answer, I am getting segmentation fault.
I tried a lot to debug it but could not identify the issue. Any help would be greatly appreciated.
Thanks in advance..!
#include<stdio.h>
void power (int, int, int *product);
int main()
{
int a,b,product[200];
scanf("%d %d",&a, &b);
power(a,b,product);
return 0;
}
void power(int a, int b, int *product)
{
int i,m,j,x,temp,sum=0;
int *p = product;
*(p+0)=1; //initializes array with only 1 digit, the digit 1
m=1; // initializes digit counter
temp=0; //Initializes carry variable to 0.
for(i=1;i<=b;i++)
{
for(j=0;j<m;j++)
{
x = (*(p+j))*a+temp;
*(p+j)=x%10;
temp = x/10;
}
while(temp>0) //while loop that will store the carry value on array.
{
*(p+m)=temp%10;
temp = temp/10;
m++;
}
}
//Printing result
for(i=m-1;i>=0;i--)
sum = sum + *(p+i);
printf("\n%d",sum);
printf("\n");
}
I hope the below code does what you are trying to do. Which is simple and looks good too.
#include<stdio.h>
void power (int, int);
int main()
{
int a,b;
scanf("%d %d",&a, &b);
power(a,b);
return 0;
}
void power(int a, int b)
{
int c=1,sum=0;
while(b>0)
{
c = c*a;
b--;
}
printf("%d\n",c);
while(c!=0)
{
sum = sum+(c%10);
c =c/10;
}
printf("%d\n",sum);
}
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);
}
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.