0% found this document useful (0 votes)
2 views19 pages

c++ Major Assignment

The document contains a series of C++ programming exercises and solutions, including programs that utilize loops to sum arithmetic sequences, handle one-dimensional and two-dimensional arrays, and perform various mathematical operations. It also includes user input handling for determining minimum values, checking even or odd numbers, and identifying prime or composite numbers. Additionally, there are functions for basic arithmetic operations such as sum, difference, quotient, and product of two numbers.
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)
2 views19 pages

c++ Major Assignment

The document contains a series of C++ programming exercises and solutions, including programs that utilize loops to sum arithmetic sequences, handle one-dimensional and two-dimensional arrays, and perform various mathematical operations. It also includes user input handling for determining minimum values, checking even or odd numbers, and identifying prime or composite numbers. Additionally, there are functions for basic arithmetic operations such as sum, difference, quotient, and product of two numbers.
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/ 19

Name: Zahidullah Saadat______________________________

Reg#: _201-2404125___________________________________

Teacher: Waliullah Shinwari

Question 1: Write three C++ programs using for, while and do while loops respectively that
add up all the elements of the following arithmetic sequences.
1,2,3……..………500
10,20…….………800
20,40……………700
50,100…………2000
5,10……….………450

#include <iostream>
using namespace std;
int main() {

//using a for loop


//of the following arithmatic sequence
// Sequence: 1, 2, 3, ..., 500
int sum1 = 0;
for (int i = 1; i <= 500; i=i+1) {
sum1=sum1+i;
}
cout << "Sum of sequence 1: " << sum1 << endl;
// Sequence: 10, 20, ..., 800
int sum2 = 0;
for (int i = 10; i <= 800; i=i+10) {
sum2=sum2+i;
}
cout << "Sum of sequence 2: " << sum2 << endl;

// Sequence: 20, 40, ..., 700


int sum3 = 0;
for (int i = 20; i <= 700; i += 20) {
sum3 += i;
}
cout << "Sum of sequence 3: " << sum3 << endl;

// Sequence: 50, 100, ..., 2000


int sum4 = 0;
for (int i = 50; i <= 2000; i += 50) {
sum4 += i;
}
cout << "Sum of sequence 4: " << sum4 << endl;
// Sequence: 5, 10, ..., 450
int sum5 = 0;
for (int i = 5; i <= 450; i += 5) {
sum5 += i;
}
cout << "Sum of sequence 5: " << sum5 << endl;

return 0;
}

// Using while loop


// Initialize variables

#include <iostream>
using namespace std;
int main() {
// Sequence: 1, 2, 3, ..., 500
int sum=0;
int i=1;

while(i <=500)
{
sum=sum+i;
i=i+1;
}
cout<<"sum sequence is="<<sum<<endl;

// Sequence: 10,20,……..800
int sum1=0;
int i=10;

while(i <=800)
{
sum1=sum1+i;
i=i+10;
}
cout<<"sum1 sequence is="<<sum1<<endl;

// Sequence: 20, 40, ..., 700


int sum2=0;
int i=20;

while(i <=700)
{
sum2=sum2+i;
i=i+20;
}
cout<<"sum2 sequence is="<<sum2<<endl;

// Sequence: 50, 100,, ..., 2000


int sum3=0;
int i=50;

while(i <=2000)
{
sum3=sum3+i;
i=i+50;
}
cout<<"sum3 sequence is="<<sum3<<endl;

// Sequence: 5, 10, ..., 450


int sum4=0;
int i=5;

while(i <=450)
{
sum4=sum4+i;
i=i+5;
}
cout<<"sum4 sequence is="<<sum4<<endl;

return 0;
}

// Using Do while loop

#include <iostream>
Using namespace std;
Int main(){
// Sequence: 1, 2, 3, ..., 500
int sum1 = 0;
int i = 1;
do {
sum1 += i;
i=i+1;
} while (i <= 500);
cout << "Sum of sequence 1: " << sum1 << endl;

// Sequence: 10, 20, ..., 800


int sum2 = 0;
int i = 10;
do {
sum2 += i;
i=i+ 10;
} while (i <= 800);
cout << "Sum of sequence 2: " << sum2 << endl;

// Sequence: 20, 40, ..., 700


int sum3 = 0;
int i = 20;
do {
sum3 += i;
i=i+20;
} while (i <= 700);
<< "Sum of sequence 3: " << sum3 << endl;

// Sequence: 50, 100, ..., 2000


int sum4 = 0;
int i = 50;
do {
sum4 += i;
i=i+50;
} while (i <= 2000);
<< "Sum of sequence 3: " << sum4 << endl;

// Sequence: 5, 10, ..., 450


int sum5 = 0;
int i = 5;
do {
sum5 += i;
i=i+5;
} while (i <= 450);
<< "Sum of sequence 3: " << sum5 << endl;

return 0;
}

QUESTIONS NO 2: Write a C++ program that


1. prompts the user to input elements for a one-dimensional array arrl of size 50
at runtime and then displays the elements of the array on the screen.

#include <iostream>
using namespace std;
int main() {
int arr1Size = 50;
int arr1[arr1Size];

cout << "Enter " << arr1Size << " elements for the array:" << endl;
for (int i = 0; i < arr1Size; ++i) {
cin >> arr1[i];
}

cout << "Elements of the array:" << endl;


for (int i = 0; i < arr1Size; ++i) {
cout << arr1[i] << " ";
}
cout << endl;

return 0;
}

2. Develop a C++ program that accepts elements for a two-dimensional array arr2
of size 5x6 from the user at runtime. Utilize a loop to print the elements stored at
a specified [ith, jth] position on different lines separately.
#include <iostream>
using namespace std;

int main() {
int rows = 5;
int cols = 6;
int arr2[rows][cols];

cout << "Enter " << rows * cols << " elements for the 2D array:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> arr2[i][j];
}
}

int i, j;
cout << "Enter row index (0-" << rows - 1 << "): ";
cin >> i;
cout << "Enter column index (0-" << cols - 1 << "): ";
cin >> j;

cout << "Element at [" << i << "][" << j << "]: " << arr2[i][j] << endl;

return 0;
}

3. Create a C++ program that declares a two-dimensional array with 3 rows and 4
columns, initializing values to the array during declaration. Subsequently, use any
looping construct to display all elements on the screen.
#include <iostream>
using namespace std;

int main() {
int arr3[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

cout << "Elements of the 2D array:" << endl;


for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
cout << arr3[i][j] << " ";
}
cout << endl;
}

return 0;
}

4. Implement a C++ program that declares a one-dimensional array capable of storing


marks for seven subjects. Calculate and return the sum, average, and percentage of the
total marks obtained by students.

#include <iostream>
using namespace std;

int main() {
const int numSubjects = 7;
int marks[numSubjects];

cout << "Enter marks for " << numSubjects << " subjects:" << endl;
for (int i = 0; i < numSubjects; ++i) {
cin >> marks[i];
}

int sum = 0;
for (int i = 0; i < numSubjects; ++i) {
sum += marks[i];
}

double average = static_cast<double>(sum) / numSubjects;


double percentage = (sum * 100.0) / (numSubjects * 100.0);

cout << "Sum of marks: " << sum << endl;


cout << "Average marks: " << average << endl;
cout << "Percentage: " << percentage << "%" << endl;

return 0;
}

5. Declare a one-dimensional array of size 6. Write code that identifies and returns all
odd numbers present within the array. Ensure that there are some odd elements
already stored in the array.

#include <iostream>
using namespace std;
int main() {
const int arr4Size = 6;
int arr4[arr4Size] = {2, 5, 8, 11, 14, 17};

cout << "Odd numbers in the array:" << endl;


for (int i = 0; i < arr4Size; ++i) {
if (arr4[i] % 2 != 0) {
cout << arr4[i] << " ";
}
}
cout << endl;

return 0;
}

Questions 3:
i.Write a C++ program that accepts two numbers from the user and determines the minimum
between them using an if-else statement.
#include <iostream>
using namespace std;

int main() {
int num1, num2;

cout << "Enter the first number: ";


cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

if (num1 < num2) {


cout << "The minimum is: " << num1 << endl;
} else {
cout << "The minimum is: " << num2 << endl;
}
return 0;
}

ii.Develop a C++ program that takes a number from the user and identifies whether the
number is even or odd.

#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter a number: ";


cin >> number;

if (number % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}

return 0;
}

iii.Create a C++ program that takes a number from the user and determines if the number is
prime or composite.

#include <iostream>
using namespace std;

bool isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return false;
}
}
return true;
}

int main() {
int number;

cout << "Enter a positive integer: ";


cin >> number;

if (isPrime(number)) {
cout << number << " is a prime number." << endl;
} else {
cout << number << " is a composite number." << endl;
}

return 0;
}

IV.Write a C++ program that takes a number from the user and indicates whether
the
number is positive, negative, or neutral (zero).
#include <iostream>
using namespace std;

int main() {
double number;

cout << "Enter a number: ";


cin >> number;

if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is neutral (zero)." << endl;
}

return 0;
}

V.Develop a C++ program that takes a student's marks and assigns a grade
according to the following ranges: 0 to 49 for F, 50 to 59 for D, 60 to 69 for B, 70
to 79 for A, and 80 to 100 for At.
#include <iostream>
using namespace std;

int main() {
int marks;

cout << "Enter the student's marks: ";


cin >> marks;

if (marks >= 0 && marks <= 49) {


cout << "Grade: F" << endl;
} else if (marks >= 50 && marks <= 59) {
cout << "Grade: D" << endl;
} else if (marks >= 60 && marks <= 69) {
cout << "Grade: B" << endl;
} else if (marks >= 70 && marks <= 79) {
cout << "Grade: A" << endl;
} else if (marks >= 80 && marks <= 100) {
cout << "Grade: A+" << endl;
} else {
cout << "Invalid marks entered." << endl;
}

return 0;
}

Write a function in C++

1. Take two numeric values from the user and return their sum.
2. ii. Take two numeric values from the user and return their difference.
3. iii. Take two numeric values from the user and return their quotient.
4. iv. Take two numeric values from the user and return their product.

#include <iostream>
using namespace std;
float sumoftwonums(float,float);
int main()

//sum of the values


{
cout<<sumoftwonums(50,60);

}
float sumoftwonums(float a , float b)
{
return a+b;
}
…………………………………………………………………………………
#include <iostream>
using namespace std;
float diffrenceoftwonums(float,float);
int main()

//difrence of the values


{
cout<<diffrenceoftwonums(75,60);

}
float diffrenceoftwonums(float a , float b)
{
return a-b;
}

#include <iostream>
using namespace std;
float quotientoftwonums(float,float);
int main()

//quotient of the values


{
cout<<quotientoftwonums(10,2);

}
float quotientoftwonums(float a , float b)
{
return a/b;
}

#include <iostream>
using namespace std;
float productoftwonums(float,float);
int main()

//product of the values


{
cout<<productoftwonums(10,2);

}
float productoftwonums(float a , float b)
{
return a*b;
}

You might also like