Prime Number,Palindrome Program in C++


       Prime Number Program in C++

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n, i, m=0, flag=0;
6. cout << "Enter the Number to check Prime: ";
7. cin >> n;
8. m=n/2;
9. for(i = 2; i <= m; i++)
10. {
11. if(n % i == 0)
12. {
13. cout<<"Number is not Prime."<<endl;
14. flag=1;
15. break;
16. }
17. }
18. if (flag==0)
19. cout << "Number is Prime."<<endl;
20. return 0;
21. }

OutPut
1. Enter the Number to check Prime: 17
Number is Prime.
2. Enter the Number to check Prime: 57
Number is not Prime.

Palindrome program in C++

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n,r,sum=0,temp;
6. cout<<"Enter the Number=";
7. cin>>n;
8. temp=n;
9. while(n>0)
10. {
11. r=n%10;
12. sum=(sum*10)+r;
13. n=n/10;
14. }
15. if(temp==sum)
16. cout<<"Number is Palindrome.";
17. else
18. cout<<"Number is not Palindrome.";
19. return 0;
20. }

OutPut
1. Enter the Number=121
Number is Palindrome.
2.Enter the number=113
Number is not Palindrome.

Post a comment

0 Comments