找出100以内的质数并求和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           int a = 0;
                for(int s=0;s<=100;s++)
                {
                 int num = 0;//记录能整除的数的个数

                 for (int i = 1; i <= s; i++)
                 {

                     if (s % i == 0)
                     {
                         num++;
                     }
                 }
                     if (num == 2)
                     {

                         Console.WriteLine(s);
                         a = a + s;
                     }

                }
                Console.WriteLine("100以内所有质数和为{0}",a);
                Console.ReadLine();
        }
    }
}
时间: 2024-08-10 03:56:44

找出100以内的质数并求和的相关文章

求100以内的质数

求100以内的质数. 1 num = [2] 2 i = 3 3 while i < 101: 4 j = 2 5 while j < i: 6 if i % j == 0: 7 break 8 else: 9 j = j + 1 10 continue 11 else: 12 num.append(i) 13 i = i + 1 14 print(num)

编写一个程序找出100~999之间所有的水仙花数

如果一个3位数等于其各位的立方和,称该数为水仙花数. 如,所以407是一个水仙花数,编写一个程序找出100~999之间所有的水仙花数. 1 #include<stdio.h> 2 #include<stdlib.h> 3 //判断水仙花数,是则返回1 4 int isNarcissus(int n); 5 6 int main() 7 { 8 int i; 9 for(i = 100; i < 1000; i++) 10 if(isNarcissus(i)) 11 print

C++实现 找出10000以内的完数

C++实现 找出10000以内的完数 #include <stdio.h> int main(){ int n; // 用户输入的整数 int i; // 循环标志 printf("输入一个整数:"); scanf("%d",&n); printf("%d=",n); // n>=2才执行下面的循环 for(i=2; i<=n; i++){ while(n!=i){ if(n%i==0){ printf("

题目:一个数如果恰好等于它的因子之和,这个数就称为 &quot;完数 &quot;。例如6=1+2+3.编程&#160;&#160;&#160;&#160; 找出1000以内的所有完数。

题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 ".例如6=1+2+3.编程     找出1000以内的所有完数. 1 package day11_2; 2 3 public class lianxi09 { 4 public static void main(String[] args) { 5 6 for (int i = 1; i < 1000; i++) { 7 int sum=0; 8 for (int j = 1; j <i; j++) { 9 10

找出1000以内的所有完数

题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 ".例如6=1+2+3.编程 找出1000以内的所有完数. 1 package com.li.FiftyAlgorthm; 2 3 /** 4 * 题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 ".例如6=1+2+3.编程 找出1000以内的所有完数. 5 * 6 * @author yejin 7 * 8 */ 9 public class Wanshu { 10 public static

java实现找出1000以内的所有完数

/**  *   *题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程 找出1000以内的所有完数  *  */ public class Test9 {     public static int perfact(int n) {         int s = 0;         for (int i = 1;i <= n / 2;i++) {             if (n % i == 0) {                 s 

【Java】编程找出1000以内的所有完数。

1 package com.xt.homework.hw09; 2 /** 3 * 5. 一个正整数,如果恰好等于除它本身外的所有因子之和,这个数就称为"完数". 4 * 例如6=1+2+3,编程找出1000以内的所有完数. 5 * 6 * 7 * @author 天耀二期 8 * 杨勃隆 9 */ 10 public class HomeWork05 { 11 public static void main(String[] args){ 12 { 13 int i,j,k; 14

C语言打印100以内的质数

C语言打印100以内的质数 #include <stdio.h> int main() { int number; int divisor; for( number = 3; number <= 100; number += 2 ) { for( divisor = 3; divisor <= number; divisor += 2 ) { if( number % divisor == 0 ) break; } if( divisor == number ) printf(&q

打印100以内的质数

#include<stdio.h> #include<stdlib.h> int is_zs(int a); int main(void) { int i; int count = 0; for(i = 1; i <= 100; i++) { if(is_zs(i)) { printf("%d\t", i); count++; if(count % 5 == 0) { printf("\n"); } } } return EXIT_SU