Created
September 19, 2013 13:34
-
-
Save Zulqurnain/4d0cc69ea5a0dac633d6 to your computer and use it in GitHub Desktop.
Write A Program which Inputs a Number and Tell Us The Following Details :
*Positive or Negative
*Even or Odd
*Perfect or Prime or Not both
*Number Or a Character Note : Input Can be a Character so you should perform all check on the ASCII of that character .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream.h> | |
| #include <stdlib.h> | |
| int main(){ // By Zulqurnain jutt | |
| int n; | |
| char c[100]; | |
| cout<<"Enter Your Number :=:"; cin>>c; | |
| for(int it=0,Fail=0,Pass=0;c[it]!='\0';it++){ | |
| if(c[it]>=48 && c[it]<=57) | |
| Pass++; | |
| else{ | |
| if(c[0]!='-') | |
| Fail++; | |
| } | |
| } | |
| if( Pass == it || (Pass>=1 && !Fail) || (Fail==1 && !Pass) ){ | |
| if(Pass == it) | |
| n=atoi(c); // atoi() function to convert Character to integer | |
| else | |
| n=c[0]; | |
| if(n>0){ | |
| cout<<"\nPostive\n"; | |
| } | |
| else{ | |
| cout<<"Negative\n"; | |
| } | |
| /*************************/ | |
| if(n%2==0){ | |
| cout<<"Even\n"; | |
| } | |
| else{ | |
| cout<<"Odd\n"; | |
| } | |
| /*************************/ | |
| int h=1,cou=0,p=0; | |
| while(h<n){ | |
| if(!(n%h)){ // *As perfect number is the sum of its perfect divisors ! | |
| cou+=h; | |
| p++; // Only divisible by 1 so if prime p == 1 | |
| } | |
| h++; | |
| } | |
| if(n==cou && p == 1){ | |
| cout<<"Perfect & Prime\n"; | |
| } | |
| else if(n==cou){ | |
| cout<<"Perfect\n"; | |
| } | |
| else if(p == 1){ | |
| cout<<"Prime\n"; | |
| } | |
| else{ | |
| cout<<"Not Both\n"; | |
| } | |
| /************************/ | |
| if(Pass == it){ | |
| cout<<"Number\n"; | |
| } | |
| else | |
| cout<<"Character\n"; | |
| } | |
| else{ | |
| cout<<"Wrong Entry\n"; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment