public class calculation { Public integer myInputQueryString { get; set; } Public string myoutputString { get; set; } string val = ''; Public void myInputQuery() { if (myInputQueryString != null) { system.debug('not null');
for (integer i = 0; i < myInputQueryString; i++) {
if (math.mod(i, 2) == 0) { system.debug('this is even number: ' + i);
val = val + string.valueOf(i) + ', '; myoutputString = val;
}
} myInputQueryString = 0; } }
Public void myOddNumbers() { if (myInputQueryString != null) { system.debug('not null');
for (integer i = 0; i < myInputQueryString; i++) {
if (math.mod(i, 2) != 0) { system.debug('this is even number: ' + i);
val = val + string.valueOf(i) + ', '; myoutputString = val;
}
} myInputQueryString = 0; } } Public void myPrimeNumbers() {
if (myInputQueryString != null) { system.debug('not null');
for (integer i = 0; i < myInputQueryString; i++) { integer count = 0; for (integer j = i; j >= 1; j--) {
if (math.mod(i, j) == 0) { count++; } } if (count == 2 || count == 1) {
system.debug(i); val = val + string.valueOf(i) + ', '; myoutputString = val;
Check once below links :
https://success.salesforce.com/answers?id=9063A000000iRYGQA2
https://developer.salesforce.com/forums/?id=906F00000005J7fIAE
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Hi Manogna,
if you find this helpful mark it as the best answer.
public class PrimeNumber {
static integer remainder;
static boolean isPrime=true;
public static void func(Integer num){
for(integer i=2;i<=num/2;i++)
{
remainder=math.mod(num,i);
System.debug(num+ 'Divided by'+ i + 'gives a remainder'+remainder);
//if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
if(remainder==0)
{
isPrime=false;
break;
}
}
if(isPrime)
{
System.debug(num + 'is a Prime numberToCheckber');
}
else
{
System.debug(num + 'is not a Prime numberToCheckber');
}
}
}
public class calculation {
Public integer myInputQueryString {
get;
set;
}
Public string myoutputString {
get;
set;
}
string val = '';
Public void myInputQuery() {
if (myInputQueryString != null) {
system.debug('not null');
for (integer i = 0; i < myInputQueryString; i++) {
if (math.mod(i, 2) == 0) {
system.debug('this is even number: ' + i);
val = val + string.valueOf(i) + ', ';
myoutputString = val;
}
}
myInputQueryString = 0;
}
}
Public void myOddNumbers() {
if (myInputQueryString != null) {
system.debug('not null');
for (integer i = 0; i < myInputQueryString; i++) {
if (math.mod(i, 2) != 0) {
system.debug('this is even number: ' + i);
val = val + string.valueOf(i) + ', ';
myoutputString = val;
}
}
myInputQueryString = 0;
}
}
Public void myPrimeNumbers() {
if (myInputQueryString != null) {
system.debug('not null');
for (integer i = 0; i < myInputQueryString; i++) {
integer count = 0;
for (integer j = i; j >= 1; j--) {
if (math.mod(i, j) == 0) {
count++;
}
}
if (count == 2 || count == 1) {
system.debug(i);
val = val + string.valueOf(i) + ', ';
myoutputString = val;
}
}
}
}
}
VF Page
<apex:page controller="calculation" id="vfPageId">
<apex:form id="formId">
<script>
function CheckNull() {
//alert('ff');
debugger;
var dd = document.getElementById("vfPageId:formId:inputnum1").value;
if (dd == '') {
alert('Please enter number');
}
}
</script> Input Number:
<apex:inputText value="{!myInputQueryString}" id="inputnum1" />
<apex:pageblock id="evenPBId">
<apex:commandButton value="Even Numbers" reRender="DisplayEvenID" action="{!myInputQuery}" onClick="CheckNull();" />
<b>Even Numbers are: </b>
<apex:outputText value="{!myoutputString}" id="DisplayEvenID" />
<br />
<apex:commandButton value="Odd Numbers" reRender="DisplayOddID" action="{!myOddNumbers}" onClick="CheckNull();" />
<b>Odd Numbers are: </b>
<apex:outputText value="{!myoutputString}" id="DisplayOddID" />
<br />
<apex:commandButton value="Prime Numbers" reRender="DisplayPrimeID" action="{!myPrimeNumbers}" onClick="CheckNull();" />
<b>Prime Numbers are: </b>
<apex:outputText value="{!myoutputString}" id="DisplayPrimeID" />
</apex:pageblock>
</apex:form>
</apex:page>
why have you used n/2 when we can do without that