Bài 26: Tính tích tất cả các “ước số lẻ” của số nguyên dương n

Bài 26: Tính tích tất cả các “ước số lẻ” của số nguyên dương n

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

Tính tích tất cả các “ước số lẻ” của số nguyên dương n

Cài đặt

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

int main()
{
    int i, n;
    long P;
    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;
    P = 1;
    printf("\nCac uoc so le cua so %d la: ",n);
    while(i <= n)
    {
        if(n % i == 0)
        {
            if(i % 2 == 1)
            {
            printf("%4d", i);
            P = P * i;
            }
        }
            i++;
    }
    printf("\nTich cac uoc so le cua %d la: %ld", n, P);

    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)
        {
            S = S * i;
        }
    }
}
cout << "\ntich cua cac uoc so le cua n : " << S << endl;
return 0;
}
Bạn thấy bài viết này như thế nào?: 
Average: 10 (2 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

 
Sony Ericsson Xperia Ray & Active Android Phones Coming Soon

Sony Ericsson Xperia Ray & Active Android Phones Coming Soon

Sony Ericsson Xperia Ray and Sony Ericsson Xperia Active both feature 1GHz processors and compact designs and both models will come with Android 2.3 Gingerbread.

Chrome soán ngôi Firefox

Chrome soán ngôi Firefox trong năm 2012

Ngày trước, Firefox từng được kỳ vọng sẽ vượt mặt Internet Explorer trên thị trường toàn cầu nhưng nay lại không thể bảo vệ vị trí á quân trước Chrome.

Hướng dẫn custom entity reference: Drupal & Entity reference

Hướng dẫn custom entity reference: Drupal & Entity reference

There are some good examples out there on how to create a custom Entity reference Behaviour plugin, but not so much for Selection handlers.

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

 

Diet con trung