Showing posts with label CP-I Programs. Show all posts
Showing posts with label CP-I Programs. Show all posts

Palindrome


#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{clrscr();
     char str[25],str1[25];	
     cout<<"Enter a string: ";
	
     gets(str);

     strcpy(str1,str);
     strrev(str);
	if(strcmp(str,str1)!=0)
	{
	     cout<<"string is not palindrome";
	}
	else
	{
	      cout<<"string is a palindrome";
	}

	  cout<<endl;
getch();
}

Print all odd nos till 50


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=1;
do
{
cout<<n<<endl;
n=n+2;
}
while(n<=50);
getch();
}
**********output***********
1
3                                                                            
5                                                                            
7                                                                            
9                                                                            
11                                                                            
13                                                                            
15                                                                            
17                                                                            
19                                                                            
21                                                                            
23                                                                            
25                                                                            
27                                                                            
29                                                                            
31
33                                                                            
35                                                                            
37                                                                            
39                                                                            
41                                                                            
43                                                                            
45                                                                            
47  

Algorithm:
1. Start
2. Set n=1
3. Write n
4. Increment n
5. Check if no<50
     if true then:
     a)Go to step 2
    if false then:
     b)go to step 5
6. Stop

                                                                        
49

GCD & LCM


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,no1,no2,i,temp;
cout<<"enter 2 nos";
cin>>a>>b;
no1=a;
no2=b;
while(no2)
{
int temp=no2;
no2=no1%no2;
no1=temp;
}
cout<<"\n GCD="<<no1;
for(i=a; i<=a*b; i++)
{
if(i%a==0 && i%b==0)
{
cout<<"\n LCM="<<i;
break;
}
}

getch();
}

******output******
enter 2 nos 4
6                                                                            
                                                                             
 GCD=2                                                                        
 LCM=12                                                                      
                                                                             
 Algorithm:
1.Read a,b,no1,no2,temp & i.
2.While no2 not equal to zero
         a)  Set no2=no1/no2 & no1=temp                                                                          
         b)  Write G.C.D = no2
3.for initialize integer i=a;i<=a*b;i++
       {
           if   i%a=0 & i%b=0
          Write: L.C.M
        [End of If structure]
        [End of For structure]
4.Exit                                                            
             




                                                              
                                                           
                                           

                                                   

Armstrong number


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,temp,sum=0,d=0;
cout<<"enter the number";
cin>>a;
temp=a;
while(a)
{
d=a%10;
sum=sum+(d*d*d);
a=a/10;
}
if(sum==temp)
{
cout<<"The no.is armstrong";
}
else
{
cout<<"The no.is not armstrong";
}
getch();
}
******output*******
enter the number153
The no.is armstrong    

Algorithm:
1. Start
2. Read A
3. Set temp=A
4. Check while (A)
    if true:
    a)Set D=A%10
    b)Set sum=sum + (D*D*D)
    c)Set A=A/10
    [end of while structure]
5. Check if sum = temp
    a) Write: The no. is amstrong
6. else
    a) Write: The no. not is amstrong 
    [end of if else structure]  
7. Stop                                                        
                                                                             

Roots of a quadratic equation


#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); float a,b,c,d,e,f; cout<<"Enter the coefficients a,b,c of X in a quadratic equation:"; cin>>a>>b>>c; d=sqrt((b*b)-(4*a*c)); if(d>0) { e=((-b+d)/2*a); f=((-b-d)/2*a); cout<<"First square root is:"<<e<<endl; cout<<"Second square root is:"<<f<<endl; } else { cout<<"The two roots are not real"<<endl; } getch(); }
*********output Section***********
Enter the coefficients a,b,c of X in a quadratic equation
:1
    
-1                                                         
-2                                        
                                     
First root is:2                                                                
Second root is:-1                                                           
Algorithm:
  1. Read A,B,C
  2. Set D = B2- 4*A*C                 
  3. If D > 0, then :
a)      Set E = - B + sqrt (D)/2*A
And F= - B - sqrt (D)/2*A
b)     Write: E, F
       Else
               Write: ‘NO REAL SOLUTION ‘
       [End of If structure]
       4. Exit

Factorial

#include<iostream.h>
#include<conio.h>
void main();
{
clrscr();
int fact=1,no;
cout<<"Enter the number";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"the factorial of the inputed number is:"<<fact;
getch();
}


Armstrong Number

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int no,temp;
cout<<"enter the no. you want to check";
cin>>no;
temp=no;
while(no)
{
d=no%10;
sum=sum+(d*d*d);
no=no/10;
}
if(sum==temp)
cout<<"Armstrong";
else
cout<<"Not Armstrong";
//coding by Jerry Johns.
//http://jerryjo94.blogspot.com
getch();
}

Fibonacci Series


  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a=0,b=1,c=0,n;
  7. cout<<"Enter the number of terms you wanna see: ";
  8. cin>>n;
  9. cout<<a<<" "<<b<<" ";
  10. for(int i=1;i<=n-2;i++)
  11. {
  12. c=a+b;
  13. a=b;
  14. b=c;
  15. cout<<c<<" ";
  16. }//Coding by: Jerry Johns
  17. //http://jerryjo94.blogspot.com
  18. getch();
  19. }