0% found this document useful (0 votes)
18 views7 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy