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

C++_Programming_lab

The document outlines various C++ programming exercises from RM.D. Engineering College, including tasks such as calculating the sum of digits, finding the GCD of two numbers, computing square roots, and removing duplicates from arrays. Each exercise includes input and output formats, source code snippets, and execution results demonstrating successful test cases. The document serves as a collection of programming assignments for students from 2024 to 2028 in the ECE department.

Uploaded by

sumathy S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views

C++_Programming_lab

The document outlines various C++ programming exercises from RM.D. Engineering College, including tasks such as calculating the sum of digits, finding the GCD of two numbers, computing square roots, and removing duplicates from arrays. Each exercise includes input and output formats, source code snippets, and execution results demonstrating successful test cases. The document serves as a collection of programming assignments for students from 2024 to 2028 in the ECE department.

Uploaded by

sumathy S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 27
Date: 2024-10- S.No: 1 Exp. Name: Sum of digits ae Write a C++ program to print the sum of individual digits of a positive integer. Input Format: + The first input line reads a positive integer Output Format: + The output isa positive integer representing the sum of individual digits of given integer Source Code: SumofDigits. cpp #include using namespace std; int main() { int n,m, sum = 03 cin > ny while (n>0) cout << sum << endl; return @; Execution Results - All test cases have succeeded! Test Case - 1 User Output 123456789 45 3 8 2 é ri 3 3 5 z 3 RM.D. Engineering College 2024-2028-ECE-A. Z:0N abeg | ZzovOLvesiLt ‘ai Test Case - 2 User Output 0123 ‘W-999-8202"PZ0Z) 2691109 GussaujGua-cWY Date: 2024-10- S.No: 2 Exp. Name: GCD of two numbers - Write a C++ program to compute the GCD of two numbers. Input Format: + The input line reads two space-separated positive integers. Output Format: + The output is a positive integer representing the GCD of two numbers. Source Code: GedoFTwoNumbers cpp include using namespace std; int main() { int a, b , remainder; cin >> a >>b; while (b>0) { renainder=a%; ab; b-remainder} } cout << a << endl} return @; Execution Results - All test cases have succeeded! Test Case - 1 User Output 128 a 3 8 2 é ri 3 3 5 z 3 RM.D. Engineering College 2024-2028-ECE-A. VON abeg | 2zovOlvesiii ‘ai Test Case - 2 User Output 2515 W-999-8202"PZ0Z) 2691109 GueaujGua-GWY Date: 2024-12- S.No: 3 Exp. Name: Square root of a number “ Aim: Write a C++ program that calculates the square root of a given number using Newton's method. Input format: ‘The input is the double value for which the square root needs to be calculated Output format: The output isthe double value that represents the square root. Source Code: squareRoot . cpp. include #include // Function to calculate square root using Newton's method double squareRoot (double n) { 11 write the code. . double i=sqrt(n); return 43 7 int main() { double nunber; // Input number from user std::cin >> numbers // Calculate square root using Newton's method double sqrtNunber = squareRoot (number); // Output the result std::cout << sqrtiunber << std: rend); return 0; Execution Results - all test cases have succeeded! 3 8 2 é ri 3 3 5 z g € a g 8 g 3 3 g ¥ p 3 8 2 iS 9:0N abeg [@zovorvestit Test Case - 1 Test Case - 2 User Output User Output a 3 52. 2.28035 \W-393-820Z-PZ0z! e6a]]09 Suvsauibu3 ‘GWY Exp. Name: Maximum of an array of Date: 2024-12- — numbers 14 Write a C++ program to find the maximum of an array of numbers. Input Format: +The first input line reads a positive integer, n, representing the size ofthe array. + The second input line reads n space-separated integers representing the elements of the array. Output Format: + The output is an integer representing the maximum of the array of integers Source Code: Maxofarray cpp JI Type Content here... #includeciostrean> using namespace std; int main() 7 int 133 cimons int arr[n]; for(i-0;icnjis+) 7 cin arr[i]3 } int max-arr[@]; for(istsicnsit+) { if (arr[i]omax){ maxzare[i]; 7 coutccmaxc<"\n" Execution Results - all test cases have succeeded! Test Case - 1 ~ 3 8 2 é r 3 3 5 z a RM.D. Engineering College 2024-2028-ECE-A. User Output 5 12145 -54412 788451 663311 4124 788451, Test Case - 2 User Output 3 5-2-8 2 . 3 8 2 é ri 3 3 5 z a RM.D. Engineering College 2024-2028-ECE-A. Exp. Name: Removing duplicates using _ Date: 2024-12- S.No 5 strings 14 Aim: Write a C++ program to remove duplicates from an array of numbers. Input Format: +The first input line reads a positive integer, n, representing the size ofthe array. + The second input line reads n space-separated integers representing the elements of the array. Output Format: + The output is space-separated integers of the array after removing the duplicate elements. Source Code: RemovingDuplicate #includeciostrean> #include ‘#includecunordered_set> using namespace std; int main() { int nj cin >> ny vectorcint> arr(n); for (int 4 = Os4ens+4i) { cin >> arr[i]; : unordered_set uniqueElenents; vectorcint> result; for(int is; icn 3i++){ if (uniqueElenents.find(arr[i]) =-uniqueElenents.end()) 7 uniqueElenents. insert(arr(i]); esult.push_back(arr[i]); 7 for (const auto& elenent : result) { cout << elenent << } cout << endl; return 0; . 3 8 2 é ri 3 3 5 z a ¢ a g 8 g 3 3 8 ¥ % 2 3 8 2 iS RMD. Eni Execution Results - all test cases have succeeded! Test Case - 1 User Output 5 12345 12345 Test Case - 2 User Output 7 535377-2 537-2 ° 5 2 3 g 8 8S sl RM.D. Engineering College 2024-2028-ECE-A. Date: 2024-12- S.No: 6 Exp, Name: Removing even numbers “ Aim: Write a C++ program to print the numbers in an array after removing the even numbers. Input Format: +The first input line reads a positive integer, n, representing the size ofthe array. + The second input line reads n space-separated integers representing the elements of the array. Output Format: + The output is space-separated integers of the array after removing the even numbers. Source Code: RemoveEven. cpp #includeciostrean> #include using namespace std; int main() { int n5 cin >> nj vectorcint> arr(n); for (int i =} i¢ nj +44) { cin >> are[i]s t vectorcint> oddNunbers; for (int 4-0; ic nj +44) { if (arr[i] % 2 1-0) { oddtiunbers.push_back(arr[1]) 5 } : for (const auto& number : odd\unbers) { cout << number <<" "3 : cout << endl; return 0; ID: 111524104022 | Page No: 11 ¢ a g 8 g 3 3 8 ¥ % 2 3 8 2 iS RMD. Eni Execution Results - all test cases have succeeded! Test Case - 1 User Output 6 123456 135 Test Case - 2 User Output 8 -10.45 -8 6 -123 4 3378 45333 a 5 2 3 g 8 8S sl RM.D. Engineering College 2024-2028-ECE-A. Date: 2024-12- S.No: 7 Exp. Name: Checking Palindrome a Write a C++ program to check whether a given string is palindrome or not. Input format: +The input line reads a string Output Format: + The output line contains a string Palindrome ifthe given string is palindrome otherwise it will be Not a Palindrome JI Type Content here, #include using namespace std; int main() { string name, p; cin>>nane; psname; reverse(name.begin(),name.end()); 4¢(nane==p){ cout c<"Palindrone"< #includecmap> using namespace std; void countCharacterOccurrences(const char* str){ int frequency[256]={0}; while(*str l= "\@"){ frequency[*str]++; stress + for(int 1+0;1<25654++){ Af(Frequency[i]>0){ coutcestatic_castcchar>(i) nt main(){ char str[256]; cin.getline(str,256); countCharacterOccurrences(str); return 0; ID: 111524104022 | Page No: 15 ¢ a g 8 g 3 g g ¥ p 3 8 2 iS RMD. Eni Execution Results - all test cases have succeeded! Test Case - 1 User Output Hello Hea 1 © 5 2 3 g 8 8S s Test Case - 2 User Output CodeTantra RM.D. Engineering College 2024-2028-ECE-A. Date: 2024-12- S.No: 9 Exp. Name: Salary Slip 3 Aim: Write a C++ program to generate salary slips of employees using structures and pointers. Create a structure Employee with the following members: + EID, Ename, Designation, DOB, DOJ, Basicpay Note that DOB and DO) should be implemented using structure within structure Input Format: «The frst input line reads an integer representing the employee ID + The second input line reads a string representing the name + The third input line reads a string representing employee designation + The fourth input line reads space-separated three integers representing day, month, and year representing the DOB + The fifth input line reads space-separated three integers representing day, month, and year representing DOJ + The sixth input line reads afloat value representing the Basic pay Output Format: + The output lines print the EID, Ename, Designation, DOB, DOI, Age in years, Experience in years, and basic pay Note: For age calculation, use the current year as 2024, Source Code: SalarySlip. cpp ID: 111524104022 Page No: 17 RM.D. Engineering College 2024-2028-ECE-A. #include include using namespace std; struct Date{ int day,month, year; ‘ struct Enployee( int EID; string Enane; string Designation; Date DOB; Date 009; Float Basicpay; b int calculateage(Date dob) { int currentYear = 2624; return currentYear - dob.year; + int calculatetxperience(Date doj){ int currentYear = 2624; return currentYear - doj.year; }/ write your code here for structures Enployee and Date void generateSalarySlip(const Enployee* emp) { // write your code here int age= calculatesge(enp ~2008); int experience = calculatetxperience(enp-»003); coutc<"EID: "<Enaneccendl; coute<"Designation: "<00) .dayc<"/"00) onthe" /"<> emp.EID; cin. ignore(); getline(cin, emp.Enane); getLine(cin, enp.Designat ion); ccin >> enp.D0B.day >> emp.008.nonth >> enp.DOB.year; ‘cin >> enp.003.day >> enp.003.nonth >> enp.003.year; cin >> enp.Basicpay; generateSalarySlip(emp); return @; Execution Results - All test cases have succeeded! Test Case - 1 User Output 101 Kalyan SoftwareEngineer 10 10 2001 15.08 2023, 15000 EID: 1@1 Ename: Kalyan Designation: Softwaretngineer Doe: 1¢/1@/20e1 03: 15/8/2023 ‘Age: 23 Experience: 1 Basic pay: 15000 Test Case - 2 User Output 320 John Doe Software Engineer ID: 111524104022 | Page No: 19 RM.D. Engineering College 2024-2028-ECE-A. 15.05 1990 10.08 2015 5000 EID: 320 Ename: John Doe Designation: Software Engineer oa: 15/5/1996 Do: 10/8/2615 Age: 34 Experience: 9 Basic pay: 5600 ID: 111524104022 | Page No: 20 RM.D. Engineering College 2024-2028-ECE-A. Exp. Name: Sum of marks of the student Date: 2024-12- S.No: 10 by using pointers 2B Aim: Write a C++ program to print the sum of the marks in 5 subjects of 2 students by using structures and functions. Input Format: +The frst input line reads a string representing the name of the frst student + The second input line reads 5 space-separated integers representing the marks of first student 5 subjects +The thitd input line reads a string representing the name of the second student + The fourth input line reads 5 space-separated integers representing the marks of the second student in 5 subjects Output Format: + The first output line prints the name ofthe fist student and the sum of marks separated by space + The second output line prints the name of the second student and the sum of marks separated by space Source Code: Sumofitarks cpp ID: 111524104022 | Page No: 21 RM.D. Engineering College 2024-2028-ECE-A. #include using namespace std; J/ write your code in the following structure and the function struct Student — { string nane; int marks[5]3 u int calculateSum(const Student& student) { int sum = 0; for (int ic 5; itt) { student.marks[ i]; sum return sun; int main() { Student student, student2; getline(cin, studenti.name) ; for (int i=; 1 55 +44) { cin >> student1.marks[i]; } cin.ignore(); // Clear the newline character fron the input buffer getline(cin, student2.name); for (int 4 = 8; 1.< 5; +4) { cin >> student2.marks[i]; ) cin.ignore(); // Clear the newline character fron the input buffer int sum = calculateSum(student1) ; Ant sum2 = caleulateSum(student2) ; ID: 111524104022 | Page No: 22 € a g 8 g 3 g g ¥ % 2 3 8 2 2 RMD. Eni cout << studenti.name << cout << student2.nane << "ce sum << endl; "ce sum2 /] Inline function to calculate the naxinun of elenents in an integer array inline int maxArray( inttarr,int size) { int maxearr[0]; for(int ist;icsize;++i){ if(are[i}omax){ naxzarr(i]; } return max; /I Inline function to calculate the minimum of elenents in an integer array inline int minArray( int*arr,int size a7 int min=arr[@]; for(int Aatstesize;+4i){ Af(are[i]> size; int® arr = new int[size]; std::cout << " for (int i = 0; i < size; ++i) { std::cout << "Enter integer std::cin >> arr[i]s nter " << size << " integers:" << std::endl «isle } int maximum = maxArray(arr, size); ID: 111524104022 | Page No: 25 2024-2028-ECE-A. e & 3 3 & 2 z g int minimum std::cout << std::cout <« minarray(arr, size); faximun of array elements: linimun of array elements: << maximum << std::endl]; << minimum <¢ std::endl]; return 2; Execution Results - All test cases have succeeded! Test Case - 1 User Output Size of the array: 5 Enter 5 integers: Enter integer 1: 10 Enter integer 2: 20 Enter integer 3: 30 Enter integer 4: 40 Enter integer 5: 50. Maximum of array elements: 58 Mininun of array elements: 16 Test Case - 2 User Output Size of the array: 4 Enter 4 integers: Enter integer 1: 12 Enter integer 2 23 Enter integer 3: 123, ID: 111524104022 | Page No: 26 RM.D. Engineering College 2024-2028-ECE-A. Enter integer 4: 41 Maximum of array elements: 123 Minimum of array elements: 12 ID: 111524104022 Page No: 27 RM.D. Engineering College 2024-2028-ECE-A.

You might also like