Posts

Showing posts from December, 2020

Program to identify a given character is a vowel or a consonant

 Program to identify a given character is a vowel or a consonant #include<iostream> using namespace std; int main() { char c; cout<<"enter an alphabet ";  //prints the msg between "" cin>>c; int isuppercase,islowercase; // declaration of variables islowercase=(c=='a'||c=='e'||c=='i'||c=='o'||c=='u');  / / initialization of variables isuppercase=(c=='A'||c=='E'||c=='I'||c=='O'||c=='U');  // initialization of variables if(isuppercase || islowercase) { cout<<"you entered "<< c <<" is a vowel";     //prints the msg between "" } else { cout<<"you entered "<< c <<" is a consonant";     //prints the msg between "" } return 0; } Output: enter an alphabet q you entered q is a consonant

program for pointer to a pointer in C++

Program for pointer to a pointer in C++ #include<iostream> using namespace std; int main() { int *p,**pp; int x; p=&x; //pointer p stores the address of variable x pp=&p; // double pointer stores the addres of pointer p cout<<"the address of x is : "<<p<<endl; //prints the address of variable x cout<<"the address of pointer p is : "<<pp<<endl;//prints the address of pointer p } output: the address of x is : 0x6ffdfc the address of pointer p is : 0x6ffe00

Program for Constructor in C++

 Program for Constructor in C++: #include<iostream> using namespace std; class alpha  { int x; public: alpha(int i)  //constructor  { x=i; cout<<"alpha initialized\n"; } void showx() { cout<<"x="<<x<<endl; } }; class beta { float y; public: beta(float b) { y=b; cout<<"Beta initialized\n"; } void showy() { cout<<"y="<<y<<endl; } }; class gamma:public alpha,public beta { int m,n; public: gamma(int a,float b,int c,int d):alpha(a),beta(b) { m=c; n=d; cout<<"Gamma initialized\n"; } void showmn() { cout<<"M="<<m<<endl; cout<<"N="<<n<<endl; } }; int main() { gamma g(5,30.3,23,12); cout<<"\n"; g.showx(); g.showy(); g.showmn(); } Output: alpha initialized Beta initialized Gamma initialized x=5 y=30

Program for average of 4 numbers in C++

 Program for average of 4 numbers in C++: #include<iostream> using namespace std; int main() { int a,b,c,d; double avg; cout<<"enter any four no"; cin>>a>>b>>c>>d; avg=(a+b+c+d)/4; cout<<"average is "<<avg; return 0; } output: enter any four no1 2 3 4 average is 2

Program for ASCII value for a character entered by user in C++

 Program for ASCII value for a character: #include<iostream> using namespace std; int main () { char a; cout<<"enter value for a"; cin>>a; cout<<"ASCII value of "<<a<<" is "<<int(a); return 0; } output : enter value for a  q ASCII value of q is 113 here, int(a) is a function used to find the ASCII value of an character.

Program to print Fibonacci series in C++

 Program to print Fibonacci series in C++: #include<iostream> using namespace std; int main() { int n,fib2; int fib0=0,fib1=1; cout<<"enter how many terms of Fibonacci you want : "; cin>>n; for(int i=0;i<=n;i++) { fib2=fib1+fib0; fib0=fib1; fib1=fib2; cout<<fib2<<" "; } return 0; } output: enter how many terms of Fibonacci you want : 8 1 2 3 5 8 13 21 34 55 or using WHILE LOOP : #include<iostream> using namespace std; int main() { int t1=0,t2=1,nextterm,num,i=1; cout<<"enter number of terms of fibonacci series to be displayed"; cin>>num; while(nextterm<=num) { nextterm=t1+t2; t1=t2; t2=nextterm; cout<<nextterm<<" "; } } output: enter how many terms of Fibonacci you want : 8 1 2 3 5 8 13 21 34 55

Program for class and class data member and member function

Program for class: #include<iostream> using namespace std; class rectangle { private: float length; float width; float area; public: void setdata(float, float); void calarea(); float getlength(); float getwidth(); float getarea(); }; void rectangle::setdata(float l, float w) { length=l; width=w; } void rectangle ::calarea() { area=length*width; } float rectangle ::getlength() { return length; } float rectangle ::getwidth() { return width; } float rectangle ::getarea() { return area; } int main() { rectangle box; float boxlength,boxwidth; cout<<" what is length "; cin>>boxlength; cout<<"what is width "; cin>>boxwidth; box.setdata(boxlength,boxwidth); box.calarea(); cout<<"here is the rectangle's data : \n"; cout<<"\n length : "<< box.getlength()<<endl; cout<<" width : "<< box.getwidth() <<endl; cout&

Program for "size of " operator in C++

size of operator returns the size of the given data type.   syntax :  sizeof(datatype); Program for "size of " operator in C++: #include<iostream> using namespace std; int main() { cout<<"\nsize of int is "<<sizeof(int); cout<<"\nsize of short int is "<<sizeof(short int); cout<<"\nsize of char is "<<sizeof(char); cout<<"\nsize of double is "<<sizeof(double); cout<<"\nsize of float is "<<sizeof(float); return 0; } Output: size of int is 4 size of short int is 2 size of char is 1 size of double is 8 size of float is 4   

Program to print a rectangle of * in C++ programming language

 Program to print a rectangle of * in C++ programming language: #include<iostream> using namespace std; int main() { int height,length; cout<<"enter the lenghth "; cin>>length; cout<<"\nenter the height "; cin>>height; for(int i=0;i<=length;i++) { for(int j=0;j<=height;j++) { cout<<"*"; } cout<<endl; } return 0; } output: enter the length 10 enter the height 10 *********** *********** *********** *********** *********** *********** *********** *********** *********** *********** ***********

Program to find the smallest element in the given array in C programming language

  Program to find the smallest element in the given array: #include<stdio.h> int main() { int i,n,arr[20],pos,small; printf("enter no of element in array \n"); scanf("%d",&n); printf("array element are =\n"); for(i=0;i<n;i++) { printf("\n arr[i]= ",i); scanf("%d",&arr[i]); } small=arr[0]; pos=0; for(i=0;i<n;i++) { if(arr[i]<small) { small=arr[i]; pos=i; } } printf("Smallest number in the array is %d\n",small); printf("  Position of the element is %d",pos); } output: enter number of elements in array:3 12 33 2 smallest element in array is 2.

Program for area of a circle using class in C++

Program for area of a circle using class:  #include<iostream> using namespace std; class circle { float r; float area; public: void getdata() { cout<<"Enter the value: "<<endl; cin>>r; } void showdata() { area=3.14*r*r; cout<<"Area is: "<<area; }  }; int main() { circle c; c.getdata(); c.showdata(); } output: Enter the value: 4 Area is: 50.24

Program for strlen function in string function

 Program for strlen funtion: #include<iostream> #include<cstring> using namespace std; int main() { char str[10]="HELLO"; char str2[100]="world"; char str3[20]; int len; strcpy(str3,str); cout<<"\nstrcpy(str3,str) : "<<str3; strcpy(str, str2); cout<<"\nstrcpy(str, str2): "<<str; len=strlen(str); cout<<"\nthe lenghth of "<<str<<" is "<<len; } output: strcpy(str3,str) : HELLO strcpy(str, str2): world the lenghth of world is 5

Program For Printing Factors Of Any Number Entered By The User

 Program For Printing Factors Of Any Number Entered By The User: #include<iostream> using namespace std; int main() { int i,n; cout<<"enter any value"; cin>>n;  //stores value entered by user in n cout<<"factors of "<<n<<" are : "; for(i=1;i<=n;i++) { if(n%i==0) { cout<<i<<","; } } return 0; } output: enter any value42 factors of 42 are : 1,2,3,6,7,14,21,42, Explanation of the above program: #include<iostream> - is used for the cout and cin objects, it supports the input output operations. using namespace std; - tells the compiler to use the std(standerd) namespace. int main() - declares the main() function which is the point from where all C++ program begin their execution. // this is used to denote single line comment in the C++ program for multiple line comment we use /* ------ */ . in the dashed symbol we can put multiple line comment.