You are on page 1of 13

Practical part

1. Write a program to find maximum of three numbers.


#include <iostream>
using namespace std;

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

cout<<"Enter the first number : ";


cin>>a;
cout<<"Enter the second number : ";
cin>>b;
cout<<"Enter the third number : ";
cin>>c;

int max;

if (a>b && a>c) {


max = a;
} else if (b>c) {
max = b;
} else {
max = c;
}

cout<<"The required maximum number is = " << max << endl;


}

2. Write a program to find out factorial of an integer by using a function.


#include <iostream>
using namespace std;
int fact(int n) {
if ((n==0)||(n==1))
return 1;
else
return n*fact(n-1);
}
int main() {
int n;
cout<<"Enter the number : ";
cin>>n;
cout<<"Factorial of "<<n<<" is "<<fact(n)<<"\n";
return 0;
}

3. Write a program to print Fibonacci series upto n.


#include<iostream>
using namespace std;

int main()
{
int n,a=0,b=1,c;
cout<<"Enter the number : ";
cin>>n;
cout<<a<<" "<<b;
for(int i=2;i<n;i++)
{
c=a+b;
cout<<" "<<c;
a=b;
b=c;
}
cout<<\n;
return 0;
}

4. Write a program to count number of vowels in a given string.


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

int main()
{
char str[100];
int vowels = 0;
cout<<"Enter your desired sentence = ";
cin.getline(str, 100);

for(int i = 0; str[i]!= '\0' ; i++)


{
if(str[i]=='a'|| str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'
||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O' ||str[i]=='U')
{
vowels++;
}
}

cout << "Total Vowels : "<< vowels;

return 0;
}

5. Write a program to find out the value of y.


y = 1-3+5-7+9-11+….upto n.

#include<iostream>

using namespace std;

int main()

int i, y=0, a=2, n;

cout<<"Enter the n term odd value : ";

cin>>n;

for(i=1; i<=n; i=i+2)

if(a%2==0)

y=y+i;

else

y=y-i;

a=a+1;
}

cout<<y;

6. Write a program to find out the sum of the digits of an integer number.
#include<iostream>
using namespace std;
int main() {
int x, s = 0;
cout << "Enter the number : ";
cin >> x;
while (x != 0) {
s = s + x % 10;
x = x / 10;
}
cout << "\nThe sum of the digits : "<< s;
}

7. Write a program to find out HCF of 2 numbers.


#include <iostream>
using namespace std;

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

int temp, hcf;

if (x > y) {
temp = x;
x = y;
y = temp;
}

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


if (x%i == 0 && y%i == 0)
hcf = i;
}

cout<<"HCF of "<<x<<" and "<<y<<" is: "<<hcf<<"\n";


return 0;
}

8. Write a program to find out trace of a square matrix.


#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int mat[100][100],tr=0,i,j,m,n;
cout<<"Enter the order of the matrix : \n";
cin>>m>>n;
while(m!=n){
cout<<"Not a square matrix!\n";
exit(0);
}
cout<<"Enter the elements of the matrix : \n";
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cin>>mat[i][j];
}
}
cout<<"The matrix is : \n";
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cout<<mat[i][j];
cout<<"\t";
}
cout<<endl;
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j){
tr = tr + mat[i][j];
}
}
}
cout<<"The trace is "<<tr<<"\n";
}

9. Write a program to find out the sum of two matrixes and display the resultant matrix in its
form.
#include<iostream>
using namespace std;
int main()
{
int matOne[3][3], matTwo[3][3], matThree[3][3];
int i, j, k;
cout<<"Enter 9 Elements for First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matOne[i][j];
}
cout<<"\nEnter 9 Elements for Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matTwo[i][j];
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
for(k=0; k<3; k++)
matThree[i][j] = (matOne[i][j] + matTwo[i][j]);
}
}
cout<<"\nSum of the matrix are :\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matThree[i][j]<<"\t";
cout<<endl;
}
cout<<endl;
return 0;
}

10. Write a program to find the maximum and minimum of a list of integer.
#include <iostream>
using namespace std;
int main()
{
int array[50], *max, *min, size, i;
cout<<"Enter the number of integer in the list\n";
cin>>size;
cout<<"Enter integers\n";
for ( i = 0 ; i < size ; i++ )
cin>>array[i];
max = array;
min = array;
for (i = 0; i < size; i++)
{
if (*(array+i) > *max)
*max = *(array+i);
}
cout<<"Maximum integer in the list is "<< *max << "\n" ;
for (i = 0; i < size; i++)
{
if (*(array+i) < *min)
*min = *(array+i);
}
cout<<"Minimum integer in the list is "<< *min <<"\n";
return 0;
}

11. Write a program to find the value of y.


y= 1-2x+x2 .

#include<iostream>

using namespace std;

int main()

int x, y;

cout<<"Enter the value of x : ";

cin>>x;

y= (x*x)-2*x+1;
cout<<"The value of y : "<<y<<"\n";

12. Write a program to find out average of a list of integers.


#include<iostream>
using namespace std;

int main()
{
int i,n,x,sum=0;
float avg;

cout<<"How many numbers u want to enter :: ";


cin>>n;

for(i=1;i<=n;++i)
{
cout<<"\nEnter number "<<i<<" :: ";
cin>>x;

sum+=x;
}

avg=(float)sum/(float)n;

cout<<"\n\nSum of "<<n<<" Numbers :: "<<sum;

cout<<"\n\nAverage of "<<n<<" Numbers :: "<<avg<<"\n";

return 0;
}
13. Write a program to check a string is palindrom or not.
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string str)


{
int j = str.length() - 1;

for (int i = 0; i < j; i++, j--)


{
if (str[i] != str[j])
{
return false;
}
}

return true;
}

int main()
{
string word;
cout<<"Enter the word : ";
std::getline(cin, word);
for (int i = 0; i < 1; i++)
{
if (isPalindrome(word))
{
cout << word << " -> Palindrome" << endl;
}
else
{
cout << word << " -> Not a Palindrome" << endl;
}
}

return 0;
}

14. Write a program to find the ASCII value of a character.


#include <iostream>
using namespace std;
int main() {
char l;
cout << "Enter a character: ";
cin >> l;
cout << "ASCII Value of " << l << " is " << int(l)<<"\n";
return 0;
}

15. Construct a structure of ‘STUDENT’ with the data members


Roll No, Name, Marks obtained.
Write a program to insert data for n students and display their Roll No and Names who
scored more than 60 marks.

#include <iostream>

using namespace std;

struct student

char name[50];

int roll;

float marks;

} s[5];
int main()

cout << "Enter information of students: " << endl;

for(int i = 0; i < 5; ++i)

s[i].roll = i+1;

cout << "For roll number" << s[i].roll << "," << endl;

cout << "Enter name: ";

cin >> s[i].name;

cout << "Enter marks: ";

cin >> s[i].marks;

cout << endl;

cout << "Displaying Information: " << endl;

for(int i = 0; i < 5; ++i)

if(s[i].marks > 60)

cout << "\nRoll number: " << i+1 << endl;

cout << "Name: " << s[i].name << endl;

cout << "Marks: " << s[i].marks << endl;

return 0;
}

You might also like