You need to sign in to do that
Don't have an account?
Purushottam Dank 1
Hello everyone , my question is related to apex
hello everyone, i just started journey with salesforce ,
i want to do following programms in apex , help me to solve....
Factorial program
program to find HCF and LCM
program to convert decimal to binary
program to find nCr and nPr
program to add n numbers
Swap numbers program
program to reverse a number
palindrome number program.
i want to do following programms in apex , help me to solve....
Factorial program
program to find HCF and LCM
program to convert decimal to binary
program to find nCr and nPr
program to add n numbers
Swap numbers program
program to reverse a number
palindrome number program.
Type the code in developer console and exceute it
Factorial program:
public class sample
{
public void fact(Integer n)
{
long ans = 1;
if (n < 0) {
System.debug('No negative numbers');
}
else
{
for (Integer i = 1; i <= n; i++) {
ans *= i;
}
}
System.debug(ans);
}
}
Press Ctrl+E and Type it
sample s=new sample();
s.fact(5);
program to find HCF and LCM
public class sample {
public void f(Integer x,Integer y)
{
Integer a, b,t, gcd, lcm;
System.debug('x:'+x+'y:'+y);
a = x;
b = y;
while (b != 0) {
t = b;
b = math.mod(a,b);
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
System.debug(gcd);
System.debug(lcm);
}
}
Press Ctrl+E and Type it
sample s=new sample();
s.f(5,6);
program to convert decimal to binary
public class sample {
public void f(Integer n)
{
integer[] binaryNum = new integer[1000];
integer i = 0;
while (n > 0)
{
binaryNum[i] = math.mod(n , 2);
n = n / 2;
i++;
}
for (integer j = i - 1; j >= 0; j--)
System.debug(binaryNum[j]);
}
}
Press Ctrl+E and Type it
sample s=new sample();
integer n = 17;
s.f(n);
program to find nCr and nPr
public class sample {
public void f(Integer n,Integer r)
{
Integer ncr, npr;
ncr = find_ncr(n, r);
npr = find_npr(n, r);
System.debug('n:'+n+'r:'+r+'ncr'+ncr);
System.debug('n:'+n+'r:'+r+'ncr'+npr);
}
public static Integer find_ncr( Integer n, Integer r) {
Integer result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
public static Integer find_npr( Integer n, Integer r) {
Integer result;
result = factorial(n)/factorial(n-r);
return result;
}
public static Integer factorial( Integer n) {
Integer c;
Integer result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
}
Press Ctrl+E and Type it
sample s=new sample();
s.f(7,8);
Please mark it as solved if my reply was helpful.
Thanks,
Jenefa
Sweet Potato Tec
All Answers
If you want to make an application/UI then you need to create visualforce page for that, if you want to just write code and exceute then you can run code in execute anonymous under debug tab in developer console.
Let me know if you have any issue.
Regards
Yogesh
I would suggest you search for the logic and implement it with apex syntax, in this way you can even learn basics of programming in apex.
Below is the link which can help you to start.
https://webkul.com/blog/apex-programming-basics/
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Sandhya
Type the code in developer console and exceute it
Factorial program:
public class sample
{
public void fact(Integer n)
{
long ans = 1;
if (n < 0) {
System.debug('No negative numbers');
}
else
{
for (Integer i = 1; i <= n; i++) {
ans *= i;
}
}
System.debug(ans);
}
}
Press Ctrl+E and Type it
sample s=new sample();
s.fact(5);
program to find HCF and LCM
public class sample {
public void f(Integer x,Integer y)
{
Integer a, b,t, gcd, lcm;
System.debug('x:'+x+'y:'+y);
a = x;
b = y;
while (b != 0) {
t = b;
b = math.mod(a,b);
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
System.debug(gcd);
System.debug(lcm);
}
}
Press Ctrl+E and Type it
sample s=new sample();
s.f(5,6);
program to convert decimal to binary
public class sample {
public void f(Integer n)
{
integer[] binaryNum = new integer[1000];
integer i = 0;
while (n > 0)
{
binaryNum[i] = math.mod(n , 2);
n = n / 2;
i++;
}
for (integer j = i - 1; j >= 0; j--)
System.debug(binaryNum[j]);
}
}
Press Ctrl+E and Type it
sample s=new sample();
integer n = 17;
s.f(n);
program to find nCr and nPr
public class sample {
public void f(Integer n,Integer r)
{
Integer ncr, npr;
ncr = find_ncr(n, r);
npr = find_npr(n, r);
System.debug('n:'+n+'r:'+r+'ncr'+ncr);
System.debug('n:'+n+'r:'+r+'ncr'+npr);
}
public static Integer find_ncr( Integer n, Integer r) {
Integer result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
public static Integer find_npr( Integer n, Integer r) {
Integer result;
result = factorial(n)/factorial(n-r);
return result;
}
public static Integer factorial( Integer n) {
Integer c;
Integer result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
}
Press Ctrl+E and Type it
sample s=new sample();
s.f(7,8);
Please mark it as solved if my reply was helpful.
Thanks,
Jenefa
Sweet Potato Tec