16.Eleminate repeated elements in Array:
#include<stdio.h>
int main(){
int a[15],i,j,k,size;
printf("enter array size to input\n");
scanf("%d",&size);
printf("Enter numbers\n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
for(i=0;i<size;i++)
{
for(j=i+1;j<size;)
{
if(a[j]==a[i])
{
for(k=j;k<size;k++)
{
a[k]=a[k+1];
}size--;
}else
j++;
}
}
for(i=0;i<size;i++)
{
printf("%d",a[i]);
}
return(0);
}
17.Printing range of prime numbers:
#include <stdio.h>
int main()
{
int n1, n2, flag, i, j;
printf("Enter two range:");
scanf("%d %d", &n1, &n2);
for(i=n1+1; i<n2; ++i)
{
flag=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\n",i);
}
return 0;
}
18. Maximum number of handshakes:
#include<stdio.h>
int main()
{
int num;
printf("Enter number of people");
scanf("%d",&num);
int max = num * (num-1) / 2; // Combination nC2
printf("%d",max);
return 0;
}
19. Coverting Decimal to Binary:
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
20.Converting Decimal to Hexadecimal:
#include<stdio.h>
int main() {
long int decNum,remainder,quotient;
int i=1,j,temp;
char hexdNum[100];
printf("Enter any decimal number: ");
scanf("%ld",&decNum);
quotient = decNum;
while(quotient!=0) {
temp = quotient % 16;
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexdNum[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d: ",decNum);
for (j = i -1 ;j> 0;j--)
printf("%c",hexdNum[j]);
return 0;
}
Other Related Posts:
Basic C Programs(PART 1)
Basic C Programs(PART 2)
Basic C Programs(PART 3)
Basic C Programs(PART 5)