c++ assignment.pdf
c++ assignment.pdf
Name:RUPESH KUMAR
Course: B.Sc (Hons.)
Computer Science
Roll no . : 24/48045
Subject: Object Oriented
Programming Using C++
1.Write a program to print first n Prime
numbers.
CODE
#include <iostream>
using namespace std;
int main()
{
int i,p=2,n;
cout<<"Enter value of N:";
cin>>n;
while(n){
for (i=2;i<p;i++)
{if (p%i==0)
break;}
if(i==p)
{ cout<<p<<" ";
n--;}
p++;
}
return 0;
OUTPUT
2. Write a program to draw following :
CODE
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows for the pyramid: ";
cin >> n;
CODE
#include <iostream>
using namespace std;
int main()
{
int i, j, n ;
cout<<"Enter No of N:";
cin>>n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
cout << (char)('A' + j - 1) << " ";
}
cout << endl;
}
return 0;
}
OUTPUT
CODE
#include <iostream>
using namespace std;
int main()
{
// Number of rows
int N;
cout<<"Enter no of rows:";
cin>>N;
return 0;
}
OUTPUT
3. Write a program to print GCD of 2
numbers where 2 numbers are entered by
user .
Code
#include <iostream>
using namespace std;
// Function to calculate GCD using the Euclidean Algorithm
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is: " << gcd(num1, num2) <<
endl;
return 0;
}
OUTPUT