A company is offering a special discount to its customers based on an algorithm. Two range values are fed to the algorithm and in return t will calculate the discount to offer to the customers. The discount is calculated as the sum of all the prime numbers within the defined range including the range values if the range values are the prime numbers
Write an algorithm to find the special discount given to the customers
Input
The first line of the input consists of
an integer-rangelet remeng
the minimum boundary wabererte
given range finding the p
values)
#include<stdio.h>
#include<math.h>
int isPrime(int regNumber){
int i;
if (regNumber < 2){
return 0;
}
if (regNumber == 2 || regNumber == 3){
return 1;
}
for (i = 2; i <= sqrt(regNumber * 1.0); i++){
if (regNumber % i == 0){
return 0;
}
}
return 1;
}
int main(){
int number1;
int number2;
int i;
int p=0;
printf("Enter integer 1: ");
scanf("%d",&number1);
printf("Enter integer 2: ");
scanf("%d",&number2);
for(i=number1;i<=number2;i++){
if(isPrime(i)){
p+=i;
}
}
printf("Discount: %d\n\n",p);
scanf("%d",&p);
return 0;
}
Comments
Leave a comment