Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Wednesday, February 18, 2015

C program to enter a number and print it into words


C++ program to enter a number and print it into words

#include<iostream.h>
#include<conio.h>

void once(int a)
{
switch(a)
{
case 1: cout<<"One";
break;
case 2: cout<<"Two";
break;
case 3: cout<<"Three";
break;
case 4: cout<<"Four";
break;
case 5: cout<<"Five";
break;
case 6: cout<<"Six";
break;
case 7: cout<<"Seven";
break;
case 8: cout<<"Eight";
break;
case 9: cout<<"Nine";
break;
}
}

int tens(int a,int b)
{
int flag=0;
switch(a)
{
case 1:
flag=1;
switch(b)
{
case 0: cout<<"Ten";
break;
case 1: cout<<"Eleven";
break;
case 2: cout<<"Twelve";
break;
case 3: cout<<"Thirteen";
break;
case 4: cout<<"Fourteen";
break;
case 5: cout<<"Fifteen";
break;
case 6: cout<<"Sixteen";
break;
case 7: cout<<"Seventeen";
break;
case 8: cout<<"Eighteen";
break;
case 9: cout<<"Nineteen";
break;
}
break;
case 2: cout<<"Twenty";
break;
case 3: cout<<"Thirty";
break;
case 4: cout<<"Fourty";
break;
case 5: cout<<"Fifty";
break;
case 6: cout<<"Sixty";
break;
case 7: cout<<"Seventy";
break;
case 8: cout<<"Eighty";
break;
case 9: cout<<"Ninety";
break;
}
return(flag);
}

void hundred(int a)
{
switch(a)
{
case 1: cout<<"One Hundred";
break;
case 2: cout<<"Two Hundred";
break;
case 3: cout<<"Three Hundred";
break;
case 4: cout<<"Four Hundred";
break;
case 5: cout<<"Five Hundred";
break;
case 6: cout<<"Six Hundred";
break;
case 7: cout<<"Seven Hundred";
break;
case 8: cout<<"Eight Hundred";
break;
case 9: cout<<"Nine Hundred";
break;
}
}

void main()
{
clrscr();
int n,a[3],i=0,flag=0;
cout<<"Enter any number(max 3 digits):";
cin>>n;

while(n!=0)
{
a[i++]=n%10;
n=n/10;
}

for(i=i-1;i>=0;--i)
{
if(i==0&&flag==0)
once(a[0]);
if(i==1)
flag=tens(a[1],a[0]);
if(i==2)
hundred(a[2]);
cout<<" ";
}
getch();
}

Tuesday, February 17, 2015

C Program for Finding Transpose of a Sparse Matrix


Transpose of a matrix is obtained by interchanging rows and columns. In another way, we can say that element in the i, j position gets put in the j, i position. Transpose  of the matrix B1 is obtained as B2 by inserting (i,j)th element of B1 as (j,i)th element in B2.

Also Read: C program to read a matrix of size mxn and print its transpose

#include <stdio.h>

#define MAX 20

void printsparse(int[][3]);
void readsparse(int[][3]);
void transpose(int[][3],int[][3]);

void main()
{
    int b1[MAX][3],b2[MAX][3],m,n;
    printf("Enter the size of matrix (rows,columns):");
    scanf("%d%d",&m,&n);
    b1[0][0]=m;
    b1[0][1]=n;

    readsparse(b1);
    transpose(b1,b2);
    printsparse(b2);
}

void readsparse(int b[MAX][3])
{
    int i,t;
    printf("
Enter no. of non-zero elements:");
    scanf("%d",&t);
    b[0][2]=t;

    for(i=1;i<=t;i++)
    {
        printf("
Enter the next triple(row,column,value):");
        scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
    }
}

void printsparse(int b[MAX][3])
{
    int i,n;
    n=b[0][2];  //no of 3-triples
    printf("
After Transpose:
");

    printf("
row column value
");

    for(i=0;i<=n;i++)
        printf("%d %d %d
",b[i][0],b[i][1],b[i][2]);
}

void transpose(int b1[][3],int b2[][3])
{

    int i,j,k,n;
    b2[0][0]=b1[0][1];
    b2[0][1]=b1[0][0];
    b2[0][2]=b1[0][2];

    k=1;
    n=b1[0][2];

    for(i=0;i<b1[0][1];i++)
        for(j=1;j<=n;j++)
        //if a column number of current triple==i then insert the current triple in b2
            if(i==b1[j][1])
    {

            b2[k][0]=i;
            b2[k][1]=b1[j][0];
            b2[k][2]=b1[j][2];
            k++;
    }
}

C Program for Finding Transpose of a Sparse Matrix

Sunday, February 15, 2015

C Analog Clock Program

In this tutorial we are going to use a bit of graphics class to design a C# analog clock. We have also used a timer to continuously move our clock’s pointer.
                                     
Concept
  • We will first initialize some angle values to second, minute, hour hand.
  • Then draw the clock using the graphics class, save it as a bitmap variable and load it in the picturebox.
  • Finally Use the timer to move the second hand.

C# Analog Clock Program

Also Read: C/C++ TIC-TAC-TOE GAME
Also Read: Make Analog Clock in C Using Graphics

Instructions
  • Create a new windows form applicaton project in visual c#.
  • Add a picture box on the form.
  • Double click the form to switch to code view. 
  • Delete all the existing code and paste the code given below.

C# Analog Clock Program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnalogClock
{
public partial class Form1 : Form
{
Timer t = new Timer();

int WIDTH = 300, HEIGHT = 300, secHAND = 140, minHAND = 110, hrHAND = 80;

//center
int cx, cy;

Bitmap bmp;
Graphics g;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//create bitmap
bmp = new Bitmap(WIDTH + 1, HEIGHT + 1);

//center
cx = WIDTH / 2;
cy = HEIGHT / 2;

//backcolor
this.BackColor = Color.White;

//timer
t.Interval = 1000; //in millisecond
t.Tick += new EventHandler(this.t_Tick);
t.Start();
}

private void t_Tick(object sender, EventArgs e)
{
//create graphics
g = Graphics.FromImage(bmp);

//get time
int ss = DateTime.Now.Second;
int mm = DateTime.Now.Minute;
int hh = DateTime.Now.Hour;

int[] handCoord = new int[2];

//clear
g.Clear(Color.White);

//draw circle
g.DrawEllipse(new Pen(Color.Black, 1f), 0, 0, WIDTH, HEIGHT);

//draw figure
g.DrawString("12", new Font("Arial", 12), Brushes.Black, new PointF(140, 2));
g.DrawString("3", new Font("Arial", 12), Brushes.Black, new PointF(286, 140));
g.DrawString("6", new Font("Arial", 12), Brushes.Black, new PointF(142, 282));
g.DrawString("9", new Font("Arial", 12), Brushes.Black, new PointF(0, 140));

//second hand
handCoord = msCoord(ss, secHAND);
g.DrawLine(new Pen(Color.Red, 1f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

//minute hand
handCoord = msCoord(mm, minHAND);
g.DrawLine(new Pen(Color.Black, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

//hour hand
handCoord = hrCoord(hh % 12, mm, hrHAND);
g.DrawLine(new Pen(Color.Gray, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

//load bmp in picturebox1
pictureBox1.Image = bmp;

//disp time
this.Text = "Analog Clock - " + hh + ":" + mm + ":" + ss;

//dispose
g.Dispose();
}

//coord for minute and second hand
private int[] msCoord(int val, int hlen)
{
int[] coord = new int[2];
val *= 6; //each minute and second make 6 degree

if (val >= 0 && val <= 180)
{
coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
}
else
{
coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
}
return coord;
}

//coord for hour hand
private int[] hrCoord(int hval, int mval, int hlen)
{
int[] coord = new int[2];

//each hour makes 30 degree
//each min makes 0.5 degree
int val = (int)((hval * 30) + (mval * 0.5));

if (val >= 0 && val <= 180)
{
coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
}
else
{
coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
}
return coord;
}
}
}

Saturday, February 14, 2015

C program to find sum of series 1 2 4 5 7 8


C++ program to find sum of series 1/2+4/5+7/8+......


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr(); //to clrscr the screen
int i,n;
float sum=0,x,a=1;

cout<<"1/2+4/5+7/8+......";
cout<<"

How many terms(ex: 1,2,3...n)?";

cin>>n;


for(i=0;i<n;++i)
{
x=a/(a+1);
sum+=x;
a+=3;
}


cout<<"
Sum="<<sum;

getch(); //to stop the screen
}

Java program to swap two numbers without using temp variable

Java program to swap two numbers without using temp variable

class Swap
{
public static void main(String...s)
{
int a,b;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);

System.out.println("
Before Swap:
"+"a="+a+" b="+b);


a=a+b;
b=a-b;
a=a-b;

System.out.println("
After Swap:
"+"a="+a+" b="+b);

}
}

Friday, February 13, 2015

C Program for Array Representation of Stack Push Pop and Display


What is Stack?
Stack is a LIFO (last in first out) structure. It is an ordered list of the same type of elements. A stack is a linear list where all insertions and deletions are permitted only at one end of the list. When elements are added to stack it grow at one end. Similarly, when elements are deleted from a stack, it shrinks at the same end.

Also Read: C++ program to perform a PUSH operation on a dynamically allocated stack
Also Read: Menu Driven C Program to Perform Insert, Display and Delete Operations on a Singly Linked List (SLL)

Below I have written a C program that performs push, pop and display operations on a stack. It is implemented using 1-D array.

C Program for Array Representation of Stack [Push]

C Program for Array Representation of Stack [Delete]

  C Program for Array Representation of Stack [Display]

#include<stdio.h>
#include<conio.h>
#include<process.h>       //for exit()
#include<stdlib.h>         //for system("cls")

#define MAX 5               //Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
    int ch;
    while(1)                //infinite loop, will end when choice will be 4
    {
        system("cls");      //work in windows, for other OS change it to system("clear")
        printf("*** Stack Menu ***");
        printf("

1.Push
2.Pop
3.Display
4.Exit");
        printf("

Enter your choice(1-4):");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1: push();
                    break;
            case 2: pop();
                    break;
            case 3: display();
                    break;
            case 4: exit(0);

            default: printf("
Wrong Choice!!Press any key....");
                     getch();
        }
    }
}


void push()
{
    int val;
    if(top==MAX-1)
    {
        printf("
Stack is full!!Press any key....");
        getch();
    }
    else
    {
        printf("
Enter element to push:");
        scanf("%d",&val);
        top=top+1;
        stack[top]=val;
        printf("
Press any key....");
        getch();
    }
}


void pop()
{
    if(top==-1)
    {
        printf("
Stack is empty!!Press any key....");
        getch();
    }
    else
    {
        printf("
Deleted element is %d
Press any key....",stack[top]);
        top=top-1;
        getch();
    }
}


void display()
{
    int i;
    if(top==-1)
    {
        printf("
Stack is empty!!Press any key....");
        getch();
    }
    else
    {
        printf("
Stack is...
");
        for(i=top;i>=0;--i)
            printf("%d
",stack[i]);

    printf("
Press any key....");
    getch();
    }
}

Wednesday, February 11, 2015

C program to produce the following design using s


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

void main()
{
int i,j,k,n;
clrscr(); //to clear the screen
printf("How many lines?");
scanf("%d",&n);
n*=2;
printf("
");


for(i=0;i<n;i+=2)
{
for(j=1;j<i;j+=2)
printf(" ");
for(k=n-1;k>i;--k)
printf("*");
printf("
");

}
getch(); //to stop the screen
}

Tuesday, February 10, 2015

C Program to create an Analog Clock


C++ Program to create an Analog Clock

C++ Program to create an Analog Clock

#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#include<iostream.h>

int calculatehrs(int h)
{
int x;
switch(h)
{
case 0: x=90;
break;
case 1:
case 13: x=60;
break;
case 2:
case 14: x=30;
break;
case 3:
case 15: x=0;
break;
case 4:
case 16: x=330;
break;
case 5:
case 17: x=300;
break;
case 6:
case 18: x=270;
break;
case 7:
case 19: x=240;
break;
case 8:
case 20: x=210;
break;
case 9:
case 21: x=180;
break;
case 10:
case 22: x=150;
break;
case 11:
case 23: x=120;
break;
case 12:
case 24: x=90;
break;
}
return(x);
}

int calculatemin(int m)
{
int x;
if(m%5==0)
{
switch(m)
{
case 0: x=90;
break;
case 5: x=60;
break;
case 10: x=30;
break;
case 15: x=360;
break;
case 20: x=330;
break;
case 25: x=300;
break;
case 30: x=270;
break;
case 35: x=240;
break;
case 40: x=210;
break;
case 45: x=180;
break;
case 50: x=150;
break;
case 55: x=120;
break;
case 60: x=90;
break;
}
}
else
{
if(m>0&&m<15)
{
switch(m)
{
case 1: x=84;
break;
case 2: x=78;
break;
case 3: x=72;
break;
case 4: x=66;
break;
case 6: x=54;
break;
case 7: x=48;
break;
case 8: x=42;
break;
case 9: x=36;
break;
case 11: x=24;
break;
case 12: x=18;
break;
case 13: x=12;
break;
case 14: x=6;
break;
}
}

if(m>15&&m<30)
{
switch(m)
{
case 16: x=354;
break;
case 17: x=348;
break;
case 18: x=342;
break;
case 19: x=336;
break;
case 21: x=324;
break;
case 22: x=318;
break;
case 23: x=312;
break;
case 24: x=306;
break;
case 26: x=294;
break;
case 27: x=288;
break;
case 28: x=282;
break;
case 29: x=276;
break;
}
}

if(m>30&&m<45)
{
switch(m)
{
case 31: x=264;
break;
case 32: x=258;
break;
case 33: x=252;
break;
case 34: x=246;
break;
case 36: x=234;
break;
case 37: x=228;
break;
case 38: x=222;
break;
case 39: x=216;
break;
case 41: x=204;
break;
case 42: x=198;
break;
case 43: x=192;
break;
case 44: x=186;
break;
}
}

if(m>45&&m<60)
{
switch(m)
{
case 46: x=174;
break;
case 47: x=168;
break;
case 48: x=162;
break;
case 49: x=156;
break;
case 51: x=144;
break;
case 52: x=138;
break;
case 53: x=132;
break;
case 54: x=126;
break;
case 56: x=114;
break;
case 57: x=108;
break;
case 58: x=102;
break;
case 59: x=96;
break;

}
}

}
return(x);
}

int changehrs(int m,int a)
{
if(m>15&&m<=30)
a-=12;
if(m>30&&m<=45)
a-=18;
if(m>45&&m<60)
a-=24;
return (a);
}

void main()
{
int gdriver=DETECT,gmode,h,m,s,a,b,c;
initgraph(&gdriver,&gmode,"c:\tc\bgi");

struct  time t;
gettime(&t);
h=t.ti_hour;
m=t.ti_min;
s=t.ti_sec;

a=calculatehrs(h);
b=calculatemin(m);
c=calculatemin(s);
a=changehrs(m,a);

for(int i=a;i>0;i-=6)
for(int j=b;j>0;j-=6)
for(int k=c;k>0;k-=6)
{
setbkcolor(7);
settextstyle(1,HORIZ_DIR,5);
setcolor(BLUE);
outtextxy(190,20,"Analog Clock");
settextstyle(8,HORIZ_DIR,2);

setcolor(BLUE);
circle(300,200,102);
setcolor(YELLOW);
circle(300,200,100);
outtextxy(385,185,"3");
outtextxy(288,98,"12");
outtextxy(207,185,"9");
outtextxy(295,270,"6");

circle(345,123,2);
circle(378,155,2);
circle(378,245,2);
circle(345,280,2);
circle(253,278,2);
circle(223,245,2);
circle(223,155,2);
circle(253,123,2);

setcolor(RED);
pieslice(300,200,i-1,i,75);
setcolor(WHITE);
pieslice(300,200,j-1,j,85);

setcolor(BLUE);
pieslice(300,200,k-1,k,95);

setcolor(RED);
settextstyle(3,HORIZ_DIR,1);
outtextxy(360,400,"Press any key to exit...!!");
sleep(1);
clearviewport();

if(i==6)
a=360;
if(j==6)
b=360;
if(k==6)
c=360;

if(kbhit())
{
setcolor(YELLOW);
setbkcolor(BLUE);
settextstyle(1,HORIZ_DIR,8);
outtextxy(130,150,"Thank You");
sleep(3);
exit(0);
}

}
}