Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Experiment 5: Find out the roots of quadratic equation

Input:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout<<"Find the roots of quadratic equation \nName: Ayush \nRoll no. 3554"<<endl;
int a, b, c;
char chk;
float r1, r2, disc, realPart, imgPart;
chk='y';
while(chk=='y'||chk=='Y')
{cout << "Enter coefficients a b and c: ";
cin >> a >> b >> c;
disc = b*b - 4*a*c;
if (disc > 0) {
r1 = (-b + sqrt(disc)) / (2*a);
r2 = (-b - sqrt(disc)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "r1 = " << r1 << endl;
cout << "r2 = " << r2 << endl;
}
else if (disc == 0) {
cout << "Roots are real and same." << endl;
r1 = -b/(2*a);
cout << "r1 = r2 =" << r1 << endl;
}
else {
realPart = -b/(2*a);
imgPart =sqrt(-disc)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "r1 = " << realPart << "+" << imgPart << "i" << endl;
cout << "r2 = " << realPart << "-" << imgPart << "i" << endl;
}
cout<<"\nWant To Publish New List? (Y or N)";
cin>>chk;
}
return 0;
}
}

cout<<"\nWant T Experiment 7: Search an element and print its index in


Array
Input:
#include<iostream>
using namespace std;
int main()
{
cout<<"Design a program to Search an element and print its index \nby Name: Ayush \nRoll no.
3554"<<endl;
char chk;
chk='y';
while(chk=='y'||chk=='Y')
{int n,k,ans=-1;
cout<<"Enter size of array"<<endl;
cin>>n;
int arr[n];
cout<<"Enter elements of array"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter element to be searched"<<endl;
cin>>k;
for(int i=0;i<n;i++)
{
if(arr[i]==k)
{
ans=i;
if(ans!=-1)
cout<<"The element "<<k<<" is present at index "<<ans<<endl;
else
cout<<"The element "<<k<<" is not there in the array";
continue;
}
}
cout<<"\nWant To search another element? (Y or N)";
cin>>chk;
}
return 0;
}
Experiment 8: Create a 2D array and print it like a matrix
Input:

#include<iostream>
using namespace std;
int main()
{cout<<"To Create a 2-D Array and Print it Like a Matrix \nName: Aayush \nRoll
no. 3554"<<endl;
char chk;
chk='y';
while(chk=='y'||chk=='Y')
{
int rows, cols, i, j;
cout<<"Enter Rows for Array: ";
cin>>rows;
cout<<"Enter Columns for Array: ";
cin>>cols;
int arr[rows][cols];
cout<<"\nEnter "<<rows<<"*"<<cols<<" Array Elements : \n";
for(i=0; i<rows; i++)
{for(j=0; j<cols; j++)
{cout<<" ";
cin>>arr[i][j];}}
cout<<"\nTwo Dimensional Array is : \n";
for(i=0; i<rows; i++)
{for(j=0; j<cols; j++)
{cout<<" "<<arr[i][j]<<" ";}
cout<<"\n";}
cout<<"\nWant to perform again? (Y or N): ";
cin>>chk;
}
return 0;
}

You might also like