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

CPP

The document contains C++ code snippets for common programming problems including: 1. Code to find perfect and prime numbers, convert time formats, find the greatest of three numbers, and calculate factorials. 2. Code to find binary representations, calculate area and perimeter of shapes, swap variables, check for palindromes and Armstrong numbers. 3. Code using loops to print patterns, calculate GCD, and Fibonacci series.

Uploaded by

UltimateDBZ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

CPP

The document contains C++ code snippets for common programming problems including: 1. Code to find perfect and prime numbers, convert time formats, find the greatest of three numbers, and calculate factorials. 2. Code to find binary representations, calculate area and perimeter of shapes, swap variables, check for palindromes and Armstrong numbers. 3. Code using loops to print patterns, calculate GCD, and Fibonacci series.

Uploaded by

UltimateDBZ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

CODE FOR PERFECT NUMBER IN C++ #include<iostream.h> #include<conio.

h> void main() { clrscr(); int i=1, u=1, sum=0; while(i<=500) { while(u<=500) { if(u<i) { if(i%u==0 ) sum=sum+u; } u++; } //End of second loop if(sum==i) { cout<<i<<" is a perfect number."<<"\n"; } i++; u=1; sum=0; } getch(); } //End of main //End of First loop

//Start of main

// start of first loop. //start of second loop.

//End of if statement

FIND PRIME NUMBER IN C++ #include<iostream.h> #include<conio.h> void main() { //clrscr(); int number,count=0; cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT "; cin>>number; for(int a=1;a<=number;a++) { if(number%a==0) { count++; } } if(count==2) { cout<<" PRIME NUMBER \n"; } else { cout<<" NOT A PRIME NUMBER \n"; }

//getch(); } Program take hours, minutes, seconds and print it in 24 hours & standard format #include<iostream.h> #include<conio.h> void main() { clrscr(); int hours,mins,seconds,x; cout<<"Enter hours="; cin>>hours; cout<<"\nEnter minutes="; cin>>mins; cout<<"\nEnter seconds="; cin>>seconds; if(hours > 24) { cout<<"Invalid Entery"; } else { cout<<"\n24 Hours Format\n"; cout<<"Hours : Mins : Seconds\n"<<" "<<hours<<" : "<<seconds<<"\n"; if(hours > 12) { hours=hours-12; cout<<"12 Hours Format\n"; cout<<"Hours : Mins : Seconds\n"<<" "<<hours<<" : "<<seconds; } else "<<mins<<" : "<<mins<<" :

{ cout<<"12 Hours Format\n"; cout<<"Hours : Mins : Seconds\n"<<" "<<hours<<": "<<mins<<" "<<seconds; } } } // end of main :

C++ program to find greatest number between three numbers #include<iostream> using namespace std; int main() { int num1,num2,num3; cout<<" Enter value for first number"; cin>>num1; cout<<" Enter value for second number"; cin>>num2; cout<<" Enter value for third number"; cin>>num3; if(num1>num2&&num1>num3) { cout<<" First number is greatest:"<<endl<<"whick is= "<<num1; } else if(num2>num1&&num2>num3) { cout<<" Second number is greatest"<<endl<<"whick is= "<<num2; } else { cout<<" Third number is greatest"<<endl<<"whick is= "<<num3; } return 0; } Program to find factorial of Number in C++ C++ code to find prime number using for loop #include<iostream> using namespace std; int main() { int num,factorial=1;

cout<<" Enter Number To Find Its Factorial: "; cin>>num; for(int a=1;a<=num;a++) { factorial=factorial*a; } cout<<"Factorial of Given Number is ="<<factorial<<endl; return 0; } Find the binary value of decimal number in C++ #include<iostream> #include <stdio.h> using namespace std; int main() { int n,x,a, c, k; cout<<"Enter an integer in decimal number system"; cin>>x; n=x; cout<<"Binary Value OF Given Number Is: "; for( a=1;n!=0;a++) { n=n/2; } a=a-2; for (c = a; c >= 0; c--) { k = x >> c; if (k & 1) cout<<"1"; else cout<<"0"; }

return 0; } Program to find the Area and Perimeter of a Rectangle

#include<iostream> using namespace std; int main() { int width,height,area,perimeter; cout<<"Enter Width of Rectangle = "; cin>>width; cout<<"Enter Height of Rectangle = "; cin>>height; area=height*width; cout<<"Area of Rectangle ="<<area<<endl; perimeter=2*(height+width); cout<<" Perimeter of rectangle are = "<<perimeter<<endl; return 0; } Program to find the Area of any Triangle having values of three sides #include<iostream> #include<math.h> using namespace std; int main() { float first,second,third; float s,area; cout<<"Enter size of each sides of triangle"<<endl; cout<<"Enter size for First Side ="; cin>>first; cout<<"Enter size for Second Side ="; cin>>second; cout<<"Enter size for Third Side ="; cin>>third;

s = (first+second+third)/2; area = sqrt(s*(s-first)*(s-second)*(s-third)); cout<<"Area of Triangle= "<<area<<endl; return 0; } C++ program to swap the values of two integers #include<iostream> using namespace std; int main() { int var1, var2, swap; cout<<"Enter value for first integer: "; cin>>var1; cout<<"Enter value for second integer: "; cin>>var2; cout<<" Values Before swapping: "<<endl; cout<<"First Integer ="<<var1<<endl; cout<<"Second Interger ="<<var2<<endl; swap=var1; var1=var2; var2=swap; cout<<" Values After swapping: "<<endl; cout<<"First Integer ="<<var1<<endl; cout<<"Second Interger ="<<var2<<endl; return 0; } Program to swap two variables without using third or temp variable #include<iostream> using namespace std; int main()

{ int var1, var2; cout<<"Enter value for first integer: "; cin>>var1; cout<<"Enter value for second integer: "; cin>>var2; cout<<" Values Before swapping: "<<endl; cout<<"First Integer ="<<var1<<endl; cout<<"Second Interger ="<<var2<<endl; var1=var1+var2; var2=var1-var2; var1=var1-var2; cout<<" Values After swapping: "<<endl; cout<<"First Integer ="<<var1<<endl; cout<<"Second Interger ="<<var2<<endl; return 0; } Find last prime number occur before enter number #include<iostream> using namespace std; int main() { int num,count=0; cout<<"Enter number to find last prime number occurs before it: "; cin>>num; for( int a=num-1;a>=1;a--) { for(int b=2;b<a;b++) { if(a%b==0) count++; } if(count==0) { if(a==1) { cout<<"no prime number less than 2"; break; } cout<<a<<" is the last prime number before entered number"; break;

} count=0; } return 0; } C++ program to find Fibonacci Series with understanding logic #include<iostream> using namespace std; int main() { int range, first = 0, second = 1, fibonicci=0; cout << "Enter Range for Terms of Fibonacci Sequence: "; cin >> range; cout << "Fibonicci Series upto " << range << " Terms "<< endl; for ( int c = 0 ; c < range ; c++ ) { if ( c <= 1 ) fibonicci = c; else { fibonicci = first + second; first = second; second = fibonicci; } cout << fibonicci <<" "; } return 0; } C++ program to swap two numbers using built in swap function from c++ standard library #include <iostream> using namespace std; void swap() { cout<<"this is my swap"; } int main() { int firstNum , secondNum; cout<<"Enter value for First Number: "; cin>>firstNum; cout<<"Enter value for Second Number: "; cin>>secondNum; cout<<"\n\n"; cout<<"Values BEFORE Calling Built in Swap Function"<<endl<<endl; cout<<"\tFirst Number = "<<firstNum<<endl; cout<<"\tSecond Number = "<<secondNum<<endl; cout<<"\n\n"; std::swap( firstNum , secondNum ); cout<<"Values AFTER Calling Bulit in Swap Function"<<endl<<endl; cout<<"\tFirst Number = "<<firstNum<<endl; cout<<"\tSecond Number = "<<secondNum<<endl; return 0; }

Find Palindrome number in c++ #include<iostream> using namespace std; int main() { int palindrome, reverse=0; cout<<"Enter number: "; cin>>palindrome; int num=0,key=palindrome; for(int i=1;palindrome!=0;i++){ num=palindrome%10; palindrome=palindrome/10; reverse=num+(reverse*10); } if(reverse==key){ cout<<key<<" is a Palindrome Number"; } else{ cout<<key<<"is NOT a Palindrome Number"; } return 0; } Find Armstrong number in C++ with logic explanation and code dry run #include<iostream> using namespace std; int main() { int armstrong=0,num=0,result=0,check; cout<<"Enter a Number to find it is an Armstrong number or not"; cin>>num; check=num; for(int i=1;num!=0;i++){ armstrong=num%10; num=num/10; armstrong=armstrong*armstrong*armstrong; result=result+armstrong; } if(result==check){ cout<<check<<" is an Armstrong Number"; } else{ cout<<check<<" is NOT an Armstrong Number"; } return 0; }

C++ code to print right triangle shape using nested for loops #include<iostream> using namespace std; int main() { for(int i=0;i<=5;i++){ for(int j=0;j<=i;j++) { cout<<j; } cout<<endl; } return 0; } Find Greatest Common Divisor (GCD) of two numbers c++ program #include<iostream> using namespace std; int main() { int first_number , second_number, gcd; cout<<"Enter First Number : ";cin>>first_number; cout<<"Enter Second Number: ";cin>>second_number; for(int i=1;i<=first_number&&i<=second_number;i++){ if(first_number%i==0 && second_number%i == 0 ){ gcd=i; } } cout<<"Greatest Common Divison (GCD):"<<gcd<<endl; return 0; }

You might also like