0% found this document useful (0 votes)
56 views

ADVANCED PROGRAMMING ASSIGNMEN C++

The document contains 18 C++ programming assignments covering topics such as loops, functions, classes, constructors, and destructors. Some of the key problems addressed include: 1) Printing the first 10 natural numbers using a for loop 2) Calculating the sum of the first 10 natural numbers using a for loop 3) Finding perfect numbers between 1 and 500 4) Checking if a number is prime or not 5) Finding prime numbers within a given range 6) Calculating the factorial of a number 7) Constructing rectangle and square classes with methods to calculate area and perimeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

ADVANCED PROGRAMMING ASSIGNMEN C++

The document contains 18 C++ programming assignments covering topics such as loops, functions, classes, constructors, and destructors. Some of the key problems addressed include: 1) Printing the first 10 natural numbers using a for loop 2) Calculating the sum of the first 10 natural numbers using a for loop 3) Finding perfect numbers between 1 and 500 4) Checking if a number is prime or not 5) Finding prime numbers within a given range 6) Calculating the factorial of a number 7) Constructing rectangle and square classes with methods to calculate area and perimeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

ADVANCED PROGRAMMING ASSIGNMENT-C++

1) first 10 natural numbers

#include <iostream>

using namespace std;

int main()

int i;

cout<<"THE NATURAL NUMBERS ARE :\n" ;

for (i=1;i<11;i++)

cout<< i<<endl;

return 0;

}
2) Sum of first 10 natural numbers

#include <iostream>

using namespace std;

int main()

int i,sum=0;

for (i=1;i<11;i++)

sum=sum+i;

cout << "the sum is "<<sum;

return 0;

}
3) sum of first n natural numbers

#include <iostream>

using namespace std;

int main()

int i,n,sum;

cout<<"enter the value of n = ";

cin>>n;

for (i=1;i<=n;i++)

cout <<i <<"\n";

sum = n*(n+1)/2 ;

cout << "the sum is "<<sum;

return 0;

}
4) Find the perfect numbers between 1 tp 500

#include <iostream>

using namespace std;

int main()

cout<<"The perfect numbers between 1 to 500 are: "<<endl;

int s=0;

for(int i=2;i<=500;i++)

s=0;

for(int j=1;j<=i/2;j++)

if(i%j==0)

s+=j;

if(s==i)

cout<<i<<endl;

return 0;

}
5) check if prime or not

#include <iostream>

using namespace std;

int main()

int i,n,c;

c=0;

cout<<" enter number to check \t";

cin >> n;

for(i=2;i<n;i++)

if(n%i==0)

c=c+1;

if(c==0)

cout<<n;
cout<<"is prime number";

else

cout<<n;

cout<<" is not prime number";

return 0;

}
6) to find prime number within a range.

#include <iostream>
using namespace std;
int main()
{
cout<<"Input number for starting range: ";
int a;
cin>>a;
cout<<"Input number for ending range: ";
int b;
cin>>b;
cout<<"The prime numbers between "<<a<<" and "<<b<<"
are:"<<endl;
int c=0, count=0;
for(int i=a;i<=b;i++)
{
c=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
c=1;
break;
}
}
if(c==0)
{
cout<<i<<" ";
count++;
}
}
cout<<"\nThe total number of prime numbers between "<<a<<"
and "<<b<<" is:"<<count<<endl;
return 0;
}

7)

#include <iostream>

using namespace std;

int main()
{
int i,n,factorial=1;
cout<<"enter number u want to find factorial \t" ;
cin>> n ;
for(i=n;i>0;i--)
{
factorial=factorial * i;
}
cout<<"the factorial of n is ";
cout<<factorial ;

return 0;
}
8)
#include <iostream>
using namespace std;
int main()
{
    int num1, c = 0;
    cout << " Enter a number to find the last prime number occurs before the number: ";
    cin >> num1;
    for (int n = num1 - 1; n >= 1; n--) 
    {
        for (int m = 2; m < n; m++) 
        {
            if (n % m == 0)
                c++;
        }
        if (c == 0) 
        {
            if (n == 1) 
            {
                cout << "no prime number less than 2";
                break;
            }
            cout << n << " is the last prime number before " << num1 << endl;
            break;
        }
        c = 0;
    }
    return 0;
}
9)
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float i,n,sum=0;
cout<<"max value of n ";
cin>> n;
for(i=1;i<=n;i++)
{
sum=sum+pow((1/i),i);
}

cout<<"the sum of series is "<<sum;


return 0;
}
10)

#include <iostream>
using namespace std;

int main()
{
float i,n,sum=0;
cout<<"max value of n ";
cin>> n;
for(i=1;i<=n;i++)
{
sum=sum+i*i;
}

cout<<"the sum of series is "<<sum;


return 0;
}
11)

#include <iostream>
#include <math.h>
using namespace std;

int fact(int n)
{
// single line to find factorial
return (n==1 || n==0) ? 1: n * fact(n - 1);
}

int main()
{
float i,x,n,sum=0;
cout<<"enter x value " ;
cin>> x;
cout<<"enter n th term ";
cin>> n;

for(i=0;i<n;i++)
{
sum=sum+(pow(-1,i+1)*pow(x,2*i)/fact(2*i));
}

cout<<"the sum of series is "<<sum;


return 0;
}

12)
#include <iostream>
using namespace std;

int main()
{
    int i, n, sum = 0;
    cout << " Enter n ";
    cin >> n;
    cout << " The odd numbers are: ";
    for (i = 1; i <= n; i++) 
    {
        cout << 2 * i - 1 << " ";
        sum += 2 * i - 1;
    }
    cout << "\n The Sum of odd Natural Numbers upto " << n << " terms is " << sum << endl;
}
14)
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
int i,count=0;
char ch[50];

cout<<"Enter any string : ";


cin>>ch;

for(i=0;ch[i]!='\0';i++)
{
count++;
}
cout<<"Length of String is "<<count;

return 0;
}
15)
#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << " number of rows are ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<i;
cout<<endl;
}
return 0;
}

16)
#include <iostream>
using namespace std;

int main()
{
cout<<"Input number of rows: ";
int n;
cin>>n;
int c=1;
for(int i=1;i<=n;i++)
{
for(int j=n-i;j>=0;j--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<c++<<" ";
}
cout<<endl;
}
return 0;
}
17)
#include <iostream>
using namespace std;

int main()
{
   int i,j,n;
    cout << " Enter number of rows: ";
    cin >> n;
    for(i=n;i>=1;i--)
   {
 
     for(j=1;j<=n+5-i;j++)
cout<<" ";
     for(j=1;j<=i;j++)
       cout<<j;
       for(j=i-1;j>=1;j--)
  cout<<j;
     cout<<endl;
   }
}

18)
#include <iostream>
using namespace std;
int main()
{
    int n, i, j, ctr, r;
    cout << " Enter any number: ";
    cin >> n;
    for (i = 0; i < 10; i++) 
    {
        cout << "The frequency of " << i << " = ";
        ctr = 0;
        for (j = n; j > 0; j = j / 10) 
        {
            r = j % 10;
            if (r == i) 
            {
                ctr++;
            }
        }
        cout << ctr << endl;
    }
}

#include<iostream>
using namespace std;
 
int main()
{
    int n, num, sum = 0;
 
    cout << "Enter the number : ";
    cin >> n;
    num = n;
    while (num != 0)
    {
        sum = sum + num % 10;
        num = num / 10;
    }
    cout << "The sum of the digits of "
         << n << " is " << sum;
}

Note: 13 and 19 Doubt

Constructors and destructors

1)rectangle area and perimeter

#include<iostream>

#include<math.h>

using namespace std;

class rectangle

int length;

int breadth;
public:

int getarea(int l,int b)

int area;

area = l * b;

cout << "Area = " << area << "\n";

int getperimeter(int l,int b)

int peri;

peri = 2 * (l + b);

cout << "Perimeter = " << peri << "\n";

};

int main()

int l,b;

rectangle r;

cout << "Enter length of rectangle ";

cin >> l ;

cout << "Enter breadth of rectangle ";

cin >> b ;

r.getarea(l,b);

r.getperimeter(l,b);
return 0;

2)square perimeter and area (not completed)

#include <iostream>

using namespace std;

class square

private:

int side;

public:

square()

cin>> side;

int getarea() ;

int area;

area=side*side;

cout<<"area of squre"<<area;

int getperi;

int peri;

peri=4*side;
cout<<"perimeter of square"<<peri;

};

You might also like