Question
write the code in C please

4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or no
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1

#include<stdio.h>

#include<math.h>


void perfect_Square(int a, int b) // function for perfect square

{

int count= 0; // Initialize result

for (int i = a; i <= b; i++) // for loop to check each number between a and b

{

// Check if current number 'i' is perfect square

for (int j = 1; j * j <= i; j++)

if (j * j == i){

count++; // count increase by 1

printf("%d \t" , i); // displays the output}

}

}

// Driver code

int main()

{

int a , b;

scanf("%d" , &a); // Enter the first no.

scanf("%d" , &b); // Enter the second no.

perfect_Square(a, b); // calling function

return 0;

}

Answer 2:

#include <stdio.h>
void is_Prime(int number){   // function to check if the number is prime
 int flag = 0; 

for(int i = 2; i <= number/2; ++i)
    {
        // condition for nonprime number
        if(number%i == 0)
        {
            flag = 1;
            break;
        }
    }

    if (number == 1) 
    {
      printf("1 is neither a prime nor a composite number.");
    }
    else 
    {
        if (flag == 0)
          printf("%d is a prime number.", number);
        else
          printf("%d is not a prime number.", number);
    }
}
 // driver code
int main()
{
  int i, number; 
  
scanf("%d", &number);
  is_Prime(number); // calling function
  return 0;
}


TO PRINT THE PRIME NOS. BETWEEN 2 TO 100

#include <stdio.h>
 
int main()
{
  int i, Number, count; // declaration and definition of the variables used
  
  printf(" Prime Number from 1 to 100 are: \n"); 
  for(Number = 1; Number <= 100; Number++)
  {
    count = 0;
    for (i = 2; i <= Number/2; i++)
    {
        if(Number%i == 0)
        {
          count++; 
          break; // if the no. is divisible by any no. then it'll break and i is incremented by 1
        }
    }
    if(count == 0 && Number != 1 )
    {
        printf(" %d ", Number);
    }  
  }
  return 0;
}

 
Add a comment
Know the answer?
Add Answer to:
4. Write a logical function perfect Square that receives a positive integer number and checks if ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

  • Using the C Programming Language: An integer number is said to be a perfect number if...

    Using the C Programming Language: An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each...

  • An integer is said to be a perfect number if the sum of its divisors, including...

    An integer is said to be a perfect number if the sum of its divisors, including 1(but not the number itself), is equal to the number. For example, 6 is a perfect number because 6 = 1+2+3. A) Write a function numPerfect( number ) that returns true when the number is a perfect number, false when it is not.        B) Write a C# console program that calls the function in A) to determine and print all the perfect numbers...

  • In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and...

    In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...

  • An integer number is said to be a perfect number if it is equal to the...

    An integer number is said to be a perfect number if it is equal to the sum of its factors (divisors), including 1 (but not the number itself). For example, 6 is a perfect number because 6 = 3+2+1. Write a C function isPerfect that returns true if the input integer number is a perfect number and false otherwise. Then, use this function in a C program that determines and prints all the perfect numbers between 1 and 1000.

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • Question 3 (10 marks) An integer number is said to be a perfect number if its...

    Question 3 (10 marks) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because its factors, except itself, are 3, 2, and 1, and 6 = 3+2+1, but 12 is not a perfect number because its factors, except itself, are 6, 4, 3, 2, and 1, but 12 # 6+4+3+2+1. First. Write a C function, called isperfect, that...

  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT