Monday, 20 February 2017
On 04:16 by Vardan Kumar in C tutorial No comments
Addition,Subtraction,Multiplication by 2 and division by 2 without using +,-,* and / in c
When it comes to enhance the software development skills, the magic lies in logic building. So here we are going to discuss a program with can add two numbers,subtract two numbers,multiply by 2 and division by 2,seems simple but here's a twist we can not use +,-,* and / operators for the same.
- Addition:
For the pupose of addition of two numbers off course not by using + operator, we will use ++(increment) as well as --(decrement) operator. Simple! isn't it?. And for the logic we will just decrement one of the number till it gets 0(in a loop) and increment the another one. Let us review the function definition for the same.
int add(int inum1,int inum2)
{
while(inum2--)
{
inum1++;
}
return inum1;
}
2. Subtraction:
Similarly for subtraction we'll use the combination of increment(++) as well as decrement(--) operators since we can not use '-' operator. And for the logic we'll just decrement onre of the number till it gets '0' and decrement the another one. Let us review function definition for the same.
int subtract(int inum1,int inum2)
{
while(inum2--)
{
--inum1;
}
return inum1;
}
int subtract(int inum1,int inum2)
{
while(inum2--)
{
--inum1;
}
return inum1;
}
3. Multiplication By 2:
For the purpose of multiplication by 2 we'll use already built addition function. Got the strike, yeah we will pass the number we want to multiply with '2' as arguments while calling "add" function and store the return value of add function in a pocket variable. Let us review the function definition for the same.
int mulby2(int inum)
{
int pocket;
pocket=add(inum,inum);
return pocket;
}
4. Division By 2:
For the purpose of division by 2 we'll use bitwise operators, yes we got it right, the right shift operator(>>), we'll just right shift the number by '1' we want to divide with 2, hence we'll get the quotient for the division and if we want to get the remainder, off course we know we can use modulus operator(%)
int divby2(int inum)
{
inum = inum>>1;
return inum;
}
Source Code:
#include<stdio.h>
int add(int inum1,int inum2)
{
while(inum2--)
{
inum1++;
}
return inum1;
}
int subtract(int inum1,int inum2)
{
while(inum2--)
{
--inum1;
}
return inum1;
}
int mulby2(int inum)
{
int pocket;
pocket=add(inum,inum);
return pocket;
}
int divby2(int inum)
{
inum = inum>>1;
return inum;
}
void main()
{
int num1,num2,num,ap,as,am,ad;
printf("Enter the two numbers for addition and subtraction:");
scanf("%d %d",&num1,&num2);
printf("\nEnter number for mul and div by 2:");
scanf("%d",&num);
ap=add(num1,num2);
as=subtract(num1,num2);
am=mulby2(num);
ad=divby2(num);
printf("\nResult of addition is:%d",ap);
printf("\nResult of subtraction is:%d",as);
printf("\nResult of multiplication by 2 is:%d",am);
printf("\nquotient of division by 2 is:%d and remainder is %d",ad,num%2);
}
Subscribe to:
Post Comments (Atom)
Search
Popular Posts
-
Troubleshooting Cisco VPN client Before starting troubleshooting, Let us see what VPN is and what it requires to perform its intended f...
-
Evolution-Mobile Phones With the development of portable technology,wireless communication has so evolved that (According to the announce...
-
File Versioning C# File versioning, saving file with unique file name in c# File versioning allows a user to have several versions of ...
-
Text Box Hint in c# Windows Form Application Text Box Hint in c# Windows Form Application While developing a windows form applicat...
-
Unable to set the Freeze Panes property of Window Class C# It is generally easy to resolve the compile time errors because the reason fo...
0 comments:
Post a Comment