Thursday, 23 August 2012

sorting using array and pointers


#include<iostream>
using std::cin;
using std::cout;
#include<conio.h>
void main()
{
int *p,n,a[100],i,*j,t;
cout<<"enter the no of elements: ";
cin>>n;
cout<<"\nenter the elements-\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(p=&a[0];p<=&a[n-1];p++)
{
for(j=&a[0];j<=&a[n-1];j++)
{
if(*p>*j)
{
t=*p;
*p=*j;
*j=t;
}
}
}
cout<<"\nthe sorted elements are: ";
for(p=&a[0];p<=&a[n-1];p++)
{
cout<<*p<<" ";
}
getch();
}

averaging using pointers


#include<iostream>
using std::cin;
using std::cout;
#include<conio.h>
void main()
{
int *p,n,a[100],i,j;
float sum=0,avg;
cout<<"enter the no of elements: ";
cin>>n;
cout<<"\nenter the elements-\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(p=&a[0];p<=&a[n-1];p++)
{
sum=sum+*p;
}
avg=sum/n;
cout<<"\nthe average= "<<avg;
getch();
}




Sunday, 19 August 2012

C Programming 1 (Hello World) - Visual Studio 2010

calculator


#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
using std::cin;
using std::cout;
double add(double a,double b)
{
double c;
c=a+b;
return(c);
}
double sub(double a,double b)
{
double c;
c=a-b;
return(c);
}
double mult(double a,double b)
{
double c;
c=a*b;
return(c);
}
double div(double a,double b)
{
double c;
c=a/b;
return(c);
}
double power(double a,double b)
{
double c;
c=pow(a,b);
return(c);
}
int main()
{
double y,z,i;
int x;
cout<<"*****************************\n";
cout<<"enter 1 to add 2 nos \n";
cout<<"enter 2 to sub 2 nos \n";
cout<<"enter 3 to multiply 2 nos \n";
cout<<"enter 4 to divide 2 nos \n";
cout<<"enter 5 to find power nos \n";
cout<<"*****************************\n";
cin>>x;
switch (x)
{
case 1:
{
cout<<"*****************************\n";
cout<<"enter the 2 nos to be added ";
cin>>y>>z;
i=add(y,z);
cout<<"\n"<<"the sum is"<<i;
cout<<"*****************************\n";
break;
}
case 2:
{
cout<<"*****************************";
cout<<"enter the 2 nos to be sub ";
cin>>y>>z;
i=sub(y,z);
cout<<"\n"<<"the sub is"<<i;
cout<<"*****************************";
break;
}
case 3:
{
cout<<"*****************************";
cout<<"enter the 2 nos to be multi ";
cin>>y>>z;
i=mult(y,z);
cout<<"\n"<<"mult is "<<i;
cout<<"*****************************";
break;
}
case 4:
{
cout<<"*****************************";
cout<<"enter the 2 nos to be divided ";
cin>>y>>z;
i=div(y,z);
cout<<"\n"<<"div is"<<i;
cout<<"*****************************";
break;
}
case 5:
{
cout<<"*****************************";
cout<<"enter the base and power ";
cin>>y>>z;
i=pow(y,z);
cout<<"\n"<<"the ans is"<<i;
cout<<"*****************************";
break;
}
default :
{
cout<<"*****************************";
cout<<"wrong entry";
cout<<"*****************************";
break;
}
}
getch();
}

factorial of a given number using function



#include<iostream>
using namespace std;
int fact(int );
void main()

{
int n,i,c;
cout<<"Enter the number";
cin>>n;
c=fact(n);
cout<<"\nfactorial="<<c;
system("PAUSE");
}
int fact(int n)
{
int i,fac=1;
for(i=n;i>0;i--)
{
fac=fac*i;
}
return fac;
}

string is a palindrome or not



#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
   char s1[20];
   int i, j, len=0, flag=0;
   cout<<"\nEnter any string: ";
   gets(s1);
   for (i=0; s1[i]!='\0'; i++)
        len++;
   i = 0;
   j = len-1;
   while (i < len)
 {
        if (s1[i] != s1[j])
        {
              flag = 1;
              break;
        }
        i++;
        j--;
   }
   if (flag == 0)
        cout<<"\nString is palindrome";
   else
        cout<<"\nString is not palindrome";
   getch();
}

prime numbers within two given values



#include<iostream>
using namespace std;
void main()
{
int num1,num2,i,x,prime;
cout<<"Enter the two values between which series is to be generated:";
cin>>num1>>num2;
{for(i=num1;i<=num2;i++)
{
prime=0;
for(x=2;x<=i/2;x++)
{
if(i%x==0)
{prime=1;
break;
}
}
if(prime==0)
cout<<" "<<i;
}
      }
system("PAUSE");
}

secant method


#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float a)
{
float b;
b=a*a-4*a-10;    /*just change the equation according to your wish */
return(b);
}
float main()
{
float x1,x2,x3,f1,f2,f3,e;
printf("enter the values of x1 and x2");
scanf("%f",&x1);
scanf("%f",&x2);
printf("x1\t      x2\t         x3\t          f1\t            f2\t");
do
{
f1=fun(x1);
f2=fun(x2);
x3=((x1*f2)-(x2*f1))/(f2-f1);
e=fabs((x3-x2)/(x3));
x1=x2;
x2=x3;

printf("\n%f\t    %f\t     %f\t     %f\t    %f\t",x1,x2,x3,f1,f2);

}
while(e>0.001);

getch();
}

Saturday, 18 August 2012

matrix


#include<iostream>
#include<conio.h>
using namespace std;
void add(int a[10][10], int b[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";
}
}
void sub(int a[10][10], int b[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";;
}
}
void multn(int a[10][10], int b[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[i][j]*b[j][i];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";
}
}
void trace(int a[10][10])
{int s=0,i;
for(i=0;i<3;i++)
s+=a[i][i];
cout<<"The diagonal elements : "<<s;
}
void trnsp(int a[10][10])
{int c[10][10];
     for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a[j][i];
cout<<"The new matrix C is : ";
for(int i=0;i<3;i++)\
{cout<<"\n";
for(int j=0;j<3;j++)
cout<<c[i][j]<<" ";
}
}
void main()
{int a[10][10],b[10][10],i,j,ch;
char m;
cout<<"Enter the matrix A : ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"Enter the matrix B : ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
cout<<"\nMENU:\n1)Addn\n2)Subtn\n3)Multpn\n4)Trace\n5)Transpose\nEnter your choice : ";
cin>>ch;
switch(ch)
{case 1 : add(a,b);
 break;
case 2 : sub(a,b);
 break;
case 3 : multn(a,b);
     break;
case 4 : cout<<"Which matrix to trace (A/B) ?? : ";
 cin>>m;
 if(m=='a' || m=='A')
 trace(a);
 else if(m=='b' || m=='B')
 trace(b);
 else
 cout<<"Invalid!!";
 break;
case 5 : cout<<"Which matrix to transpose (A/B) ?? : ";
 cin>>m;
 if(m=='a' || m=='A')
 trnsp(a);
 else if(m=='b' || m=='B')
 trnsp(b);
 else
 cout<<"Invalid!!";
 break;
    }
getch();
}

swap no. with out using function


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i,j,n,a,b,c,d;
char p,q,e;
cout<<"enter the 2 nos to be swaped";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<a;
cout<<b;
getch();
}

factorial


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i=2,j,n,a=1,b,c,d;
char p,q,e;
cout<<"enter the  no to be calculated for factorial";
cin>>n;
while(i<=n)
{
a=a*i;
i++;


}


cout<<a;
getch();
}

to get ascii of digits value if u have lot of time


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i=1,j,n,a,b,c,d;
char p,q;
cout<<"enter the  ascii value";
cin>>n;
if(n>>65 && n<<90)
{
p=n;
cout<<p;
}
else if(n>>97 && n<<122)
{
q=n;
cout<<q;
}
else if(n=49)
{
cout<<"1";
}
else if(n=50)
{
cout<<"2";
}
else if(n=51)
{
cout<<"3";
}
else if(n=52)
{
cout<<"4";
}
else if(n=53)
{
cout<<"5";
}
else if(n=54)
{
cout<<"6";
}
else if(n=55)
{
cout<<"7";
}
else if(n=56)
{
cout<<"8";
}
else if(n=57)
{
cout<<"9";
}

else if(n=48)
{
cout<<"0";
}
else
{
cout<<"wrong input";
}
getch();
}

u can boost to k.g kids that u can create multiplication of any number on computer


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i=1,j,n,a,b,c,d;
cout<<"enter the  digit whoes table is to be calculated";
cin>>n;
do
{
a=n*i;
cout<<a;
cout<<"\n";
i++;
}while(i<=10);
getch();
}

pattern


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i,j,n;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<i;
}
cout<<"\n";
}
getch();
}

OUTPUT
55555
 4444
  333
   22
    1

c++ program of adding 1st digit to last digit


add 1st digit and last digit
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i,j,n,a,b,c,d;
cout<<"enter the 4 digit no";
cin>>n;
a=n%10;
b=(n/1000)%10;
i=a+b;
cout<<i;
getch();
}