C++
C++
c)
*
* *
* * *
d) 1
121
13 31
1464 1
Code:
#include<iostream>
using namespace std;
int main()
{
int i,j,a,n,m,k;
cout<<"first pattern\n";
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
cout<<char(64+j);
for(k=1;k<=2*i-1;k++)
cout<<" ";
cout<<"\b\b";
for(j=j-2;j>=0;j--)
cout<<char(65+j);
cout<<"\n";
}
cout<<"second pattern"<<"\n";
{
for(i=0;i<=3;i++)
{
for(k=3;k>=i;k--)
{
cout<<"*"<<" ";
}
cout<<"\n";
}
}
cout<<"third pattern\n";
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
cout<<j<<"";
}
cout<<"\n";
}
cout<<"fourth pattern"<<"\n";
a=1;
cout<<a<<"\n";
for(i=1;i<=4;i++)
{
a=a*11;
cout<<a<<"\n";
}
return 0;
}
Output:
2: Write member function which when called asks pattern type: -if uses enters 11 then a
member function is called which generates first pattern using for loop. If user enters 12
then a member function is called which first pattern using do-while loop. If user enter 21
then a member function is called which generates second pattern using for loop and so on.
Code:
#include<iostream>
using namespace std;
int main()
{
int i,j,k,choice,a;
cout<<"enter your choice\n";
cin>>choice;
switch(choice)
{
case 11:
cout<<"PYRAMID USING FOR LOOP IS:\n\n";
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
cout<<char(64+j)<<" ";
}
for(k=1;k<=4*i-3;k++)
{
cout<<" ";
}
for(j=j-2;j>=0;j--)
{
cout<<char(65+j)<<" ";
}
cout<<"\n";
}
break;
case 12:
cout<<"PYRAMID USING WHILE LOOP IS:\n\n";
i=1;
while(i<=4)
{
j=1;
while(j<=i)
{
cout<<j<<" ";
j++;
}
i++;
cout<<"\n";
}
break;
case 21:
cout<<"PYRAMID USING WHILE LOOP IS:\n\n";
i=1;
while(i<=4)
{
j=1;
while(j<=5-i)
{
cout<<char(64+j)<<" ";
j++;
}
k=1;
while(k<=4*i-3)
{
cout<<" ";
k++;
}
j=j-2;
while(j>=0)
{
cout<<char(65+j)<<" ";
j--;
}
cout<<"\n";
i++;
}
break;
default:
{
cout<<"invalid number ";
break;
}
return 0;
}
Output:
3.Write a program to display number 1 to 10 in octal ,decimal and hexadecimal system.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Number\tOctal\tDecimal\tHexadecimal" << endl;
return 0;
}
Output:
4. Write a program using function to add,substract and multiply two matrices of order
3*3.You have to create one function for addition which accepts three array
arguments.First two array arguments are matrices to add and third matrix is
destination where the resultant of addition of first two matrices is stored.In similar way
create functions for matrix substraction and multiplication.
Code:
#include <iostream>
using namespace std;
int main() {
int matrix1[3][3], matrix2[3][3], result[3][3];
cout << "Enter elements of matrix 1:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix1[i][j];
}
}
cout << "Enter elements of matrix 2:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix2[i][j];
}
}
matrix_add(matrix1, matrix2, result);
cout << "\nResult of addition:\n";
display_matrix(result);
return 0;
}
Output:
5. Create a single program to perform following tasks without using library functions:
A) To reverse the string accepted as argument.
B) To count the number of characters in string passed as argument in form of character array.
C) To copy the one string to other string; passed as argument in form of character array and
destination character array without using library function.
D) To count no. of vowels, consonants in each word of a sentence passed as argument in form of
character array.
Code:
#include<iostream>
using namespace std;
class String
{
public:
int i,j,m,n;
char str1[10],str2[10],str3[10];
void reverse();
void length();
void copy();
void count();
void getdata();
};
void String::getdata()
{
cout<<"enter string1:"<<"\n";
cin>>str1;
}
void String::copy()
{
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
cout<<"first string="<<str1<<"\t";
cout<<"copied string="<<str2;
}
void String::length()
{
n=0;
for(i=0;str1[i]!='\0';i++)
{
n++;
}
cout<<"\nlength of the string is="<<n;
}
void String::reverse()
{
i=-1;j=0;
while(str1[++i]!='\0');
while(i>=0)
str2[j++]=str1[--i];
str2[j]='\0';
cout<<"\ngiven string is="<<str1;
cout<<"\treverse of string is="<<str2;
}
void String::count()
{
n=0;m=0;
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]=='a' && 'A'||str1[i]=='e'&&'E'||str1[i]=='i'&&'i'||str1[i]=='0'&&'0'||
str1[i]=='u'&&'u')
{
n++;
}
else{
m++;
}
}
cout<<"\nno.vowels:"<<m;
cout<<"\nno.of constants"<<n<<"\n";
}
int main()
{
String s;
s.getdata();
cout<<"****************************************************************";
s.length();
s.count();
s.copy();
s.reverse();
return 0;
}
Output:
6. Create a class student having data members to store roll no ,name of the student, name of three
subjects, max marks, min marks, obtained marks. declare an object of class student. provide
facilities to input data in data members and display result of student.
Code:
#include<iostream>//program6
using namespace std;
class student
{
private:
int i,rollno,m1,m2,m3,max,min;
char name[20],sub1[20],sub2[20],sub3[20];
float total,per;
public:
void getdata(void)
{
cout<<"enter the name of the student:\n";
cin>>name;
cout<<"enter the rollno of the student:\n";
cin>>rollno;
cout<<"enter the 3 subjects of the student:\n";
cin>>sub1>>sub2>>sub3;
cout<<"enter the marks 1st subject of the student:\n";
cin>>m1;
cout<<"enter the marks 2nd subject of the student:\n";
cin>>m2;
cout<<"enter the marks 3rd subject of the student:\n";
cin>>m3;
cout<<"enter the minimum marks\n";
cin>>min;
cout<<"enter the maximum marks\n ";
cin>>max;
}
void result()
{
cout<<sub1<<"="<<m1<<"\t\t"<<sub2<<"="<<m2<<"\t\t"<<sub3<<"="<<m3<<"\n";
total=m1+m2+m3;
cout<<"Name="<<name<<"\t"<<"Roll no="<<rollno<<"\t"<<"Total marks="<<total;
per =(total*100)/300;
cout<<"\npercentage="<<per<<"%"<<"\n";
if(per<60)
cout<<"fail in examination";
else
cout<<"pass in examination";
}
};
int main()
{
student s;
cout<<"******************inputting values*******************\n";
s.getdata();
cout<<"************************result*************\n";
s.result();
return 0;
}
Output
:
7. Create a class student having data members to store roll no., name of the student ,name
of three subjects, max marks ,min marks, obtained marks. declare array of object to hold
data of 3 students. provide facilities to display result of all students. provide also facility to
display result of specific student whose roll number is given.
Code:
#include<iostream>
using namespace std;
class student
{
private:
int i,rollno,m1,m2,m3,max,min;
char name[20],sub[10][3];
float total,per;
public:
void getdata();
void result()
{
cout<<"\nRoll no:-"<< rollno;
cout<<"\tName of the student= "<<name<<"\n";
for(i=0;i<1;i++)
{
cout<<"SUBJECT MARKS:"<<sub[i]<<"="<<m1<<" "<<sub[i+1]<<"="<<m2<<"
"<<sub[i+2]<<"="<<m3<<"\n";
}
total=m1+m2+m3;
cout<<"total marks = "<<total<<"\t";
per=(total*100)/300;
cout<<"percentage="<<per<<" ";
if(per<60)
cout<<"fail";
else
cout<<"pass";
cout<<"\n-------------------------------------------------";
}
};
void student::getdata()
{
cout<<"\nEnter the name of the student:"; cin>>name;
cout<<"Enter the rollno:"; cin>>rollno;
cout<<"Enter the name of 3 subject:";
for (i=0;i<=2;i++)
{
cin>>sub[i];
}
cout<<"Enter marks of 1st subject=";
cin>>m1;
cout<<"Enter marks of 2nd student=";
cin>>m2;
cout<<"Enter marks of 3rd student=";
cin>>m3;
cout<<"Enter the mininum marks=";
cin>>min;
cout<<"Enter the maximum marks=";
cin>>max;
}
int main()
{
student s[3];
int i;
cout<<"\n************************* inputing value**************:";
for(i=0;i<=2;i++)
{
s[i].getdata();
}
cout<<"\n***********************Result****************\n";
for(i=0;i<=2;i++)
{
s[i].result();
}
return 0;
}
Output:
8. Write a program of increment operator using static function.
Code:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int rollno;
string name;
int marks;
static int object_count;
Student() {
object_count++;
}
void getdata() {
cout << "Enter rollno and name and marks: ";
cin >> rollno >> name >> marks;
}
void display() {
cout << "rollno: " << rollno << ", name: " << name << ", marks: " << marks << endl;
}
};
int Student::object_count = 0;
int main() {
Student s1;
s1.getdata();
s1.display();
cout << "Object count: " << Student::object_count << endl;
Student s2;
s2.getdata();
s2.display();
cout << "Object count: " << Student::object_count << endl;
Student s3;
s3.getdata();
s3.display();
cout << "Object count: " << Student::object_count << endl;
cout << "Total object created: " << Student::object_count << endl;
return 0;}
Output:
9. Wap to create class complex having data members to store real and imaginary part. Part
following facilities:-
a) add two complex no. using objects.
b) subtract two complex no. using objects.
c) multiply two complex no. using objects.
d) divide two complex no. using objects.