Bài tập 27: Đếm số lượng “ước số chẵn” của số nguyên dương n

Bài tập 27: Đếm số lượng “ước số chẵn” của số nguyên dương n

Bài tập 27 (Dạng bài tập này giúp các bạn giỏi về: Lưu đồ thuật toán)

Đếm số lượng “ước số chẵn” của số nguyên dương n

Cài đặt

#include<stdio.h>
#include<conio.h>

int main()
{
    int i, n;
    int count;
    do
    {
    printf("\nNhap n(n > 0): ");
    scanf("%d", &n);
      if(n <= 0)
       {
        printf("\n N phai > 0. Xin nhap lai !");
       }
    }while(n <= 0);
    i = 1;
    count = 0;
    printf("\nCac uoc so chan cua so %d la: ",n);
    while(i <= n)
    {
        if(n % i == 0)
        {
            if(i % 2 == 0)
            {
            printf("%4d", i);
            count++;
            }
        }
            i++;
    }
    printf("\nSo luong uoc so chan cua %d la: %ld", n, count);

    getch();
    return 0;
}

Cách khác

#include<iostream>
#include<cmath>
using namespace std;
    int main() { //Tinh S(n) = 1 + 1.2 + 1.2.3 + … + 1.2.3….N
    float S = 1, P = 0;
    int n, i = 1, a = 0, dem = 0; //khai sẵn trước để dùng cho các bài sau luôn
    do
    {
        cout << "\nnhap so n : ";
        cin >> n;
        if (n < 1)
        {
            cout << "\nso nhap kh dung vui long nhap lai";
        }
    } while (n < 1);
    for (i = 1; i <= n; i++)
    {
        if (n % i == 0)
        {
            if (i % 2 == 0)
            {
                dem++;
            }
        }
    }
    cout << "\nso luong cua cac uoc so chan cua n : " << dem << endl;
    return 0;
}
Bạn thấy bài viết này như thế nào?: 
Average: 7 (3 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Quản lý iPhone, iPad trong doanh nghiệp không khó

Quản lý iPhone, iPad trong doanh nghiệp không khó

Cùng với sự phổ biến các thiết bị họ i của Apple (iDevice), số nhân viên sử dụng điện thoại di động iPhone và máy tính bảng iPad cho công việc ngày một nhiều

Hướng dẫn sử dụng Drupal Views and Isotope

Hướng dẫn sử dụng Drupal Views and Isotope

When I was trying to decide how I would lay out my "Recent Work" section for this site, I was intrigued by a style called masonry.

Hướng dẫn cài đặt Drupal VM PHP 7.0.x (configurable) trên Windows

Hướng dẫn cài đặt Drupal VM PHP 7.0.x (configurable) trên Windows

Drupal VM is a virtual machine that contains many convenient utilities for local Drupal development

Công ty diệt chuột T&C

 

Diet con trung