C++ Program to print your name 10 times
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=10;++i)
cout<<"Your Name"<<"\n";
getch();
}
C++ Program to print fibonacci series
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long n,first,second,third;
first=0;
second=1;
cout<<"How many numbers?";
cin>>n;
cout<<"Fabonacci series\n"<<first<<" "<<second;
for(int i=2;i<n;++i)
{
third=first+second;
cout<<" "<<third;
first=second;
second=third;
}
getch();
C++ Program to Print following series: 1 -4 7 -10..........-40
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a=-1,b;
for(i=1;i<=40;i+=3)
{
a*=-1;
b=i;
b*=a;
cout<<b<<" ";
}
getch();
}
c++ program to find sum of series 1^2+3^2+5^2+......+n^2
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
long sum=0;
cout<<"1^2+3^2+5^2+......+n^2\n\n Enter Value of n:";
cin>>n;
for(i=1;i<=n;i+=2)
sum+=(i*i);
cout<<"\n Sum of given series is "<<sum;
getch();
}
C++ Program to find sum of square of n natural numbers
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
unsigned long n,i,sum=0,d;
cout<<"Enter any number";
cin>>n;
for(i=1;i<=n;++i)
{
d=pow(i,i);
sum+=d;
}
++sum;
cout<<endl<<"Sum= "<<sum;
getch();
}
C++ Program to calculate roots of quadratic equation ax^2+bx+c=0
#include<iostream.h>
#include<conio.h>
#include<math.h> //to claculate square root
void main()
{
clrscr(); //to clear the screen
float root1,root2,a,b,c,d;
cout<<"Quadratic Equation is ax^2=bx+c=0";
cout<<" Enter values of a,b and c:";
cin>>a>>b>>c;
d=(b*b)-(4*a*c);
if(d>0)
{
cout<<"\nTwo real and distinct roots";
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout<<"\nRoots are "<<root1<<" and "<<root2;
}
else
if(d==0)
{
cout<<"\nTwo real and equal roots";
root1=root2=-b/(2*a);
cout<<"\nRoots are "<<root1<<" and "<<root2;
}
else
cout<<"\nRoots are COMPLEX and IMAGINARY....!!!";
getch(); //to stop the screen
}
C++ Program to print three numbers in descending order
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,big1,big2,big3;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
big1=a;
if(b>big1)
big1=b;
else
if(c>big1)
big1=c;
if(big1==a)
{
if(b>c)
{
big2=b;
big3=c;
}
else
{
big2=c;
big3=b;
}
}
else
{
if(big1==b)
if(a>c)
{
big2=a;
big3=c;
}
else
{
big2=c;
big3=a;
}
else
{
if(a>b)
{
big2=a;
big3=b;
}
else
{
big2=b;
big3=a;
}
}
}
cout<<"\n\n\tNumbers in descending order......\n\t\t";
cout<<big1<<" "<<big2<<" "<<big3;
getch();
}
#include<iostream.h>
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year;
cout<<"Enter Year(ex:1900):";
cin>>year;
if(year%100==0)
{
if(year%400==0)
cout<<"\nLeap Year";
}
else
if(year%4==0)
cout<<"\nLeap Year";
else
cout<<"\nNot a Leap Year";
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a,b,c,big1,big2,big3;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
big1=a;
if(b>big1)
big1=b;
else
if(c>big1)
big1=c;
if(big1==a)
{
if(b>c)
{
big2=b;
big3=c;
}
else
{
big2=c;
big3=b;
}
}
else
{
if(big1==b)
if(a>c)
{
big2=a;
big3=c;
}
else
{
big2=c;
big3=a;
}
else
{
if(a>b)
{
big2=a;
big3=b;
}
else
{
big2=b;
big3=a;
}
}
}
cout<<"\n\n\tNumbers in descending order......\n\t\t";
cout<<big1<<" "<<big2<<" "<<big3;
getch();
}
C++ program to swap to numbers without using temp variable
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter value of a:";
cin>>a;
cout<<"Enter value of b:";
cin>>b;
if(a>b)
{
b=a-b;
a=a-b;
b=a+b;
}
else
if(b>a)
{
a=b-a;
b=b-a;
a=b+a;
}
cout<<"\na="<<a;
cout<<"\nb="<<b;
getch();
}
C++ Program to do Addition,subtraction and multiplication of two numbers using function.
#include<iostream.h>
#include<conio.h>
int res;
void main()
{
clrscr();
int sum(int,int);
int sub(int,int);
int mul(int,int);
int a,b,m,su,s;
cout<<"Enter two numbers:";
cin>>a>>b;
s=sum(a,b);
su=sub(a,b);
m=mul(a,b);
cout<<"Sum:"<<s<<"\nSubtraction:"<<su<<"\nMultiplication:"<<m;
getch();
}
sum(int a,int b)
{
res=a+b;
return(res);
}
sub(int a,int b)
{
res=a-b;
return(res);
}
mul(int a,int b)
{
res=a*b;
return(res);
}
C++ program to do linear search in Array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],n,x,i,flag=0;
cout<<"How many Elements?";
cin>>n;
cout<<"\nEnter Elements of the Array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter Element to search:";
cin>>x;
for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag)
cout<<"\nElement is Found at position "<<i+1;
else
cout<<"\nElement not found";
getch();
}
C++ Program to explain Binary search in Array
#include<conio.h>
#include<iostream.h>
void main()
{
int search(int [],int,int);
clrscr();
int n,i,a[100],e=-3,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"\nEnter Elements of Array in Accending order\n";
for(i=0;i<n;++i)
{
cin>>a[i];
}
cout<<"\nEnter element to search:";
cin>>e;
res=search(a,n,e);
if(res!=0)
cout<<"\nElement is Founded at "<<res+1<<"st position";
else
cout<<"\nElement is not found....!!!";
getch();
}
int search(int a[],int n,int e)
{
int f,l,m;
f=0;
l=n-1;
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return 0;
}
C++ Program to Count no. of alphabates,digits and spaces present in a file STORY.TXT
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ifstream fin("STORY.TXT");
char ch;
int i,a=0,s=0,d=0;
while(fin)
{
fin.get(ch);
i=ch;
if(i>63&&i<91||i>96&&i<123)
a++;
else
if(ch==' ')
s++;
else
if(i>47&&i<58)
d++;
}
cout<<"No. OF Alphabates:"<<a;
cout<<"\nNo. Of Digits:"<<d;
cout<<"\nNo. Of Spaces:"<<s;
getch();
}
C++ Program to Count no. of words in a string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[20];
int i,count=0;
cout<<"Enter a string:";
gets(a);
for(i=0;a[i]!='\0';++i)
{
if(a[i]==' ')
count++;
}
count++;
cout<<"\nThere are "<<count<<" words in the given string";
getch();
}
C++ Program to Check whether a year is Leap year or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year;
cout<<"Enter Year(ex:1900):";
cin>>year;
if(year%100==0)
{
if(year%400==0)
cout<<"\nLeap Year";
}
else
if(year%4==0)
cout<<"\nLeap Year";
else
cout<<"\nNot a Leap Year";
getch();
}
C++ Program to check whether a String is Palindrome or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,len,flag=1;
char a[20];
cout<<"Enter a string:";
cin>>a;
for(len=0;a[len]!='\0';++len);
for(i=0,j=len-1;i<len/2;++i,--j)
{
if(a[j]!=a[i])
flag=0;
}
if(flag==1)
cout<<"\nThe string is Palindrome";
else
cout<<"\nThe string is not Palindrome";
getch();
}
C++ Program to reverse a string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[20],a1[20];
int i,j;
cout<<"Enter any String:"<<"\n";
gets(a); cout<<"Reverse of the string is: ";
for(i=0;a[i]!='\0';++i);
for(j=i-1;j>=0;--j)
cout<<a[j];
getch();
}
C++ Program to print given series:1 2 4 8 16 32 64 128
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=128;i*=2)
cout<<i<<" ";
getch();
}
C++ Program to calculate square root of any number
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float sq,n;
cout<<"Enter any number:";
cin>>n;
sq=sqrt(n);
cout<<"Square root of "<<n<<" is "<<sq;
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float sq,n;
cout<<"Enter any number:";
cin>>n;
sq=sqrt(n);
cout<<"Square root of "<<n<<" is "<<sq;
getch();
}
No comments:
Post a Comment