Question

Q1) Write an algorithm for finding the prime numbers between 1 and 100 and displaying them. As you know; prime numbers are th for matlab programming beginner level
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB CODE:

clc
clear all

numbers = 1:100; %creates an array 1 to 100
for ii = 1:length(numbers) %running loop for length of numbers array(here 100)
    count = 0;                %initialising count be zero.
    for number = 1:ii         %running loop to find that number is prime or not
        if(rem(numbers(ii),number) == 0 ) %if remainder of numbers(ii) and number is equal to zero stisfies
            count = count + 1;      %then the factor count is increased by one
        end
    end                  %by end of number loop we get number of factors in count variable
    if(count == 2)            %if count is equals to 2,then it is prime
       disp(numbers(ii))      %if it is prime we are displaying that number
    end
end- x HOE OO Editor - C:\Users\SAI KUMAR\Documents\MATLAB\cheg.m* File Edit Text Go Cell Tools Debug Desktop Window Help :DDD f

Same code without comments

clc
clear all

numbers = 1:100;
for ii = 1:length(numbers)
    count = 0;              
    for number = 1:ii       
        if(rem(numbers(ii),number) == 0 )
            count = count + 1;    
        end
    end                
    if(count == 2)          
       disp(numbers(ii))    
    end
end

code ends

- x Help Stack: Base fx HOE OO Editor - C:\Users\SAI KUMAR\Documents\MATLAB\cheg.m* File Edit Text Go Cell Tools Debug Deskto

output:

- x MATLAB 7.8.0 (R2009a) File Edit Debug Parallel Desktop Window Help Current Directory: C:\Users\SAI KUMAR\Documents\MATLAB- х MATLAB 7.8.0 (R2009a) File Edit Debug Parallel Desktop Window Help Current Directory: C:\Users\SAI KUMAR\Documents\MATLAB

FLOWCHART

х Eile Type a question for help - X 2 Drawing1 - Microsoft Visio Edit View Insert Insert Format Tools Shape Window Help HOR 自

Add a comment
Know the answer?
Add Answer to:
for matlab programming beginner level Q1) Write an algorithm for finding the prime numbers between 1...
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
  • Q1: Write an algorithm and sketch the flowchart to read 20 numbers for x and finding...

    Q1: Write an algorithm and sketch the flowchart to read 20 numbers for x and finding the number of times that the number 100 is repeated between these numbers. (12 marks) Q2: What is the difference between prefix decrementer (- - i) and postfix decrementer (i - -) explain using examples. Q3: C++ includes a very special operator called the ternary operator. Explain with example how this operator used? Q4: Write C++ Program which output this result ******50 *****500 ****5000...

  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

    The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3,...

  • Q1. Write an algorithm (pseudocode or flowchart) for the following problem. Input: Eight integers Output: the...

    Q1. Write an algorithm (pseudocode or flowchart) for the following problem. Input: Eight integers Output: the sum of even numbers only. Example: Let the eight integers are 2, 9, 3, 20,5,17, 10, 6 then S = 2+20+10+6 = 38

  • *** Write a function called circular_primes that finds the number of circular prime numbers smaller than...

    *** Write a function called circular_primes that finds the number of circular prime numbers smaller than n, where n is a positive integer scalar input argument. For example, the number, 197, is a circular prime because all rotations of its digits: 197, 971, and 719, are themselves prime. For instance, there are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. It is important to emphasize that rotation means circular...

  • Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following...

    Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...

  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized...

    Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized with the following strings: “Einstein”, “Newton”, “Copernicus”, and “Kepler”. 2. Assume names in an Integer array with 20 elements. Write a pseudocode algorithm a For Loop that displays each element of the array. 3. Assume the arrays numberArray1 and numberArray2 each have 100 elements. Write a pseudocode algorithm that copies the values in numberArray1 to numberArray2 . 4. Write a pseudocode algorithm for a...

  • Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number...

    Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number from the keyboard as a string and then converts it into a decimal number. For example, if the input is 1100, the output should be 12. (Hint: Break the string into substrings and then convert each substring to a value for a single bit. If the bits are b0, b1, b2, and b3, the decimal equivalent is 8b0+ 4b1+ 2b2+ b3.) Problem 2: Suppose...

  • Q1: Here we consider finding the length of the shortest path between all pairs of nodes...

    Q1: Here we consider finding the length of the shortest path between all pairs of nodes in an undirected, weighted graph G. For simplicity, assume that the n nodes are labeled 1; 2; : : : ; n, that the weight wij of any edge e = (i; j) is positive and that there is an edge between every pair of nodes. In this question, the goal is to solve this via dynamic programming. Note that the algorithm you will...

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