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

Program 1

Please

Uploaded by

pubgwala772
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)
40 views

Program 1

Please

Uploaded by

pubgwala772
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/ 25

Program 1

Q1 Write a program to perform all arithmetic operations with 2 number

#include <iostream>
using namespace std;
int main() {
int x = 25;
int y = 13;
cout << "Total of x + y = " << (x + y) << endl;
}

Output

Program2
Q2 Write a program to find if a number is even or add

Program3
Q4 Write a program to find greatest among 3 number
#include <iostream>
using namespace std;
int main() {

double n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


cout << "Largest number: " << n1;

else if(n2 >= n1 && n2 >= n3)


cout << "Largest number: " << n2;

else
cout << "Largest number: " << n3;

return 0;

Program4

Q4 Write a program to find the prime number


#include <iostream>
#include <cmath>
using namespace std;
int main() {
int i, n;
bool is_prime = true;

cout << "Enter a positive integer: ";


cin >> n;

if (n == 0 || n == 1) {
is_prime = false;
} else {
for (i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
}

if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";

return 0;
}
Program5
Q5 Write a program to find the smallest among three number
#include <bits/stdc++.h>
using namespace std;

int main()
{
int a = 5, b = 7, c = 10;

if (a <= b && a <= c)


cout << a << " is the smallest";

else if (b <= a && b <= c)


cout << b << " is the smallest";

else
cout << c << " is the smallest";

return 0;
}
Program6
Q6 program to calculate the division of a student occording to mark obtained in
4 subject

#include <iostream>
using namespace std;

int main() {
float subject1, subject2, subject3, subject4, average
cout << "Enter the marks obtained in subject 1: ";
cin >> subject1;
cout << "Enter the marks obtained in subject 2: ";
cin >> subject2;
cout << "Enter the marks obtained in subject 3: ";
cin >> subject3;
cout << "Enter the marks obtained in subject 4: ";
cin >> subject4;
average = (subject1 + subject2 + subject3 + subject4) / 4;
cout << "The average marks are: " << average << endl;
if (average >= 60) {
cout << "First Division" << endl;
} else if (average >= 50) {
cout << "Second Division" << endl;
} else if (average >= 35) {
cout << "Third Division" << endl;
} else {
cout << "Fail" << endl;
}
return 0;

input

Program7
Write a program to display even number between 1 and 15 (while loop )
#include <iostream>

using namespace std;

int main() {
int num = 2;

cout << "Even numbers between 1 and 15 are: ";

while (num <= 15) {


if (num % 2 == 0) {
cout << num << " ";
}
num++;
}

cout << endl;

return 0;
}

program 8
Q8 writ a program to multiplication table of a given number
#include <iostream>

int main() {
int number;

std::cout << "Enter a number: ";


std::cin >> number;
std::cout << "Multiplication table of " << number << ":\n";
for (int i = 1; i <= 10; ++i) {
std::cout << number << " x " << i << " = " << number * i << std::endl;
}
return 0;
}

Input

Program 9
Q9 Write a program to calculate the average of n number
#include <iostream>
using namespace std;

int main() {
int n;
double sum = 0.0;

cout << "Enter the number of elements: ";


cin >> n;
if (n <= 0) {
cout << "Number of elements must be greater than zero." << endl;
return 1;
}

for (int i = 0; i < n; ++i) {


double num;
cout << "Enter number " << i + 1 << ": ";
cin >> num;
sum += num;
}

double average = sum / n;

cout << "The average of the entered numbers is: " << average << endl;

return 0;
}
Program 10
Q10 write a program to display fibonaci series of n number
using for loop
#include <iostream>
using namespace std;

int main() {
int n, t1 = 0, t2 = 1, nextTerm;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {


if(i == 1) {
cout << t1 << " ";
continue;
}
if(i == 2) {
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";


}

return 0;
}
Q11. Write a program to show pass by address work?
#include <iostream>
void increment(int *ptr);

int main() {
int num = 5;

std::cout << "Before increment: " << num << std::endl;

increment(&num);

std::cout << "After increment: " << num << std::endl;

return 0;
}

void increment(int *ptr) {


(*ptr)++;
}

Q12. Write a program to show pass by value work?


#include <iostream>

// Function that takes an integer parameter by value


void passByValue(int num) {
num = 20; // Modify the parameter
std::cout << "Inside passByValue function, num: " << num << std::endl;
}

int main() {
int originalNum = 10;

std::cout << "Before calling passByValue, originalNum: " << originalNum << std::endl;

// Call the function and pass the variable by value


passByValue(originalNum);
std::cout << "After calling passByValue, originalNum: " << originalNum << std::endl;

return 0;
}

Q13 Write a program to show the concept of function overloading to calculate


area where same name function differs in number of parametes?
#include <iostream>
#include <cmath>

float area(float radius);


float area(float length, float breadth);
float area(int base, int height);

int main()
{
float radius = 3.5;
float length = 4.2, breadth = 2.8;
int base = 5, height = 8;

std::cout << "Area of circle with radius " << radius << " is: " << area(radius) <<
std::endl;
std::cout << "Area of rectangle with length " << length << " and breadth " <<
breadth << " is: "
<< area(length, breadth) << std::endl;

std::cout << "Area of triangle with base " << base << " and height " << height
<< " is: "
<< area(base, height) << std::endl;

return 0;
}

float area(float radius)


{
return M_PI * radius * radius;
}

float area(float length, float breadth)


{
return length * breadth;
}

float area(int base, int height)


{
return 0.5 * base * height;
}
Q14. program to input and the elemants of 2-dimensional array ?
#include <iostream>

int main() {
int rows, cols;

std::cout << "Enter the number of rows: ";


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

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

return 0;
}

Q16 program to calculate the product of two matrices?


#include <iostream>

int main() {
int rows1, cols1, rows2, cols2;

// Input dimensions of the first matrix


std::cout << "Enter the number of rows for the first matrix: ";
std::cin >> rows1;
std::cout << "Enter the number of columns for the first matrix: ";
std::cin >> cols1;
// Input dimensions of the second matrix
std::cout << "Enter the number of rows for the second matrix: ";
std::cin >> rows2;
std::cout << "Enter the number of columns for the second matrix: ";
std::cin >> cols2;

// Check if the multiplication is possible


if (cols1 != rows2) {
std::cout << "Matrix multiplication not possible. Number of columns in the
first matrix must equal the number of rows in the second matrix." << std::endl;
return 1;
}

// Declare the matrices


int matrix1[rows1][cols1];
int matrix2[rows2][cols2];
int product[rows1][cols2];

// Input elements of the first matrix


std::cout << "Enter the elements of the first matrix:" << std::endl;
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols
Q17 program to illustrate now a student date is entered and displayed using
student
#include <iostream>
#include <string>
class Student {
public:
std::string name;
int age;
std::string id;

void input() {
std::cout << "Enter student name: ";
std::cin.ignore();
std::getline(std::cin, name);
std::cout << "Enter student age: ";
std::cin >> age;
std::cout << "Enter student ID: ";
std::cin >> id;
}
void display() const {
std::cout << "Student Name: " << name << std::endl;
std::cout << "Student Age: " << age << std::endl;
std::cout << "Student ID: " << id << std::endl;
}
};

int main() {
Student student;

student.input();

student.display();

return 0;
}

Q 18. write a program to display the address and the content of a pointer ?
#include <iostream>

int main() {
int num = 42;
int* ptr = &num;
std::cout << "The address stored in the pointer: " << ptr << std::endl;

std::cout << "The content of the pointer (value pointed to): " << *ptr <<
std::endl;

std::cout << "The address of the pointer itself: " << &ptr << std::endl;

return 0;
}

Q 19 Write a program to using a reference variable as a arguments to swop the


value of pair of integer
#include <iostream>

void swap(int& a, int& b) {


int temp = a;
a = b;
b = temp;
}

int main() {
int x, y;
std::cout << "Enter the first integer: ";
std::cin >> x;
std::cout << "Enter the second integer: ";
std::cin >> y;

std::cout << "Before swapping: " << std::endl;


std::cout << "x = " << x << ", y = " << y << std::endl;

swap(x, y);

std::cout << "After swapping: " << std::endl;


std::cout << "x = " << x << ", y = " << y << std::endl;

return 0;
}
Q.20 write a program to find the number is palindrome ?
#include <iostream>

void swap(int& a, int& b) {


int temp = a;
a = b;
b = temp;
}

int main() {
int x, y;

std::cout << "Enter the first integer: ";


std::cin >> x;
std::cout << "Enter the second integer: ";
std::cin >> y;

std::cout << "Before swapping: " << std::endl;


std::cout << "x = " << x << ", y = " << y << std::endl;

swap(x, y);

std::cout << "After swapping: " << std::endl;


std::cout << "x = " << x << ", y = " << y << std::endl;

return 0;
}

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