Thursday, 29 August 2013

Program In C For Insertion and deletion of numbers to an array.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,choice,value,pos,A[50];
clrscr();
printf("enter the number of elements in A");
scanf("%d",&n);
printf("Enter the %d elements\n",n);
for(c=0;c<n;c++)
scanf("%d",&A[c]);
printf("1.Insertion\t2.Deletion");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter the location where you wish to insert element");
scanf("%d",&pos);
printf("enter the value to insert");
scanf("%d",&value);
for(c=n-1;c>=pos-1;c--)
A[c+1]=A[c];
A[pos-1]=value;
printf("A is ");
for(c=0;c<=n;c++)
printf("%d\t",A[c]);

}
else if(choice==2)
{
printf("Enter the location where you wish to delete element");
scanf("%d",&pos);
if(pos>=n+1)
printf("Deletion not possible");
for(c=pos-1;c<n;c++)
A[c]=A[c+1];
printf("A after delition is  ");
for(c=0;c<n-1;c++)
printf("%d\t",A[c]); }
else

printf(“Wrong Choice !!!!”);
getch();
}

Tuesday, 7 May 2013

Program In C To Find Sum Of Digits Of A Number Using Recursion

#include<stdio.h>
#include<conio.h>
int fun(int num)
{ int rem,sum;
if(num==0)
return 0;
else
{ rem=num%10;
sum=rem+fun(num/=10); }
return sum;
}
void main()
{ int n,sod;
clrscr();
printf("Enter Number: ");
scanf("%d",&n);
sod=fun(n);
printf("Sum Of Digits Is: %d",sod);
getch();
}

Program In C To Calculate Size Of Different Data Types


#include <stdio.h>
#include <conio.h>

int main(){

int *a;
printf("\nSIZE OF DIFFERENT DATA TYPES\n");
printf("\n------------------------------");
printf("\n\tBASIC");
printf("\n------------------------------");
printf("\nCHARACTER \t: %d byte(s)",sizeof(char));
printf("\nSHORT \t\t: %d byte(s)",sizeof(short));
printf("\nINTEGER \t: %d byte(s)",sizeof(int));
printf("\nLONG \t\t: %d byte(s)",sizeof(long));
printf("\nFLOAT \t\t: %d byte(s)",sizeof(float));
printf("\nDOUBLE \t\t: %d byte(s)",sizeof(double));
printf("\nLONG DOUBLE \t: %d byte(s)",sizeof(long double));
printf("\nLONG LONG \t: %d byte(s)",sizeof(long));
printf("\nPOINTER (any) \t: %d byte(s)",sizeof(*a));
printf("\nARRAY \t\t: sizeOfDataType * sizeOfArray [eg. int a[10]=%d byte(s)]",sizeof(int)*10);
printf("\n------------------------------");

return 0;
}

Program In C To Calculate sin,cos and tan values for an angle


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n,a,b,c;
clrscr();
printf("Enter the value for the angle: ");
scanf("%f",&n);
a=sin(n);
b=cos(n);
c=tan(n);
printf("Sin(%f) is: %2.2f\n",n,a);
printf("Cos(%f) is: %2.2f\n",n,b);
printf("Tan(%f) is: %2.2f\n",n,c);
getch();
}

Program In C To Check Whether A Number Is Prime Number Or Not


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{ int num,i;
clrscr();
printf("Enter Number: ");
scanf("%d",&num);
if(num==1)
{printf("It Is Not A Prime Number");
exit(0);}
for(i=2;i<=((num/2)+1);i++)
{ if(num%i==0)
{ printf("It Is Not A Prime Number");
break;}
else if(i==(num/2)+1)
printf("It Is A Prime Number");
}
getch();
}

Program In C To Calculate exponential power i.e. e(x,y) using recursion


#include<stdio.h>
#include<conio.h>
int fun( int x, int y)
{ int z;
if(y==0)
return 1;
else
z=x*fun(x,y-1);
return z;
}
void main()
{ int a,b,c;
printf("Enter base: ");
scanf("%d",&a);
printf("Enter exponent: ");
scanf("%d",&b);
c=fun(a,b);
printf("Answer Is: %d",c);
getch();
}

Program In C To Find Out Quadrant Of An Angle


#include<stdio.h>
int main(void)
{
int angle;
 printf("enter an angle in degrees:");
scanf("%d",&angle);
angle=angle%360;
if(angle%90==0) printf("angle %d lies on an axis\n",angle);
else
if( angle >0 && angle <90)
{
printf("\nThe angle is in quadrant I");
}
else if( angle >90 && angle <180)
{
printf("\nThe angle is in quadrant II");
}
else if(angle >180 && angle <270)
{
printf("\nThe angle is in quadrant III");
}
else if (angle >270 && angle <360 )
{
printf("\nThe angle is in quadrant IV");
}

if(angle%90==0)
switch(angle/90){

case 0: printf("\nThe angle is on the positive X axis");
break;
case 1:
printf("\nThe angle is on the positive Y axis");
break;
case 2:
printf("\nThe angle is on the negative X axis");
break;
case 3:
printf("\nThe angle is on the negative Y axis");
break;
default:
printf("error");
}
return 0;
}