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

c++ assignment.pdf

The document contains programming assignments for a B.Sc (Hons.) Computer Science course, specifically focusing on Object Oriented Programming using C++. It includes code examples for printing prime numbers, drawing a pyramid, printing letters in a pattern, creating a butterfly pattern, and calculating the GCD of two numbers. Each section provides the code and expected output for the respective programming task.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

c++ assignment.pdf

The document contains programming assignments for a B.Sc (Hons.) Computer Science course, specifically focusing on Object Oriented Programming using C++. It includes code examples for printing prime numbers, drawing a pyramid, printing letters in a pattern, creating a butterfly pattern, and calculating the GCD of two numbers. Each section provides the code and expected output for the respective programming task.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT

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;

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


// Print spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print stars
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl; // Move to the next line
}return 0;
}
OUTPUT

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;

// Variables to store number of spaces and stars


int spaces = 2 * N - 1;
int stars = 0;

// The outer loop will run for (2 * N - 1) times


for (int i = 1; i <= 2 * N - 1; i++) {
// Upper half of the butterfly
if (i <= N) {
spaces = spaces - 2;
stars++;
}
// Lower half of the butterfly
else {
spaces = spaces + 2;
stars--;
}
// Print stars
for (int j = 1; j <= stars; j++) {
cout << "*";
}
// Print spaces
for (int j = 1; j <= spaces; j++) {
cout << " ";
}
// Print stars
for (int j = 1; j <= stars; j++) {
if (j != N) {
cout << "*";
}
}
cout << "\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

You might also like