c++ Major Assignment
c++ Major Assignment
Reg#: _201-2404125___________________________________
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() {
return 0;
}
#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;
while(i <=700)
{
sum2=sum2+i;
i=i+20;
}
cout<<"sum2 sequence is="<<sum2<<endl;
while(i <=2000)
{
sum3=sum3+i;
i=i+50;
}
cout<<"sum3 sequence is="<<sum3<<endl;
while(i <=450)
{
sum4=sum4+i;
i=i+5;
}
cout<<"sum4 sequence is="<<sum4<<endl;
return 0;
}
#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;
return 0;
}
#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];
}
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}
};
return 0;
}
#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];
}
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};
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;
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;
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;
int main() {
int 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;
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;
return 0;
}
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()
}
float sumoftwonums(float a , float b)
{
return a+b;
}
…………………………………………………………………………………
#include <iostream>
using namespace std;
float diffrenceoftwonums(float,float);
int main()
}
float diffrenceoftwonums(float a , float b)
{
return a-b;
}
#include <iostream>
using namespace std;
float quotientoftwonums(float,float);
int main()
}
float quotientoftwonums(float a , float b)
{
return a/b;
}
#include <iostream>
using namespace std;
float productoftwonums(float,float);
int main()
}
float productoftwonums(float a , float b)
{
return a*b;
}