bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_2 / practice / prac_007.cpp

prac_007.cpp (475B)


      1 /* sum of natural numbers upto n */
      2 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 class add {
      7     int inp, i, a = 0;
      8 
      9 public:
     10     void input();
     11     void calc();
     12     void output();
     13 };
     14 
     15 void add::input() {
     16     cout << "enter the number : ";
     17     cin >> inp;
     18 }
     19 
     20 void add::calc() {
     21     for (i = 1; i <= inp; i++) {
     22         a = a + i;
     23     }
     24 }
     25 
     26 void add::output() {
     27     cout << "Addtion = " << a;
     28 }
     29 
     30 int main() {
     31     add z;
     32     z.input();
     33     z.calc();
     34     z.output();
     35     return 0;
     36 }