Friday 26 July 2013

C program to convert numbers into billion, million, thousand (words)

/*
Name: C program to convert numbers into words 
(Short scale Arabic numbering system i.e. ten, hundred, thousand, million, billion)
Author: Prashant Jain (Google plus: https://plus.google.com/110117450187891236113)
Date: 26-07-13 14:09
Description: This program is written to demonstrate the English Translation of numbers up to 4 billion. 
Compiler: Turbo C++ 3.0
*/

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

int main()
{
 int skip1=0,skip2=0,skip3=0;
 int choice;
unsigned long n;
int p1,p2,p3,p4,p5,p6,q2,r2,s2,q3,r3,s3;
char c[20][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
char c2[11][10]={"Zero","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety","Hundred"};
char string[100]="";

while(1)
{
 p1=p2=p3=p4=p5=p6=q2=r2=s2=q3=r3=s3=0;
 printf("Enter any number upto 4 billion: ");
scanf("%d",&n);
if(n==0)
printf("zero");
p1=n/1000000000;
n=n%1000000000;

p2=n/1000000;
q2=p2/100;
p2=p2%100;
r2=p2/10;
p2=p2%10;
s2=p2;
n=n%1000000;

p3=n/1000;
 q3=p3/100;
p3=p3%100;
r3=p3/10;
p3=p3%10;
s3=p3;
n=n%1000;

p4=n/100;
n=n%100;

p5=n/10;
n=n%10;

p6=n;

if(p1!=0)
{
 strcat(string,c[p1]);
 strcat(string," Billion ");
}

if(p2!=0){
if(q2!=0){
strcat(string,c[q2]);
strcat(string," hundred ");
if(r2!=0){
if(r2==1){
strcat(string,c[10+s2]);
skip1=1;
}
if(skip1!=1){
strcat(string,c2[r2]);
}
strcat(string," ");
if(skip1!=1){
if(s2!=0){
strcat(string,c[s2]);
}

}
}
else{
strcat(string," ");
if(skip1!=1){
if(s2!=0){
strcat(string,c[s2]);
}

}
}
}
else{
if(r2!=0){
if(r2==1){
strcat(string,c[10+s2]);
skip1=1;
}
if(skip1!=1){
strcat(string,c2[r2]);
}
strcat(string," ");
if(skip1!=1){
if(s2!=0){
strcat(string,c[s2]);
}

}
}
else{
strcat(string," ");
if(s2!=0){
strcat(string,c[s2]);
}
}
}
strcat(string," million ");
}

if(p3!=0){
if(q3!=0){
strcat(string,c[q3]);
strcat(string," hundred ");
if(r3!=0){
if(r3==1){
strcat(string,c[10+s3]);
skip2=1;
}
if(skip2!=1){
strcat(string,c2[r3]);
}
strcat(string," ");
if(skip2!=1){
if(s3!=0){
strcat(string,c[s3]);
}

}
}
else{
if(s3!=0){
strcat(string,c[s3]);
}
}

}
else{
if(r3!=0){
if(r3==1){
strcat(string,c[10+p6]);
skip2=1;
}
if(skip2!=1){
strcat(string,c2[r3]);
}
strcat(string," ");
if(skip2!=1){
if(s3!=0){
strcat(string,c[s3]);
}

}
}
else{
if(s3!=0){
strcat(string,c[s3]);
}
}
}
strcat(string," thousand ");

}
if(p4!=0){
strcat(string,c[p4]);
strcat(string," hundred ");
}
if(p5!=0){
if(p5==1){
strcat(string,c[10+p6]);
skip3=1;
}
if(skip3!=1){
strcat(string,c2[p5]);
}
strcat(string," ");
}
if(skip3!=1){

if(p6!=0) {
strcat(string,c[p6]);
}
}

printf("%s",string);
choice=getch();
if(choice==27) exit(1);
printf("\n\n");
strcpy(string,"");

fflush(stdin);
}
}

Sunday 23 June 2013

Midpoint ellipse drawing algorithm

/* compiler- Turbo c++ author- Mangilal Sharma =================================
*/ 
#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void plotpoints(int cx, int cy, int x, int y)
{
    putpixel(cx + x, cy + y, 4);
    putpixel(cx - x, cy + y, 4);
    putpixel(cx + x, cy - y, 4);
    putpixel(cx - x, cy - y, 4);
}

void main()
{
    int cx, cy, rx, ry;

    printf("Enter the center ");
    scanf("%d%d", &cx, &cy);
    printf("x radius : ");
    scanf("%d", &rx);
    printf("y radius : ");
    scanf("%d", &ry);

    long rx2 = (long) rx * rx;
    long ry2 = (long) ry * ry;
    long trx2 = 2 * rx2;
    long try2 = 2 * ry2;
    long p, x = 0, y = ry;
    long px = 0;
    long py = trx2 * y;

    p = (long) ((ry2 - (rx2 * ry) + (0.25 * rx2)) + 0.5);

    int gd = DETECT, gm = DETECT;
    initgraph(&gd, &gm, "");
    cleardevice();

    putpixel(cx, cy, 15);

    while (px < py) {
    plotpoints(cx, cy, x, y);
    x++;
    px += try2;
    if (p < 0)
        p += ry2 + px;
    else {
        y--;
        py -= trx2;
        p += ry2 + px - py;
    }
    }
    py = trx2 * y;
    px = try2 * x;
    p = (long) ((ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2) + 0.5);
    while (y >= 0) {
    plotpoints(cx, cy, x, y);
    y--;
    py -= trx2;
    if (p > 0)
        p += rx2 - py;
    else {
        x++;
        px += try2;
        p += rx2 - py + px;
    }
    }

    getch();
}
/*=============================19Nov.,2010=============================*/



 

Midpoint circle drawing algorithm

/* compiler- Turbo c++ author- Mangilal Sharma =================================
Midpoint circle algorithm=================================================*/ 
#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void plotpoints(int x, int y, int cx, int cy)
{
    putpixel(cx + x, cy + y, 4);
    putpixel(cx - x, cy + y, 4);
    putpixel(cx + x, cy - y, 4);
    putpixel(cx - x, cy - y, 4);
    putpixel(cx + y, cy + x, 4);
    putpixel(cx - y, cy + x, 4);
    putpixel(cx + y, cy - x, 4);
    putpixel(cx - y, cy - x, 4);
}

void main()
{
    int cx, cy, x = 0, y, r, p;
    int gd = DETECT, gm = DETECT;

    clrscr();

    printf("Enter the center \n");
    scanf("%d%d", &cx, &cy);
    printf("Enter the radius : ");
    scanf("%d", &r);

    y = r;
    p = 1 - r;

    initgraph(&gd, &gm, "");
    cleardevice();

    while (x < y) {
    plotpoints(x, y, cx, cy);
    x++;
    if (p < 0)
        p += 2 * x + 1;
    else {
        y--;
        p += 2 * (x - y) + 1;
    }
    }

    getch();

}
/*=============================19Nov.,2010=============================*/


Bresenham's line drawing algorithm

/* compiler- Turbo c++ author- Mangilal Sharma =================================*/ 
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void swap(int &x, int &y)
{
    int k = x;
    x = y;
    y = k;
}

void main()
{
    int gd = DETECT, gm = DETECT, x1, x2, y1, y2, dx, dy, p, k;
    float m = 0;

    clrscr();

    printf("Enter the sarting point x1 & y1\n");
    scanf("%d%d", &x1, &y1);

    printf("Enter the end point x2 & y2\n");
    scanf("%d%d", &x2, &y2);

    dx = abs(x2 - x1);
    dy = abs(y2 - y1);
    m = (float) (y2 - y1) / (x2 - x1);

    initgraph(&gd, &gm, "");
    cleardevice();

    if (fabs(m) > 1) {
    swap(x1, y1);
    swap(x2, y2);
    swap(dx, dy);
    }

    if ((x1 > x2)) {
    x1 = x2;
    y1 = y2;
    }
    p = 2 * dy - dx;
    for (k = 0; k < abs(dx); k++) {
    if (p < 0) {
        p = p + 2 * dy;
    } else {
        if (m < 0)
        y1--;
        else
        y1++;
        p = p + (2 * dy) - (2 * dx);
    }
    if (fabs(m) <= 1)
        putpixel(x1++, y1, 15);
    else
        putpixel(y1, x1++, 15);
    }

    getch();
}
/*=============================19Nov.,2010=============================*/


Saturday 22 June 2013

DDA (Digital differential analyzer) Line Drawing Algorithm

/* compiler- Turbo c++ author- Mangilal Sharma =================================
DDA (Digital differential analyzer) Line Drawing Algorithm========================*/ 
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{

    int gd = DETECT, gm = DETECT, s, dx, dy, m, x1, y1, x2, y2;
    float xi, yi, x, y;

    clrscr();

    printf("Enter the sarting point x1 & y1\n");
    scanf("%d%d", &x1, &y1);

    printf("Enter the end point x2 & y2\n");
    scanf("%d%d", &x2, &y2);

    initgraph(&gd, &gm, "");
    cleardevice();

    dx = x2 - x1;
    dy = y2 - y1;

    if (abs(dx) > abs(dy))
    s = abs(dx);
    else
    s = abs(dy);

    xi = dx / (float) s;
    yi = dy / (float) s;

    x = x1;
    y = y1;

    putpixel(x1, y1, 4);

    for (m = 0; m < s; m++) {
    x += xi;
    y += yi;
    putpixel(x, y, 4);
    }
    getch();
}
/*=============================19Nov.,2010=============================*/


Friday 21 June 2013

Armstrong number (Entered no. is armstrong or not)

/* compiler- Turbo c++ author- Mangilal Sharma =================================*/ 
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
 int n,a,b,arm=0;
 clrscr();
 printf("Enter a number: ");
 scanf("%d",&n);
 b=n;
 while(n>0)
 {
  a=n%10;
  n=n/10;
  arm=arm+pow(a,3);
 }
 printf("Entered digit's sum of the cubes is: %d\n",arm);
 if(arm==b)
 printf("Entered no. is armstrong");
 else
 printf("Entered no. is not armstrong");
 getch();
}
/*=============================19Nov.,2010=============================*/

C program to print reverse of a entered number

/* compiler- Turbo c++ author- Mangilal Sharma =================================*/ 
#include <conio.h>
#include <stdio.h>

void main()
{
 clrscr();
 int sum=0,num,a,n;
 printf("Enter No. in range of int (less than 32768): ");
 scanf("%d",&num);
 printf("Reverse of '%d' is: ",num);
 while(num>0)
 {
  a=num%10;
  sum=sum+a;
  printf("%d",a);
  num=num/10;
 }
 getch();
}
/*=============================19Nov.,2010=============================*/

Saturday 6 April 2013

Sum of n prime numbers

/* compiler- Turbo c++ author- Mangilal Sarswat --------------------------------------------------------*/

/* A program to find out sum of prime numbers. Suppose that user enter n=5 then output must be 28 that is sum of 5 prime values 2+3+5+7+11. You can modify this program for limited range as sum of prime numbers that come between 100 to 1000 etc.*/

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

long sum(int n)
{
long count=1, sum=2;
for(long i=3; ;i++)
{
int pass=0;
for(long j=2; j<=ceil(sqrt(i)); j++)
{
if(i%j==0)
{
pass=1;
break;
}
}
if(pass==1);
else
{
count++;
sum=sum+i;
}
if(count==n) break;
}
return sum;
}

void main()
{
 while(1)
 {
  clrscr();
  int n;
  printf("Enter number of prime values");
  scanf("%d",&n);
  printf("Total of starting %d prime values is %d",n,sum(n));
  getch();
  if(n==99) exit(1);
 }
}

 /*==========================6 April, 2013 2:00 PM==========================*/

Saturday 9 March 2013

Count reapetation number of unique values of an array (Sort according higher occurance of values)


/* A program to calculate no. of uniqe values of an array
implemented logic can be useful anywhere-
where we required count no. of same rate books in a library
e.g. 250$: 3books 300$: 4books 34$: 5books
Date: 9/3/2013 10:05pm
Author: Mangilal Saraswat */

#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,j,k,done,a[10],b[10],c[10];
 i=0; j=0; k=0; done=0;

 printf("Enter array:\n");
 for(i=0;i<10;i++)
 {
  scanf("%d",&a[i]);
  c[i]=0;
 }

 for(i=0; i<10; i++)
 {
  for(j=0;j<k;j++)
  {
   if(a[i]==b[j])
   {
    b[j]=a[i];
    c[j]=c[j]+1;
    done=1;
   }
  }
  if(done==0)
  {
   b[k]=a[i];
   c[k]=c[k]+1;
   k=k+1;
  }
  done=0;
 }

 for(j=0;j<k;j++)
 {
  printf("value: %d\t",b[j]);
  printf("times: %d\n",c[j]);
 }
 getch();
}