KEMBAR78
Electronics Engg. Lab Programs | PDF | Namespace | Integer (Computer Science)
0% found this document useful (0 votes)
42 views23 pages

Electronics Engg. Lab Programs

Uploaded by

24et10ya155
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)
42 views23 pages

Electronics Engg. Lab Programs

Uploaded by

24et10ya155
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/ 23

MADHAV INSTITUTE OF TECHNOLOGY &

SCIENCE, GWALIOR
(Deemed University)
(Declared Under Distinct Category by Ministry of
Education, Government of India)
NAAC Accredited with A++ Grade

Department of Electronics Engineering


A
Practical File
On
Computer Programming Lab
(20241106)

SUBMITTED BY
Aryan Shrivastava
BTET24O1034
ET A (Batch B1)
Semester I
Electronics and Telecommunication Engineering

SUBMITTED TO
Dr. Himanshu Singh
Session: July – Dec. 2024
TABLE OF CONTENTS

S.No. Name of the Program Date Remarks


1. WAP to perform addition, subtraction, multiplication and division of
integer and floating values.
2. WAP to perform swapping between two user entered values without using
third variable.
3. WAP to take temperature from the user in Fahrenheit, then convert

and display the temperature in Celsius and Kelvin.


4. WAP to calculate and display Simple Interest where the principle, rate and
time are given by the user.
5. WAP to calculate and print the values of sinθ, cosθ and tanθ using

math.h library.
6. WAP to implement Pythagoras Theorem.
7. WAP to display whether a user entered number is even or odd.

8. WAP to check and display whether a user entered number is divisible

by 30 or not (using nested if).


9. WAP to find and display the greatest number among the three numbers
entered by the user.
10. WAP to check and print whether a user entered number is negative,
positive or zero.
11. WAP to print whether a user entered character is vowel or consonant
using switch-case.
12. WAP to print mathematical table of a user entered number

(example, 5*1=5) (for loop).


13. WAP to find factorial of a user entered number using while loop.

14. WAP to print all the numbers between 1 to 100 whose sum of the is
even (do-while loop).
15. WAP to find factorial of a user entered number using recursion.

16. WAP to print the maximum and minimum element of a user entered
ID array and sort the array elements in ascending and descending
order.
17. WAP to search an element and print its position in a user entered 2D
array.
18. WAP to take enrollment number, name, 5 subject marks form students
and calculate and print percentage along with their respective
enrollment numbers using structure.
Program 1: WAP to perform addition, subtraction, multiplication and division
of integer and floating values.

Code:
#include <iostream>
using namespace
std;

int main()
{
float a, b, c, d, e, f;
cout << "Enter the value of a:
"; cin >> a;
cout << "Enter the value of b:
"; cin >> b;

cout << "Sum:


"; c = a + b;
cout << c << endl;

cout << "Subtraction:


"; d = a - b;
cout << d << endl;
cout << "Multiplication:
"; e = a * b;
cout << e << endl;

cout << "Division:


"; if (b != 0)

f = a / b;
cout << f << endl;
}
else
{
cout << "Error: Division by zero!" << endl;
}
return 0;
}

Output:
Program 2: WAP to perform swapping between two user entered values
without using third variable.

Code:
#include <iostream>
using namespace std;

int main() {
int a, b;

// Taking input from the user


cout << "Enter the first number (a): ";
cin >> a;
cout << "Enter the second number (b): ";
cin >> b;

// Swapping without a third variable


a = a + b;
b = a - b;
a = a - b;

// Output the swapped values


cout << "After swapping: a = " << a << " b = " << b << endl;

return 0;
}

Output:
Program 3: WAP to take temperature from the user in Fahrenheit, then convert
and display the temperature in Celsius and Kelvin.

Code:
#include <iostream>
using namespace std;

int main() {
float fahrenheit, celsius, kelvin;

// Taking input from the user


cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;

// Conversion to Celsius
celsius = (fahrenheit - 32) * 5 / 9;

// Conversion to Kelvin
kelvin = celsius + 273.15;

// Display the results


cout << "Temperature in Celsius: " << celsius << "°C" << endl;
cout << "Temperature in Kelvin: " << kelvin << "K" << endl;

return 0;
}

Output:
Program 4: WAP to calculate and display Simple Interest where the principle,
rate and time are given by the user.

Code :

#include <iostream>
using namespace std;

int main() {
float principal, rate, time, simpleInterest;

// Taking input from the user


cout << "Enter the principal amount: ";
cin >> principal;

cout << "Enter the annual interest rate (in %): ";
cin >> rate;

cout << "Enter the time (in years): ";


cin >> time;

// Simple Interest formula: SI = (Principal * Rate * Time) / 100


simpleInterest = (principal * rate * time) / 100;

// Display the calculated Simple Interest


cout << "The Simple Interest is: " << simpleInterest << endl;

return 0;
}

Output:
Program 5: WAP to calculate and print the values of sinθ, cosθ and tanθ using
math.h library.

Code :

#include <iostream>
#include <cmath> // Required for sin, cos, and tan functions
using namespace std;

int main() {
double theta, radians, sinValue, cosValue, tanValue;

// Taking input from the user in degrees


cout << "Enter the angle in degrees: ";
cin >> theta;

// Converting degrees to radians since math functions expect radians


radians = theta * (M_PI / 180.0);

// Calculating sin(θ), cos(θ), and tan(θ)


sinValue = sin(radians);
cosValue = cos(radians);
tanValue = tan(radians);

// Displaying the results


cout << "sin(" << theta << " degrees) = " << sinValue << endl;
cout << "cos(" << theta << " degrees) = " << cosValue << endl;
cout << "tan(" << theta << " degrees) = " << tanValue << endl;

return 0;
}

Output:
Program 6: WAP to implement Pythagoras Theorem.
Code:

#include <iostream>
#include <cmath> // for sqrt() function

using namespace std;

int main() {
double a, b, c;

// Input values for the two sides


cout << "Enter the length of side a: ";
cin >> a;
cout << "Enter the length of side b: ";
cin >> b;

// Using Pythagoras theorem to calculate the hypotenuse


c = sqrt((a * a) + (b * b));

// Output the result


cout << "The length of the hypotenuse is: " << c << endl;

return 0;
}

Output:
Program 7: WAP to display whether a user entered number is even or
odd.

Code :

#include <iostream>
using namespace std;

int main()
{
int a;
cout<<"Enter the value of a:";
cin>> a;
if (a % 2 == 0)//condition for any variable to be even;
{
cout<<"The Entered value is even";
}
else{
cout<<"The Entered value is odd";
}
return 0;
}

Output :
Program 8: WAP to check and display whether a user entered number is
divisible
by 30 or not (using nested if).

Code:

#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;

if (number % 2 == 0) { // Check divisibility by 2


if (number % 3 == 0) { // Check divisibility by 3
if (number % 5 == 0) { // Check divisibility by 5
cout << number << " is divisible by 30" << endl;
} else {
cout << number << " is not divisible by 30 (not divisible by 5)" << endl;
}
} else {
cout << number << " is not divisible by 30 (not divisible by 3)" << endl;
}
} else {
cout << number << " is not divisible by 30 (not divisible by 2)" << endl;
}
return 0;
}
Output :
Program 9: WAP to find and display the greatest number among the
three numbers entered by the user.

Code :

#include <iostream>
using namespace std;

int main() {
int num1, num2, num3;

// Input three numbers


cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;

int greatest;

// Compare the numbers using conditional statements


if (num1 >= num2 && num1 >= num3) {
greatest = num1;
} else if (num2 >= num1 && num2 >= num3) {
greatest = num2;
} else {
greatest = num3;
}

// Output the greatest number


cout << "The greatest number is: " << greatest << endl;

return 0;
}
Output:
Program 10: WAP to check and print whether a user entered number
is negative, positive or zero.

Code :

#include <iostream>
using namespace std;

int main() {
int number;

// Input number from user


cout << "Enter a number: ";
cin >> number;

// Check whether the number is negative, positive or zero


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

Output :
Program 11: WAP to print whether a user entered character is vowel
or consonant using switch-case.

Code:
#include <iostream>
using namespace std;

int main() {
char ch;

// Input character from user


cout << "Enter a character: ";
cin >> ch;

* // Convert to lowercase to handle both uppercase and lowercase letters


ch = tolower(ch);
// Switch statement to check for vowels
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << ch << " is a vowel." << endl;
break;
default:
// Check if the entered character is an alphabet
if ((ch >= 'a' && ch <= 'z')) {
cout << ch << " is a consonant." << endl;
} else {
cout << "Invalid input, please enter an alphabet." << endl;
}
}
return 0;
}
Output :
Program 12: WAP to print mathematical table of a user entered number
(example, 5*1=5) (for loop).

Code:

#include <iostream>
using namespace std;

int main() {
int number;

// Input number from user


cout << "Enter a number to print its multiplication table: ";
cin >> number;

// Using for loop to print the multiplication table


for (int i = 1; i <= 10; i++) {
cout << number << " * " << i << " = " << number * i << endl;
}
return 0;
}

Output :
Program 13: WAP to find factorial of a user entered number using
while loop.

Code :

#include <iostream>
using namespace std;

int main() {
int number, factorial = 1, i = 1;

// Input number from user


cout << "Enter a number to find its factorial: ";
cin >> number;

// Using while loop to calculate the factorial


while (i <= number) {
factorial *= i; // Multiply factorial by i
i++; // Increment i
}

// Output the factorial result


cout << "The factorial of " << number << " is " << factorial << endl;

return 0;
}

Output :
Program 14: WAP to print all the numbers between 1 to 100 whose
sum of the is even (do-while loop).

Code :
#include <iostream>
using namespace std;

// Function to calculate the sum of digits of a number


int sumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

int main() {
int number = 1;

// Using do-while loop to iterate from 1 to 100


do {
int sum = sumOfDigits(number); // Calculate the sum of digits

// Check if the sum of digits is even


if (sum % 2 == 0) {
cout << number << " "; // Print the number
}

number++; // Increment the number


} while (number <= 100);

cout << endl;


return 0;
}

Output :
Program 15: WAP to find factorial of a user entered number using
recursion.

Code :
#include <iostream>
using namespace std;

// Function to calculate factorial using recursion


int factorial(int n) {
// Base case: if n is 0 or 1, factorial is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive case: n * factorial of (n-1)
else {
return n * factorial(n - 1);
}
}

int main() {
int num;

// Input from the user


cout << "Enter a number to find its factorial: ";
cin >> num;

// Check for valid input (non-negative integer)


if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
// Call the factorial function and display the result
cout << "The factorial of " << num << " is " << factorial(num) << "." << endl;
}
return 0;
}
Output :
Program 16: WAP to print the maximum and minimum element of a
user entered ID array and sort the array elements in ascending and
descending order

Code :
#include <iostream>
#include <algorithm> // For sort function
using namespace std;

int main() {
int n;

// Input: Size of the array


cout << "Enter the number of elements in the array: ";
cin >> n;

// Declare an array of size n


int arr[n];

// Input: Array elements from the user


cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}

// Finding the maximum and minimum elements


int maxElem = arr[0], minElem = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] > maxElem) {
maxElem = arr[i];
}
if (arr[i] < minElem) {
minElem = arr[i];
}
}
// Output: Maximum and Minimum elements
cout << "Maximum element: " << maxElem << endl;
cout << "Minimum element: " << minElem << endl;

// Sorting the array in ascending order


sort(arr, arr + n);

// Output: Array in ascending order


cout << "Array in ascending order: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;

// Sorting the array in descending order


sort(arr, arr + n, greater<int>());

// Output: Array in descending order


cout << "Array in descending order: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
Output :
Program 17: WAP to search an element and print its position in a
user entered 2D array.

Code :
#include <iostream>
using namespace std;

int main() {
int rows, cols;

// Input: number of rows and columns


cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;

// Declare a 2D array of size [rows][cols]


int arr[rows][cols];

// Input: elements of the 2D array


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

int target;
cout << "Enter the element to search: ";
cin >> target;

// Search for the element in the 2D array


bool found = false;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (arr[i][j] == target) {
// If the element is found, print its position (1-based index)
cout << "Element " << target << " found at position (" << i + 1 << ", " << j + 1
<< ")." << endl;
found = true;
break; // Break inner loop as we found the element
}
}
if (found) break; // Break outer loop if the element is found
}

if (!found) {
cout << "Element " << target << " not found in the array." << endl;
}

return 0;
}

Output :
Program 18: WAP to take enrollment number, name, 5 subject
marks form students and calculate and print percentage along with
their respective enrollment numbers using structure.

Code :
#include <iostream>
#include <string>
using namespace std;

// Define a structure to store student details


struct Student {
string enrollmentNumber;
string name;
float marks[5];
float percentage;

// Function to calculate percentage


void calculatePercentage() {
float total = 0;
for (int i = 0; i < 5; ++i) {
total += marks[i];
}
percentage = (total / 500) * 100; // Assuming total marks per subject is 100
}
};

int main() {
int n;

// Input the number of students


cout << "Enter the number of students: ";
cin >> n;

// Create an array of students


Student students[n];

// Input details for each student


for (int i = 0; i < n; ++i) {
cout << "\nEnter details for student " << i + 1 << endl;
cout << "Enrollment Number: ";
cin >> students[i].enrollmentNumber;
cin.ignore(); // to clear the newline character after enrollment number
cout << "Name: ";
getline(cin, students[i].name);
cout << "Enter marks for 5 subjects: ";
for (int j = 0; j < 5; ++j) {
cin >> students[i].marks[j];
}

// Calculate the percentage for the student


students[i].calculatePercentage();
}

// Output the details of each student


cout << "\nStudent Details and Percentage:" << endl;
for (int i = 0; i < n; ++i) {
cout << "\nStudent " << i + 1 << endl;
cout << "Enrollment Number: " << students[i].enrollmentNumber << endl;
cout << "Name: " << students[i].name << endl;
cout << "Marks in 5 subjects: ";
for (int j = 0; j < 5; ++j) {
cout << students[i].marks[j] << " ";
}
cout << "\nPercentage: " << students[i].percentage << "%" << endl;
}

return 0;
}

Output :

You might also like