Q. If sum of two integer is 14 and if one of them is 8 then find other integer. Ans: Well, this is very simple. Simply, one integer is 8 and sum of two integers is 14. So, we need to subtract the number 8 from their sum of 14 to get the second number. So, the second number is: 14 - 8 = 6 C-Program of this task Code---> #include<stdio.h> int main () { int num1 = 8 , num2 , sum = 14 ; num2 = sum - num1 ; printf ( "The second number is: %d" , num2 ); return 0 ; }
In this C-program we will calculate the LCM of two numbers. This is the easiest way to calculate LCM for both Negative and Positive numbers. The numbers will be taken from the user.
input:
The numbers.(i.e : 20,25,40 etc.)output:
The LCM of two numbers given by the user.
CODE---->
#include<stdio.h>
#include<conio.h>
main()
{
int copy_num_1,copy_num_2,num_1,num_2,gcd,lcm=0,i ;
printf("Please, Enter two numbers : ");
scanf("%d%d",&num_1,&num_2);
copy_num_1=num_1;
copy_num_2=num_2;
//copying numbers , (entered by user) for future use.
if(num_1<0)
num_1=(num_1)*(-1);
if(num_2<0)
num_2=(num_2)*(-1);
//here, we are converting negative numbers(if any, entered by user), so that, we can calculate the lcm for both positive and negative numbers.
for(i=1;i<=num_1 && i<=num_2;i++)
{
if(num_1%i==0 && num_2%i==0)
gcd=i;
}
lcm=num_1*num_2/gcd;
//relation between lcm and gcd. so, we can get the lcm using this formula.
printf("\nThe LCM of %d and %d is : %d",copy_num_1,copy_num_2,lcm);
getch();
}
Comments
Post a Comment