To accept two numbers from the user; Display all prime numbers between these two numbers.

24 ビュー (過去 30 日間)
To accept two numbers from the user; Display all prime numbers between these two numbers.
To accept two numbers from the user and display perfect numbers between these two numbers.
To accept two numbers from the user and display Armstrong number between these numbers.
Simple coding using DoWhile, IfElse

採用された回答

B.k Sumedha
B.k Sumedha 2015 年 5 月 28 日
編集済み: Andrei Bobrov 2015 年 5 月 28 日
clc
num1 =input('Enter num1 value ');
num2 =input('Enter num2 value ');
n = num1 : num2;
p = isprime(n);
n(p) %displays the primes
  1 件のコメント
BeTrayer EK
BeTrayer EK 2015 年 5 月 28 日
Hi if lets say my matlab dont have the "isprime" how do i do with it ?
How about these 2 more
To accept two numbers from the user and display perfect numbers between these two numbers.
To accept two numbers from the user and display Armstrong number between these numbers.
*thank you for reply and taking your time to read it . very appreciated*

サインインしてコメントする。

その他の回答 (2 件)

B.k Sumedha
B.k Sumedha 2015 年 5 月 28 日
Try this for Armstrong number:
clear, clc
% Let i take two numbers say 100 to 999
for i = 100 : 999
% We examine every digit in i
is = num2str(i);
% i1 is the left-most digit in i
i1 = str2num(is(1));
% i2 is the middle digit
i2 = str2num(is(2));
% i3 is the right-most digit in i
i3 = str2num(is(3));
% We calculate the probable AN
an = i1^3 + i2^3 + i3^3;
% We compare to the number itself
if i == an
% We display the pair of equal numbers
disp([i an])
end
end

B.k Sumedha
B.k Sumedha 2015 年 5 月 28 日
This is for perfect numbers where u can change the values of m and n where they are the two input numbers
n=100;
m=10;
output = zeros(1,n);
for i = m:n
test = 1:i-1;
if (sum(test(mod(i,test) == 0)) == i)
output(i) = i;
end
end
output(output == 0) = []
  3 件のコメント
BeTrayer EK
BeTrayer EK 2015 年 5 月 28 日
Thank you very much B.k Sumedha for answering & taking your time to reply .
but if let say i want the user to input the value any more simple breakdown way i can use ?
Thank you
BeTrayer EK
BeTrayer EK 2015 年 5 月 28 日
My method for doing single is like that . lets say i want double ?
clear clc
Key=0; Num1 = input('Enter A Number :'); Num2 = input('Enter A Number:'); Count = 1;
while(Count<=Num1)
if (rem(Num1,Num2,Count)==0)
Key=Key+1;
end
Count = Count + 1;
end
if(Key==2)
fprintf('%d Is A Prime Number\n' ,Num1);
else
fprintf('%d NOT A Prime Number\n', Num1);
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by