C++ Basic Example for student and Programmer

Posted on

 

C++ Applications

1. Examine No. is Prime or Not Prime.

#embrace#embracevoid foremost(){  int i,no;  clrscr();  cout<<"Enter any num: ";  cin>>no;  if(no==1)  {  cout<<"Smallest prime num is 2";  }  for(i=2;i<no;i++)  {  if(no%i==0)  {  cout<<"Not prime num";  break;  }  } if(no==i) { cout<<"Prime num"; }  getch();}
Enter any num: 10Not Prime Num

2. Print subsequent Prime quantity.

Once we enter any quantity this code will print subsequent Prime quantity.
Instance: Suppose we enter 5 then subsequent prime quantity is 7.

Instance

#embrace#embracevoid foremost(){  int i,j=2,num;  clrscr();  cout<<"Enter any num: ";  cin>>num;  cout<<"Subsequent prime num: ";  for(i=num+1;i<3000;i++) {for(j=2;j<i;j++)  { if(i %j==0) { break;  } // if } // for if(i==j || i==1) { cout<<"t"<<i;break;} // if}  // outer forgetch();}

Output

Enter any num: 10Subsequent prime num 11

3. Fibonaccci Sequence in C++ with out Recursion.

Instance

#embrace utilizing namespace std;int foremost() {  int n1=0,n2=1,n3,i,quantity; cout<<"Enter the variety of parts: "; cin>>quantity; cout<<n1<<" "<<n2<<" "; //printing Zero and 1   for(i=2;i<quantity;++i) //loop begins from 2 as a result of Zero and 1 are already printed   {n3=n1+n2;cout<<n3<<" ";n1=n2;n2
=n3; } return 0; }

Output

Enter the variety of parts: 100 1 1 2 3 5 8 13 21 34 

4. Fibonnaci sequence utilizing recursion in C++.

Instance

#embrace  utilizing namespace std;void printFibonacci(int n){  static int n1=0, n2=1, n3;  if(n>0){ n3 = n1 + n2; n1 = n2; n2 = n3; cout<<n3<<" "; printFibonacci(n-1);  }  }  int foremost(){  int n;  cout<<"Enter the variety of parts: ";  cin>>n;  cout<<"Fibonacci Sequence: ";  cout<<"0 "<<"1 ";printFibonacci(n-2);  //n-2 as a result of 2 numbers are already printed   return 0;}

Output

Enter the variety of parts: 15  Fibonacci Sequence: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

4. Reverse a String in C++.

Instance

#embrace#embrace#embrace#embracevoid foremost(){ char str[100],temp; int i,j=0; clrscr(); cout<<"Enter any the string :"; will get(str);  //  will get perform for enter string i=0; j=strlen(str)-1;  whereas(i<j) { temp=str[i]; str[i]=str[j]; str[j]=temp; i++; j--; } cout<<"Reverse string is: "<<str; getch();}

Output

Enter any the string  : hiteshReverse string is : hsetih

5. Matrix Program in C++.

Addition of two matrix in c++

#embrace#embracevoid foremost(){int x[3][3],y[3][3],z[3][3],i,j;clrscr();cout<<"ENTER ELEMENTS OF 1st MATRIXn";for(i=0;i<3;i++){for(j=0;j<3;j++)cin>>x[i][j];}cout<<"ENTER ELEMENTS OF 2nd MATRIXn";for(i=0;i<3;i++){for(j=0;j<3;j++)cin>>y[i][j];}cout<<"MATRIX [X]";for(i=0;i<3;i++){cout<<"nn";for(j=0;j<3;j++)cout<<x[i][j];}cout<<"nMATRIX [Y]";for(i=0;i<3;i++){cout<<"nn";for(j=0;j<3;j++)cout<<y[i][j];}for(i=0;i<3;i++){for(j=0;j<3;j++)z[i][j]=x[i][j]+y[i][j];}cout<<"nMATRIX [Z]";for(i=0;i<3;i++){cout<<"nn";for(j=0;j<3;j++)cout<<z[i][j];}getch();}

Output

cpp matrix output - C++ Basic Example for student and  Programmer


6. Swap two numbers utilizing third variables.

Instance

#embrace#embracevoid foremost(){int a,b,c;clrscr();cout<<"Enter worth of a: ";cin>>a;cout<<"Enter worth of b: ";cin>>b;c=a;a=b;b=c;cout<<"After swap a: "<<a<<"b: "<<b;getch();}

Output

Enter worth of a: 10Enter worth of b: 20After swap a: 20 b: 10

7. Swap two numbers with out utilizing third variable.

Instance

#embrace#embracevoid foremost(){int a,b;clrscr();cout<<"Enter worth of a: ";cin>>a;cout<<"Enter worth of b: ";cin>>b;a=a+b;b=a-b;a=a-b;cout<<"After swap a: "<<a<<"b: "<<b;getch();}

Output

Enter worth of a: 10Enter worth of b: 20After swap a: 20 b: 10

8. Evaluate two strings in C++.

Instance

#embrace#embrace#embracevoid foremost() { char str1[20],str2[20],i,j,flag=0; clrscr(); cout<<"Enter first string: "; will get(str1); cout<<"Enter Second string: "; will get(str2); i=0; j=0;  whereas(str1[i]!='