Accept any two numbers from the user.
Perform the following operations on it using functions:
1. Addition
2. Subtraction
3. Multiplication
4. Swap the values
5. Print both the numbers in reverse order for example (if
a given number is 234, it should
be printed as 432)
Answer Posted / neeru bansal
/*Question 2 . Accept any two numbers from the user.
Perform the following operations on it using functions:
1. Addition
2. Subtraction
3. Multiplication
4. Swap the values
5. Print both the numbers in reverse order for example (if a
given number is 234, it should
be printed as 432)
CONCEPT : Using SWITCH statement and functions
SYNTAX : switch
{
case 1: statements;
Break;
case 2: statements;
Break;
…
default: statements;
}
*/
// Declaration for the preprocessor::
#include
#include
// Main function begins::
void main()
{
// Declaration of variables::
int a,b,c,t,n,num;
long int i;
int l;
char ch;
// Declaration of functions::
char num_status(int);
void sum(int,int);
void sub(int,int);
void mul(int,int);
void swap(int,int);
int reverse(int);
// To clear the screen before printing ::
clrscr();
// Printing SCDL student details for code-authentication::
printf(“\n***************************************\n NAME :
PRIYANKA”);
printf(“\n COURSE: PGDIT [1st Sem.]“);
printf(“\n REGISTRATION NUMBER: 200705826″);
printf(“\n SUBJECT: Assignment for C Programming
\n***************************************”);
// printing the directions for user to select::
printf(“\n\nPerform the following operations:\n”);
printf(“\n1.Addition:\n”);
printf(“\n2.Subtration:\n”);
printf(“\n3.Multiplication:\n”);
printf(“\n4.Swap the values:\n”);
printf(“\n5.Print the numbers in reverse order:”);
printf(“\n\nSlect your operation:\n”);
scanf(“%d”,&l);// accepts the operation choice
ch=num_status(l);// calling function for selectiong choice
//switch statement for the calculation
switch(ch)
{
case ’1′:
printf(“Enter the first number:\t”);
scanf(“%d”,&a);
printf(“Enter the second number:\t”);
scanf(“%d”,&b);
sum(a,b);
break;
case ’2′:
printf(“Enter the first number:\t”);
scanf(“%d”,&a);
printf(“Enter the second number:\t”);
scanf(“%d”,&b);
sub(a,b);
break;
case ’3′:
printf(“Enter the first number:\t”);
scanf(“%d”,&a);
printf(“Enter the second number:\t”);
scanf(“%d”,&b);
mul(a,b);
break;
case ’4′:
printf(“Enter the first number:\t”);
scanf(“%d”,&a);
printf(“Enter the second number:\t”);
scanf(“%d”,&b);
swap(a,b);
break;
case ’5′:
printf(“Enter the first number:\t”);
scanf(“%d”,&a);
printf(“The reverse of %d is %d”,a,reverse(a));
printf(“\nEnter the second number:\t”);
scanf(“%d”,&b);
printf(“The reverse of %d is %d”,b,reverse(b));
break;
}
getch();
}
// Function definition for selecting choice::
char num_status(int l)
{
if(l==1)
return ’1′;
else if(l==2)
return ’2′;
else if(l==3)
return ’3′;
else if(l==4)
return ’4′;
else
return ’5′;
}
// Function definition for addition of 2 numbers::
void sum(int a,int b)
{
int c;
c=a+b;
printf(“Addition of 2 numbers:\n a=%d\t b=%d\n c=%d\n”,a,b,c);
}
// Function definition for subtraction of 2 numbers::
void sub(int a,int b)
{
int c;
c=a-b;
printf(“Subtraction of 2 numbers:\n a=%d\t b=%d\n
c=%d\n”,a,b,c);
}
// Function definition for multiplication of 2 numbers::
void mul(int a,int b)
{
int c;
c=a*b;
printf(“Multiplication of 2 numbers:\n a=%d\n b=%d\n
c=%d\n\t\t “,a,b,c);
}
// Function definition for swapping of 2 numbers::
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf(“After swapping 2numbers:\n a=%d\n b=%d\n”,a,b,t);
}
// Function definition for reversing 2 numbers::
int reverse(int n)
{
int r,rev;
rev=0;
while(n!=0)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
return rev;
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
what is the largest prime number in 8 digit number?
Find the value of @@+25-++@16, where @ denotes "square" and + denotes "square root".
The average of 13 papers is 40. The average of the first 7 papers is 42 and of the last seven papers is 35. Find the marks obtained in the 7th paper?
If D is adjacent to F then who is adjacent to C?
No of financial emergency announced by president
A person travels 12 km in the southward direction and then travels 5km to the right and then travels 15km toward the right and finally travels 5km towards the east, how far is he from his starting place?
a man is 24 years older than his son. In two years, his age will be twice the age of his son. The present age of his son is?
What is the error in the given sequence. 2,3 6,7 14,15 29,46.
There is a lucky draw held every day. If there is a winning number eg. 1876, then all possible numbers like 1867 ,1687,1768 etc are the numbers that match irrespective of the position of the digits. Thus all these numbers qualify fr the lucky draw prize. Assume there is no zero digit in any numbers. Write a program to show all the possible winning numbers if a "winningnumber" is passed as an argument to the function. i want a solution in c/c++ language
The average salary of Ram, Shyam and Sundar is 95 Rs. per week. If Ram earns Rs.115 and Shyam earns Rs.65.What is the salary of Sundar.
The square of a two digit number is divided by half the number. After 36 is added to the quotient, this sum is then divided by 2. The digits of the resulting number are the same as those in the original number, but they are in reverse order. The ten's place of the original number is equal to twice the difference between its digits. What is the number?
The difference between two numbers is 1365. When you divide the larger number with the smaller number the quotient is 6 and remainder is 15. What is the smaller number?
What is the max possible 3 digit prime number?
Distance d=rt where r & t are positive and r is constant,as t increases then d_________
Complete the series 2, 7, 24, 77,__ (hint: 2*12= 24, 7*11= 77, therefore 24*10= 240)