peri hokiperihokiclarustologisticsrimashaop.comfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76pola petir tersembunyi gates of olympus perihokipgsoft perkuat mahjong ways 2 di akun perihokiperihoki beri pola jitu mahjong wins 3 auto maxwinexclusive perihoki gates of olympus hadirkan fitur super scatterpgsoft mahjong ways perihoki punya irama scatter sendiripola jitu scatter hitam mahjong wins 3 auto maxwin di perihokiakun baru langsung panen di gates of olympus di duta76cara buat akun duta76 menjadi gacor main mahjong ways 2 pgsoftmahjong wins 3 duta76 viral karena tembus maxwin dalam 7 menitcuan 10rb scatter mahjong wayssantai main mahjong wins gacorscatter emas 257jt mahjong winspegawai menang mahjong awsbetteknik menang mahjong ways yang efektif

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Tính tích tất cả các ước số của số nguyên dương n bằng C / C++
SIMPLE
STRING
ARRAY
SORTING
POINTER
CALCULATION
NUMBER
OTHER
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Tính tích tất cả các “ước số” của số nguyên dương n bằng C / C++

Trong bài viết này chúng ta sẽ thực hiện chương trình tính tích các ước số của số nguyên dương n bằng C / C++.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Một số được gọi là ước số của số nguyên A khi A chia hết cho số đó, hay nói cách khác thì mọi số mà A chia hết được gọi là ước số của A.

Mình sẽ thực hiện hai chương trình để tính tích các ước số của số nguyên dương n bằng hai ngôn ngữ khác nhau là C và C++.

Tính tích các ước số của số nguyên n bằng C

Ta cần một biến n là số nguyên dương cần tính ước số, n được nhận từ bàn phím và biến P là tích các ước số của n.

Bài viết này được đăng tại [free tuts .net]

Sử dụng vòng lặp do..while để yêu cầu người dùng nhập vào số n lớn hơn 0, nếu n < 0 thì nhập lại.

Để tính tích các ước số đầu tiên ta cần liệt kê các ước số của nó, ta sử dụng vòng lặp for lặp từ 1 đến <= n.

for(int i = 1; i <= n; i++)
  {
    if(n % i == 0)
    {
      printf("%4d", i);
      P = P * i;
    }
  }

Tích các ước số của n sẽ được tính và lưu vào biến P, sau khi tính xong chỉ cần hiển thị P ra màn hình bằng printf().

Code:

#include<stdio.h>
int main()
{
  int n;
  long P = 1;
  //sử dụng dowhile để yêu cầu người dùng nhập vào số lớn hơn 0
  //nếu n < 0 thì yêu cầu nhập lại
  do
  {
    printf("\nNhập vào số nguyên n lớn hơn 0: ");
    scanf("%d", &n);
    if(n <= 0)
    {
      printf("\n Số n phải lớn hơn 0, vui lòng nhập lại !");
    }
  }while(n <= 0);
  //sử dụng vòng lặp for để tính tổng các ước số của n
  printf("Các ước số của %d là: ",n);
  for(int i = 1; i <= n; i++)
  {
    if(n % i == 0)
    {
      printf("%4d", i);
      P = P * i;
    }
  }
  printf("\nTích các ước số của %d là: %ld", n, P);

  printf("\n-----------------------------------------\n");
  printf("Chương trình này được đăng tại Freetuts.net");
}

Kết quả:

bai16 01 PNG

Tính tích các ước số của số nguyên n bằng C++

Việc tính tích các ước số của số nguyên bằng ngôn ngữ C++ tương tự như ngôn ngữ C.

Ta chỉ cần thay thế printf(), scanf() trong ngôn ngữ C bằng cout và cin trong ngôn ngữ C++. Nhớ khai báo thư viện <iostream> trước khi sử dụng cout và cin nhé.

#include<iostream>
using namespace std;
int main()
{
  int n;
  long P = 1;
  //sử dụng dowhile để yêu cầu người dùng nhập vào số lớn hơn 0
  //nếu n < 0 thì yêu cầu nhập lại
  do
  {
    cout<<"\nNhập vào số nguyên n lớn hơn 0: ";
    cin>>n;
    if(n <= 0)
    {
      cout<<"\n Số n phải lớn hơn 0, vui lòng nhập lại !";
    }
  }while(n <= 0);
  //sử dụng vòng lặp for để tính tích các ước số của n
  cout<<"Các ước số của "<<n<<" là: ";
  for(int i = 1; i <= n; i++)
  {
    if(n % i == 0)
    {
      cout<< i << "\t";
      P = P * i;
    }
  }
  cout<<"\nTích các ước số của "<<n<<" là: "<<P;

  cout<<"\n-----------------------------------------\n";
  cout<<"Chương trình này được đăng tại Freetuts.net";
}

Kết quả:

bai16 02 PNG

Như vậy là chúng ta đã thực hiện xong chương trình tính tích các ước số của số nguyên n bằng C / C++. Các bạn có thể tham khảo các bài tập khác tại Tổng hợp 1000 bài tập C / C++ nhé, chúc các bạn thành công !!!

Câu hỏi thường gặp liên quan:

Cùng chuyên mục:

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý chuỗi (string.h) trong C/C++

Các hàm xử lý chuỗi (string.h) trong C/C++

Thread Pools và Parallel Algorithms trong C++

Thread Pools và Parallel Algorithms trong C++

Tạo và quản lý các Multithreading trong C++

Tạo và quản lý các Multithreading trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Try, Catch, và Throw của Exception Handling trong C++

Try, Catch, và Throw của Exception Handling trong C++

Cách sử dụng Lambda Expressions trong C++

Cách sử dụng Lambda Expressions trong C++

Sử dụng weak_ptr trong C++

Sử dụng weak_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng unique_ptr trong C++

Sử dụng unique_ptr trong C++

Tổng quan về Smart Pointers trong C++

Tổng quan về Smart Pointers trong C++

Sử dụng Iterators trong STL của C++

Sử dụng Iterators trong STL của C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng trong List trong C++

[Iterator] Sử dụng trong List trong C++

[STL] Sử dụng Vector trong C++

[STL] Sử dụng Vector trong C++

Tổng quan về Iterators trong C++

Tổng quan về Iterators trong C++

Top