Thursday, July 30, 2009

Write a C program to delete an element from the array.

#include< stdio.h>
#include< conio.h>
int main( )
{
int a[20], len, i, n, item, j;
clrscr( );
printf("Enter how many elements you want to enter:");
scanf("%d", &len);

printf("\nEnter the array elements:");
for(i=0; i<'len' ; i++) // remove the single quotes in for loop
scanf("%d", &a[ i]);

printf("\nEnter the location, where you want to delete:");
scanf("%d", &n);

item= a[n-1];
printf("\nDeleted value is : %d", item);

for( j= n-1; j<'len-1; j++)

{ a[ j]= a[ j+1]; }

len=len-1;
printf("\nThe new element list :");
for(i=0; i<'len; i++)

printf("%5d", a[i]);

getch( );
return 0;
}

Output:-
Enter how many elements you want to enter: 4
Enter the array elements:
10
20
30
40
Enter the location, where you want to delete: 3
Deleted value : 30
The new element list: 10 20 40

Wednesday, July 29, 2009

Write a C program to Insert an element at the 'n' th position

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], len, i, j, x, n ;
clrscr( );
printf("Enter the array length:");
scanf("%d", &len);
printf("\nEnter the array elements:");
for(i=0; i<'len'; i++) //remove the single quotes in for loop
scanf("%d", &a[i]);
printf("\nEnter the 'n' th position:");
scanf("%d", &n);
printf("\nEnter the array element you want to insert:");
scanf("%d", &x);
for( j= len; j>=n; j--)
{ a[ j+1]= a[ j]; }
a[ j]= x;
len=len+1;
printf("\nThe New List is :\n");
for( i=0; i<'len'; i++) //remove the single quotes in for loop
printf("%5d", a[i] );
getch( );
return 0;
}
Output:-
Enter the array length : 4
Enter the array elements: 5
8
1
9
Enter the 'n' position: 4
Enter the array element you want to insert: 10
The new List is:
5
8
1
10
9
Note:- Here 'x' is for to store the 'n' position value

Write a C program to print all Combinations of characters A, B, C

#include(stdio.h)
#include(conio.h)
int main( )
{
char ch1, ch2, ch3;
clrscr( );
for(ch1='A' ; ch1<='C' ; ++ch1)
{
for(ch2='A' ; ch2<='C' ; ++ch2)
for(ch3='A' ; ch3<='C' ; ++ch3)
printf(" %c %c %c", ch1, ch2, ch3);
}
getch( );
return 0;
}

Output:-
AAA AAB AAC ABA ABB ABC ACA ACB ACC
BAA BAB BAC BBA BBB BBC BCA BCB BCC
CAA CAB CAC CBA CBB CBC CCA CCB CCC

Write a C Program to find HCF (GCD) between two numbers using Goto statement

#include< stdio.h>
#include< conio.h>
int main( )
{
int a, b, temp, rem,
clrscr( );
printf("Enter the required two input values:");
scanf(" %d%d",&a, &b);
if(a==0)
{ printf("\nHCF of number is : %d", b );
goto last;
}
if(b==0)
{ printf("\nHCF of number is : %d", a );
goto last;
}

// Here i'm taking a empty 'for' loop
// means no initial, final and incre/decrement

for( ; ; )
{
rem=a %b;
if(rem==0) break;
a= b;
b= rem;
}
printf("\nHCF of Number is : %d", b);

last:
getch( );
return 0;
}

Output:-
Enter the required two input values: 49
47
HCF of number is: 1

Write a C program to find the roots of a Quadratic equation

#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(math.h)
int main( )
{
int a , b, c, d;
float x1, x2;
ab:
printf("Enter the value of a, b, c:");
scanf("%d%d%d", &a, &b, &c);
if(a==0)
{
printf("\nIt is a Linear Equation");
prinft("\nRe-Enter the data again.");
goto ab;
}
d=(b*b)-(4*a*c);
if(d==0)
{
printf("\nRoots are real and equal");
x1= x2=(-b)/(2*a);
printf("\nRoots are: %f \t %f ", x1, x2);
}
else
{
if(d<0)
printf("\nRoots are imaginary");
else
{
x1=((-b)+sqrt(d))/(2*a);
x2=((-b)-sqrt(d))/(2*a);
printf("\nRoots are real");
printf("\nRoots are : %f \t %f", x1, x2);
}
}
getch( );
}
Output:-
Enter the values of a, b,c : 5
2
4
Roots are imaginary.

Write a C program to Convert any Decimal number into Binary coded decimal

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], n, ct=0, rem , i=0;
clrscr( );
printf("Enter the required decimal number:");
scanf("%d", &n);
printf("\nBinary Number is :");
while(n>=1)
{
rem= n%2;
a[i]= rem;
i++; // to increase the array location
ct++; // to count the no. of bits storing in an array
n=n/2;
}
for(i=ct ; i>=1; i-- )
printf("%3d", a[i] );
getch( );
return 0;
}

Output:
Enter the required decimal number: 5
Binary Number is : 1 0 1

Write a C program to Calculate the sum of the series sum=1+1/x+1/x2+1/x3+...........+1/xn

#include(stdio.h)
#include(conio.h)
#include(math.h)
int main( )
{
int i, x, n;
float sum, p;
clrscr( );
printf("Enter the base value 'x':");
scanf("%d", &x);
printf("\nEnter the power value 'n':");
scanf(" %d", &n);
sum=1;
for( i=1; i<=n ; i++)
{
p=pow(x, i);
sum=sum+(1/p);
}
printf("\nSum of the series: %f", sum);
getch( );
return 0;
}

Note:- if u have any douts regarding this program, please feel free to contact.