Showing posts with label c example. Show all posts
Showing posts with label c example. Show all posts

Friday, April 20, 2012

C Program to Generate Analog Clock Using Graphics

It is a sample C program to generate analog clock using graphics in c programming. You run this code and enter the time Hour, Minutes and Second and press Enter. You will get Analog clock that start from your set time.

Program Code
//C program For Analog Clock
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
main()
{
int gd=DETECT,gm;
int xm,ym,i,j,k,hrs,min,sec;
clrscr();
// Entering the correct and current time with verification
printf("Enter the current time\n Hours: ");
scanf("%d",&hrs);
printf("Minutes: ");
scanf("%d",&min);
printf("Seconds: ");
scanf("%d",&sec);

while (hrs<=0||hrs>12)
 {
 printf("Enter the correct hour:");
 scanf("%d",&hrs);
 }

while (min<0||min>=60)
 {
 printf("Enter the correct minute:");
 scanf("%d",&min);
 }

while (sec<0||sec>=60)
 {
 printf("Enter the correct second:");
 scanf("%d",&sec);
 }

//   Initilization of graphics mode
initgraph(&gd,&gm,"..\\bgi");
xm=getmaxx();
ym=getmaxy();

//   Generating the gasic outline of the clock
for(i=0;i<3;i++)         //   Drawing outer circle in dark
 circle(xm/2,ym/2,200+i);

setcolor(4);

for (i=-1;i<2;i++)      //Drawing the 2 vertical lines in thick
 {
 line(xm/2+i,ym/2-200,xm/2+i,ym/2-170);
 line(xm/2+i,ym/2+200,xm/2+i,ym/2+170);
 }

for (i=-1;i<2;i++)        //Drawing 2 horizontal lines in thick
 {
 line(xm/2-200,ym/2+i,xm/2-170,ym/2+i);
 line(xm/2+200,ym/2+i,xm/2+170,ym/2+i);
 }

setcolor(15);

for (i=1;i<12;i++)                  //Drawing the remaining lines in light
 if ((i!=3)&&(i!=6)&&(i!=9))
  line(xm/2+200*cos(3.14*i/6),ym/2+200*sin(3.14*i/6),xm/2+170*cos

(3.14*i/6),ym/2+170*sin(3.14*i/6));

// outtextxy(xm/2+50,ym/2+225,"CLOCK MADE BY  D VAMSI KRISHNA");

// Setting the error in hours hand
if (hrs==12)
 hrs=0;
if (hrs==11)
 hrs=-1;
if (hrs==10)
 hrs=-2;
if (hrs==9)
 hrs=-3;


k=sec;
j=min*60+i;
i=hrs*60*60+j;

//   Drawing the lines of minutes hours n seconds and updating it regularly
while(!kbhit())
 {
  
 setcolor(9);
 outtextxy(xm/2-60,ym/2+75,"D VAMSI KRISHNA");
 setcolor(14);
 line(xm/2,ym/2,xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570));
 circle(xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570),3);
 setcolor(3);
 line(xm/2,ym/2,xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570));
 circle(xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570),3);
 setcolor(5);
 line(xm/2,ym/2,xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570));
 //circle(xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570),3);
 delay(1000);

 setcolor(0);
 line(xm/2,ym/2,xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570));
 circle(xm/2+100*cos(0.000145*i-1.570),ym/2+100*sin(0.000145*i-1.570),3);
 line(xm/2,ym/2,xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570));
 circle(xm/2+125*cos(0.001745*j-1.570),ym/2+125*sin(0.001745*j-1.570),3);
 line(xm/2,ym/2,xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570));
 //circle(xm/2+150*cos(0.1047*k-1.570),ym/2+150*sin(0.1047*k-1.570),3);

 if (i==32400)
  i=-10800;
 if (k==60)
  k=0;
 if (j==3600)
  j=3600;

 i++;
 j++;
 k++;

 }
getch();
closegraph();
return 0;
}

Output




Saturday, December 24, 2011

C Program to Print Patterns of Stars

It is a sample c program to print patterns of stars.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int i,j,n,k;
printf (“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
printf(“ ”);
}
for(k=1;k<=i;k++)
{
printf(“* ”);
}
printf(“\n”);
}
getch();
}

Output


Print Patterns of Stars

C Program to Print Patterns of Numbers

It is a sample c program to print patterns of numbers.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int i,j,n,k;
printf (“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
printf(“ ”);
}
for(k=1;k<=i;k++)
{
printf(“1”);
}
printf(“\n”);
}
getch();
}

Output


Print Patterns of Numbers

Check Whether Given Number is Armstrong or Not in C Program

It is a sample c program to check whether given number is Armstrong or not.

Program Source Code


#include"stdio.h"
#include"conio.h"
#include"math.h"
void main()
{
int number, a , i , j , sum=0 , count=0;
clrscr();
printf(“Enter any number”);
scanf(“%d”, &number);
i=number;
j=number;
while(j>0)
{
j=j%10;
count++;
}
while(i>0)
{
a=i%10;
sum=sum + pow(a, count);
i=i/10;
}
if(sum==number)
printf(“%d is a Armstrong number”, number);
else
printf(“%d is a not Armstrong number”, number);
getch();
}

Output


Armstrong or Not in C Program

C Program to Generate Fibonacci Series

It is a sample c program to generate a Fibonacci series.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int a , b , c , i=1;
clrscr();
a=0;
b=1;
printf(“Fibonacci Series”);
printf(“%d\n”, a);
printf(“%d\n”, b);
while(i<=10)
{
c=a+b;
printf(“%d”, c);
a=b;
b=c;
i++;
}
getch();
}

Output


Generate Fibonacci Series Output

Thursday, December 15, 2011

C Program to String Reverse

It is a basic C program to reverse a string.
In this c programming example user input a string and get reverse of accepted string.

Aim - C program to reverse accepted string [Using strrev function]


Program Code
//String Reverse using strrev function
#include"stdio.h"
#include"conio.h"
#include"string.h"
void main()
{
   char str[20];
   clrscr();
   printf("********* String Reverse Using strrev Function *******\n");
   printf("Enter a string:  ");
   gets(str);
   strrev(str);
   printf("\n Reverse string is:  %s",str);
   getch();
}
Output




Aim - C program to reverse accepted string [without using strrev function]


Program Code
//String Reverse without using strrev function
#include"stdio.h"
#include"conio.h"
#include"string.h"
void main()
{
 char str1[20],str2[20];
 int i,j=0;
 clrscr();
 printf("***********String Reverse*********\n");
 printf("Enter String:");
 gets(str1);
 for(i=strlen(str1)-1;i>=0;i--,j++)
 {
  str2[j]=str1[i];
 }
 str2[j]=NULL;
 printf("\nReverse String is:");
 puts(str2);
 getch();
}

Output

Why C is Called Middle Level Language?

Why C is Called Middle Level Language?

C language supports high level language which is user friendly and low level language which is machine friendly.
C combines features of a high level language with the functions of an assembly level language. It reduces the gap between low level language and high level language.
So C language called as middle level language.


C contains many features of high level language like modular programming, user friendly, loop, arrays etc. C language is also related to machine level language(low). It provides features like access to memory using pointers and assembly aspects.
C is one of the language who able to directly interact to system.
C language is used to develop both user applications and operating systems.

Thus, C is called as Middle Level Language.



Tuesday, November 22, 2011

C Program to Swap Two Numbers

It is a sample c program to swap two number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void swap(int,int);
void main()
{

int a,b;
clrscr();
printf("\n Enter value of a:-");
scanf("\n%d",&a);
printf("\n Enter value of b:-");
scanf("\n%d",&b);
printf("\n\n a before swapping is:--%d",a);
printf("\n\n b before swapping is:--%d",b);
swap(a,b);
printf("\n\n a after swapping is:--%d",a);
printf("\n\n b after swapping is:--%d",b);
getch();

}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\n\n a in swapping is:--%d",x);
printf("\n\n b in swapping is:--%d",y);
}

Output


c program to swap two number

C Program to Calculate Factorial of a Number

It is a sample c program to calculate factorial of a number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int fact=1,i,n;
clrscr();
printf("\n ***FIND THE FACTORIAL NUMBER OF ANY
NUMBER***");
printf("\n Enter the value of n:--");
scanf("\n %d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\n factorialis:-- %d",fact);
}
getch();
}

Output


Calculate Factorial of a Number

C Program to Check Largest Number in Two Variable

It is a sample C program to check largest number in two variable.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int a,b;
clrscr();
printf("***PRINT LARGEST VALUE AMONG TWO OR MORE
NUMBER***");
printf("Enter the value of a:--");
scanf("%d",&a);
printf("Enter the value of b:--")
scanf("%d",&b);
if(a>b)
{
printf("a is greater");
}
else
{
printf("b is greater");
}
getch();
}

Output


Largest Number in Two Variable in C Program

Program in C to Find Even or Odd Number

It is a sample c program to find even or odd number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int n ;
clrscr();
printf("\n ***Check 'even' ** or ** 'odd'**** ");
printf("\n Enter the number to check:-- ");
scanf("%d",&n);
if (n%2==0)
printf("\n Given no is even:--");
else
printf("\n Given no is odd:--");
getch();
}

Output


Even or Odd Number in C Program

Sunday, November 20, 2011

C Program to Find Armstrong Series

It is a sample c program to generate Armstrong number series from 1 to 1000. 

Program Source Code

//C Program to Find All Armstrong Number between 1 to 1000. #include"stdio.h" #include"conio.h" void main() { int no,r,sum=0,i; clrscr(); printf("\n\n***The Armstrong No. Series Between 1 and 1000 are:*** "); for(i=1;i<=1000;i++) { sum=0; no=i; while(no!=0) { r=no%10; no=no/10; sum=sum+(r*r*r); } if(sum==i) printf("\n%d",i); } getch(); }
Output