Answer to Question #301546 in C for Sneha

Question #301546

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)

1
Expert's answer
2022-02-23T06:24:17-0500


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS