Welcome..!!

Please visit http://csprograms.in/ for more Computer Science programs.

Wednesday 14 March 2012

19. C++ program to find the length of a string.


# include <iostream>
# include <string>

int main()

{
char s1[25];
cout << “Enter the string:”;

cin > > str1;
cout < < “Strlen(str1): “< < strlen(str1) < < end1;
return 0;
}

18. C++ program to convert a string into upper-case or lower-case .


# include < iostream>
# include < string>
int main()
{
char str1 [30], temp[30];
cout < < “Enter the string:”;
cin > > str1;
strcpy ( temp, str1);
cout < < “strupr ( temp ) : “ < < strupr (temp) < < end1;
cout < < “strlwr (temp) : “ < < strlwr ( temp) < < end1;
return 0;
}

17. C++ program to compare strings.


# include < iostream>
# include < string>
int main()
{
char str1[30], str2 [30];
cout << “Enter string str1:”;
cin >> str1;
cout << “Enter string str2:”;
cin >> str2;
int status = strcmp ( str1 ,str2 );
cout < < “strcmp (str1, str2 ):”;
if (status = = 0)
cout < < “str1 < <” is greater than” << str2;
else
cout < < str1 < < “is less than” << str2;

return 0;

}

16. C++ program to concatenate strings.


# include < iostream>
# include < string >
int main()

{
char str1[50], str2 [35];
cout < < “Enter string str1;”;
cin >> str1;
cout < < “Enter string str2:”;
cin >> str2;
strcat(str1,str2);
cout < < “strcat (str1, str2 ) : “< < str1;
return 0;
}

15. C++ program to copy string.


# include < iostream>
# include < string>
int main()
{
char str1[30], str [30];
cout << “Enter the string:” ;
cin >> str1;
strcpy( str2, str1);
cout << “strcpy (str2, str1) : “< < str2;
return 0;
}

14. C++ program to reverse the string .


# include < iostream>
# include < string>
int main()
{
char str1[50], str2 [50];
cout << “Enter the string:” ;
cin >> str1;
str2 = strrev ( str1);
cout << “strrev (str1) : “< < str2;
return 0;
}

13. C++ program to find smallest among 'n' numbers.


#include <iostream>
int main()
{
int i,n,m;
cout <<"Enter the total number\n";
cin >>n;
cout <<"The total number of array is \n"<<n;
for(i=1;i<=n;i++)
cin >>a[i];
cout <<"the array is \n";
for(i=1;i<=n;i++)
cout <<"\n"<<a[i];

m = a[1];
for(i=2;i<=n;i++)
{
if(m>a[i])
s = a[i];
}
cout <<"smallest among 'n' number is \n"<< m;
return 0;
}

12. C++ program to find largest among 'n' numbers.



#include <iostream>
int main()
{
int i,n,m; 
cout <<"Enter the total number \n";
cin >>n;
cout <<"The total number of array is \n"<<n;
for(i=1;i<=n;i++)
cin >>a[i];
cout <<"The entered array is \n";
for(i=1;i<=n;i++)
cout <<"\n"<<a[i];
m = a[1];
for(i=2;i<=n;i++)
{
if(m<a[i])
m = a[i];
}
cout <<"The largest number is \n"<< m;
return 0;
}

11. C++ program to find sum of 'n' numbers.


#include <iostream>
int main()
{
int i,n,sum;
cout <<"Enter the total number \n";
cin >>n;
cout <<"The total number of array is \n"<<n;
for(i=1;i<=n;i++)
cin >>a[i];
sum=0;
cout <<"the array is \n";
for(i=1;i<=n;i++)
cout <<"\n"<<a[i];
for(i=1;i<=n;i++)
sum=sum+a[i];
cout <<"The sum of 'n' number is \n"<< sum;
return 0;
}

10. C++ program to find average of "n" numbers.


#include <iostream>
int main()
{
int i,n,m;
cout <<"Enter the total numbers \n";
cin >>n;
cout <<"The total number of array is \n"<<n;
for(i=1;i<=n;i++)
cin >>a[i];
m=0;
cout <<"The entered array is \n";
for(i=1;i<=n;i++)
cout <<"\n"<<a[i];
for(i=1;i<=n;i++)
m = m + a[i];
m = m / n;
cout <<" The average of 'n' numbers is \n"<< m;
return 0;
}

9. C++ program to find if the given number is even or odd.


#include <iostream>
int main()
{
int p,q;
cout <<"enter the number \n";
cin >>p;
cout <<"the given number is \n"<<n;
q=p%2;
if(q==0)
cout << p<<" is even number \n";
else
cout <<p<<" is odd number \n";
return 0;
}

Tuesday 13 March 2012

8. C++ program to find the given number is palindrome or not.

#include <iostream>
int main()
{
int n,m,p,q;
cout <<"Enter the number\n";
cin >>n;
cout << "Entered number is \n"<<n;
m=n;
p=0;
do
{
q=n%10;
n=n/10;
p=p*10+q;
} while(n!=0);
if(p==m)
cout <<"The given number is palindrome \n"<<m;
else
cout <<"The given number is not palindrome \n"<<m;
return 0;
}

7. C++ program to find prime number.


#include <iostream>
int main()
{
int l, i, j, k;
clrscr();
l=0;
cin >>n;
for(i=1;i<=n;i++)
{
for(j=2;j<=(i-1);j++)
{
k=i%j;
if (k==0)
l=1;
else
l=l;
}
if (l==0)
cout <<"The number is prime \n"<<i;
else
cout <<"This is not a prime number \n"<<i;
l=0;
}
return 0;
}

6. C++ program to find Armstrong number


#include <iostream>
int main()
{
int s, n, m, r;
cout <<"Enter the number \n";
cin >>n;
cout <<" Entered number is \n"<< n;
s=0;
m=n;
do
{
r=n%10;
n=n/10;
s=s+r*r*r;
}while (n!=0);
if(s==m)
cout <<"This is Armstrong number\n";
else
cout <<"This is not Armstrong number\n";
return 0;
}

5. C++ program to find the Fibonacci series.


#include <iostream>
int main()
{
int x, i, j, n, m;
cout <<"Enter the total number \n";
cin >>n;
cout <<"Total number of the series is \n"<< n;
i=0,j=1;
cout <<i<<j;
for(x=3;x<=n;x++)
{m=i+j;
cout <<"\n"<<m;
i=j;j=m;
}
return 0;
}

4. C++ program to find factorial of a given number.



#include <iostream>
int main()
{
int i,f,n;
f=1;
cout <<"Enter the number \n";
cin >>n;
for(i=1;i<=n;i++)
f=f*i;
cout <<"Factorial of  "<<n <<" is  "<<f;
return 0;
}

3. C++ program to find reverse a given number.


#include <iostream>
int main()
{
int r, n;
cout <<"enter the number \n";
cin >>n;
cout <<"reverse of the number is ";
do
{
r=n%10;
n=n/10;
cout <<r;
}while(n!=0);
cout << "\n";
return 0;
}

2. C++ program to find the sum of digits of a given number.

#include <iostream>
int main()
{
int s,n,n1;
cout <<"enter the number \n";
s=0;
cin >>n;
cout <<"the given number is \n"<<n;
a: do
{
n1=n%10;
s=s+n1;
n=n/10;
}
while (n!=0);
if(s>9)
{
n=s;
s=0;
goto a;
}
else
cout <<"sum of digit is \n"<<s;

return 0; 
}

1. C++ program to display a message.

The very first program is the simplest one in any programming language.
It goes..


#include <iostream>
int main()
{
cout << “ Welcome to Shad’s blog “ << endl;

return 0;
}

Welcome

                          

Please visit http://csprograms.in/ for more Computer Science programs.