Showing posts with label Loops. Show all posts
Showing posts with label Loops. Show all posts

Friday, June 26, 2009

PROGRAM TO EXCLUDE THE REPEATED ELEMENTS FROM AN ARRAY

#include "stdio.h"
#include "conio.h"
#include "alloc.h"
#define TRUE 1
#define FALSE 0

void main()
{
int *a1,*a2,*a3,i,j,size=0,repeated,num;
clrscr();
printf("How Many Numbers? ");
scanf("%d",&num);
a1=(int *) malloc(num*sizeof(int));
a2=(int *) malloc(num*sizeof(int));
a3=(int *) malloc(num*sizeof(int));
printf("\nEnter %d Numbers:\n",num);
for(i=0;i {
scanf("%d",&a1[i]);
/* Copying the elements of array a1 to another array a2 */
a2[i]=a1[i];
repeated=FALSE;
for(j=0;j if(a1[i]==a2[j])
{
repeated=TRUE;
break;
}
if(!repeated)
a3[size++]=a1[i];
}

printf("\nRepeated Numbers Excluded.");
printf("\nPrinting Third Array...\n");
for(i=0;i printf("%d\t",a3[i]);

free(a1);
free(a2);
free(a3);
getch();
}

PROGRAM TO PRINT THE TRANSPOSE OF MATRIX

#include "stdio.h"
#include "conio.h"
#define ROW 5
#define COL 4

void main()
{
int a[ROW][COL],i,j;
clrscr();
printf("Enter 5*4 Matrix:\n");
for(i=0;i for(j=0;j scanf("%d",&a[i][j]);

/* Transpose of Matrix can simply be printed */
/* by changing Rows and Columns */
printf("\n");
printf("Transpose of Matrix:\n");
for(j=0;j {
for(i=0;i printf("%d\t",a[i][j]);
printf("\n");
}
getch();
}

PROGRAM TO SORT 2 ARRAYS AND MERGE THEM INTO A SINGLE ARRAY

#include "stdio.h"
#include "conio.h"
#include "alloc.h"

void main()
{
int *a,*b,*c,i,j,m,n,temp;
clrscr();
printf("How many elements are there in array first? ");
scanf("%d",&m);
a=(int *)malloc(m*sizeof(int));
printf("\nEnter The Elements of Array first:\n");
for(i=0;i scanf("%d",&a[i]);
printf("\nHow many elements are there in array Second? ");
scanf("%d",&n);
b=(int *)malloc(n*sizeof(int));
printf("\nEnter The Elements of Array Second:\n");
for(i=0;i scanf("%d",&b[i]);

printf("\nSorting Both Arrays...\n");
for(i=0;i for(j=m-1;j>i;j--)
if(a[j] {
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}

for(i=0;i for(j=n-1;j>i;j--)
if(b[j] {
temp=b[j];
b[j]=b[j-1];
b[j-1]=temp;
}

c=(int *)malloc((m+n)*sizeof(int));
printf("\nCopying First array to Third array....\n");
for(i=0;i c[i]=a[i];
printf("\nCopying Second array to Third array...\n");
for(i=m,j=0;i<(m+n);i++,j++)
c[i]=b[j];
printf("\nNow Printing Third array...\n");
for(i=0;i<(m+n);i++)
printf("%d\t",c[i]);

free(a);
free(b);
free(c);
getch();
}

PROGRAM TO GENERATE THE PYRAMID OF *'S

#include "stdio.h"
#include "conio.h"

void main()
{
int n,i,j,k;
clrscr();
printf("Enter Number of lines: ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" "); /* Printing Space(s) */

for(k=1;k<=2*i-1;k++)
printf("*");

printf("\n"); /* Changing Line */
}
getch();
}

PROGRAM TO CONVERT BINARY TO DECIMAL

#include "stdio.h"
#include "conio.h"

long int pow(int,int);

void main()
{
long int num;
int digit,decimal=0,i=0;
clrscr();
printf("Enter Binary Number: ");
scanf("%ld",&num);

do
{
digit=num%10;
decimal+=digit*pow(2,i);
num/=10;
i++;
}while(num!=0);

printf("\nDecimal Equivalent - %d",decimal);
getch();
}

long int pow(int m,int n)
{
int i;
long int value=1;

for(i=1;i<=n;i++)
value*=m;

return value;
}

PROGRAM TO CHECK THE PALINDROME OF NUMBERS

#include "stdio.h"
#include "conio.h"

void main()
{
long num,reverse=0,digit,temp;
clrscr();
printf("Enter Number: ");
scanf("%ld",&num);
temp=num; /* saving the input to a temporary variable */
do
{
digit=num%10; /* Extracting Last Digit */
reverse=reverse*10+digit; /* to bring the digit to first decimal place */
num=num/10; /* to exclude last digit */
} while(num!=0);

printf("\n%ld ",reverse);
if(temp==reverse)
printf("Palindrome.");
else
printf("Not Palindrome.");
getch();
}

PROGRAM TO CONVERT DECIMAL TO BINARY

#include "stdio.h"
#include "conio.h"

void main()
{
long num,remain,binary=0,i;
clrscr();
printf("Enter Number: ");
scanf("%ld",&num);
i=1;
while(num!=0)
{
remain=num%2;
binary=binary+remain*i
num=num/2;
i*=10;
}
printf("%d\t",binary);
getch();
}

Saturday, June 20, 2009

PROGRAM TO CHECK PALINDROME OF NUMBERS

#include "stdio.h"
#include "conio.h"

void main()
{
long num,reverse=0,digit,temp;
clrscr();
printf("Enter Number: ");
scanf("%ld",&num);
temp=num;
do
{
digit=num%10;
reverse=reverse*10+digit;
num=num/10;
} while(num!=0);

printf("\n%ld ",reverse);
if(temp==reverse)
printf("Palindrome.");
else
printf("Not Palindrome.");
getch();
}

PROGRAM TO PRINT BOX

#include #include "conio.h"

void main()
{
int i,j,m,n;
clrscr();
printf("Enter row and column: ");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(j==1i==1j==ni==m)
printf("x");
else
printf(" ");
}
printf("\n");

}
getch();
}


by Ankit Pokhrel.