0% found this document useful (0 votes)
36 views

C++ Notes For Website

The algorithm uses the concept of binary search to find the square root of a number. It works by iteratively minimizing the possible range between the lower and upper bound and calculating the average at each step. If the average squared equals the number, it returns the average as the square root. Otherwise, it updates the lower or upper bound for the next iteration. This process doubles the precision each time until the closest value to the actual square root is found within the set number of iterations.

Uploaded by

zameerhasan12
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

C++ Notes For Website

The algorithm uses the concept of binary search to find the square root of a number. It works by iteratively minimizing the possible range between the lower and upper bound and calculating the average at each step. If the average squared equals the number, it returns the average as the square root. Otherwise, it updates the lower or upper bound for the next iteration. This process doubles the precision each time until the closest value to the actual square root is found within the set number of iterations.

Uploaded by

zameerhasan12
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Square root of the function without sqrt function //DESCRIPTION //The algorithm is very simple and uses the

same concept of binary search; it works by minimizing the possible range of the square root. //Suppose NUM = 25, //We know that NUM is between 0 and 25, therefore its square roots lower bound is LB = 0 and upper bound is UB = 25. //The next step is calculating the average of the bounds t = (LB + UB)/2. //If t2 = NUM we return t, if t2 < NUM all of the numbers between LB and t are not the square root, hence LB = t. Similarly, if t2 > NUM then our upper bound, UB = t. //We can repeat this step as many times as we want. Each iteration doubles the precision. //If we didnt find the specific square root when we finish, we should return (LB + UB) / 2, as it is the closest we can get to the actual square root. //**************************************************************************************/ #include <iostream.h> #include <conio.h> double sqrrt(double num) { double lb,ub; int iteration = 35; //The greater the number of iteration, The accurate is the result lb = 0; //Set Lower Bound to zero ub = num; //Set Upper Bound to num while(iteration > 0) { double t = (lb + ub) / 2; if(t * t == num) //If square of a t is equal to the num itself then return t; //Return Root else if(t * t > num) //If square of t is greater than the num ub = t; //Update Upper Bound else lb = t; //Update Lower Bound iteration--; //Decrease iteration } return (lb + ub) / 2; //Return closest value if root is not found in 35 iterations } int main() { double num; int result; cout << "Square Root of : "; cin >> num; if (num < 0) { cout << "Cannot Square Root a Negative Number"; getch(); return 0; //Halt the program }

result = sqrrt(num); cout << "is " << result; //Print Result getch(); return 0; } Output :

LCM of two Numbers using LCM function

Q.3. of Question bank #include<iostream.h> #include<conio.h> int LCM(int,int); int main() { int n1,n2,res; cout<<"Enter value of n1 and n2:\n"; cin>>n1>>n2; res = LCM(n1,n2); cout<<"\nThe LCM of the numbers is "<<res; getch(); return 0; } int LCM(int a, int b) { int x,y,z; x=a; y=b; while(a!=b) { if(a>b) a = a-b; if(b>a) b = b-a; } z= (x*y)/a; return(z); }

Output :

GCD and LCM of three numbers entered by the user

Q.5. and Q.10 of Question bank //WAP to calculate the H.C.F and L.C.M of three numbers. //eg. H.C.F of 11,22,33 is 11 // L.C.M of 11,22,33 is 66 #include<iostream.h> #include<conio.h> int main() { int a, b, c; // three numbers int hcf,lcm; cout<<"Enter three numbers :\n"; cin>>a>>b>>c; //calculate the minimum of three numbers int min = (a < b ? a : b); min = (min < c ? min : c); for( int i = min; i>0; i--) { if(a%i == 0 && b%i == 0 && c%i == 0) { hcf = i; break; } } lcm = (a*b*c)/hcf; cout<<" \n The H.C.F of " << a <<" , " << b << " , " << c << "is " << hcf; cout<<" \n The L.C.M of " << a <<" , " << b << " , " << c << "is " << lcm; getch();

} Output :

To Check Prime Number Q.6. Write a Program in C++ to check whether the number entered by the user is Prime number or not. #include<iostream.h> #include<conio.h> char isprime(int); int main() { int i,n; char ch; cout<<"Enter the number : \n"; cin>>n; ch = isprime(n); if(ch=='Y') cout<<"The given number "<<n<<" is a Prime Number "; else cout<<"The given number "<<n<<" is not a Prime Number "; getch(); return 0; } char isprime(int a) { char flag ='Y'; int i= 2; while(i<=a/2) { if(a%i==0) { flag = 'N'; } i++; } return (flag); } Output :

Q.11. Write a Program to check whether the number entered by the user is a Palindrome number or not. #include<iostream.h> #include<conio.h> int palin(int); int main() { int n,res; cout<<"Enter the number: \n"; cin>>n; cout<<"The number you entered is : "<<n; res = palin(n); if(res==n) cout<<"\nThe number "<<n<<" is a Palindrome number "; else cout<<"\nThe number "<<n<<" is a not a Palindrome number "; getch(); return 0; } int palin(int z) { int digit,rev=0; while(z>0) { digit= z%10; rev= rev*10 +digit; z =z/10; } return (rev); } Output :

To check Character using chr function Q.4. Write a Program in C++ that acceppt a character from the user in main function and make use of chr function to print the result.

#include<iostream.h> #include<conio.h> void chr(char); int main() { char ch; cout<<"Enter the character : \n"; cin>>ch; chr(ch); getch(); return 0; } void chr(char alpha) { switch(alpha) { case 'A' : case 'a' : cout<<"Alpha"; break; case 'B' : case 'b' : cout<<"Beta"; break; case 'D' : case 'd' : cout<<"Delta"; break; case 'G' : case 'g' : cout<<"Gamma"; break; default : cout<<"Invalid Character "; } } Output :

You might also like