C Language is a high level procedural programming language, C language is also know as Middle level language because it have characteristics of both Low Level and High Level Programming Languages, it is called Procedural Programming because main organizing unit of c Program is function and a set of functions is called a Procedure.
It is also called Structured Language because its program goes in one structure from top to down. here are some sample programs written in C , just copy these programs one by one into any text editor like notepad or notepad ++ than run program and see the results and try to understand the logic, if you have any question regarding C you write into comment.
/*1. A simple
program which display some data*/
#include<stdio.h> // Preprocessor directives.
#include<conio.h> // Preprocessor directives.
/*
These files are included to each program by
defualt
so we wont declare them any more. */
void main(void) //Each c language program must start with
this function.
{ //Starting delimiter
char
EmpName[]="Ali",salgrade='B';
//Intialize character veriables.
int age=45; //Intialize integer
variable.
float salary=5567.70; //Intialize float variable.
clrscr(); // This function
use to clear the output screen.
/* printf() fuction is output
function use to display some data. */
printf("\n\n\t\t************
Employee Record ************");
printf("\n\n\t\t
Employee Name............=
%s",EmpName);
printf("\n\n\t\t
Employee age.............=
%d",age);
printf("\n\n\t\t Employee
Salary grade....= %c",salgrade);
printf("\n\n\t\t
Employee salary..........=
%.2f",salary);
getch(); //This function use to input a invisible
character.
//(Here used to
pause execution untill press any key.)
} //Ending delimiter
/*2. A program which read and display the bio data of
student. /*
void main(void)
{
/* variable declarations. */
char name[25];
char gender;
int age;
float height;
clrscr(); //clears screen
/* printf() prompts for input
and scanf() function gets input. */
printf("\n\n\t\t Enter
student's gender(M/F).......");
scanf("%c",&gender);
// scanf() function use to take input from keyboard
printf("\n\n\t\t Enter
student's name ..........");
scanf("%s",&name);
printf("\n\n\t\t Enter
student's age ..........");
scanf("%d",&age);
printf("\n\n\t\t Enter
studetn's height........");
scanf("%f",&height);
clrscr();
printf("\n\n\t\t
*********** student Bio data ************
");
printf("\n\n\t\t
Name ...........= %s",name);
printf("\n\n\t\t Gender
...........= %c",gender);
printf("\n\n\t\t Height
...........= %.2f",height);
printf("\n\n\t\t
Age ...........= %d",age);
getch();
}
/*3. A program for calculating the area of
triangle,volume of sphere and arrange result
in assending order. */
#include<math.h> //
this header file contains the math function
void main(void)
{
float
Base,alti,radius,area,vol,pi=22/7;
clrscr();
printf("\n\n\t\t Enter
length of Base of triangle ..........");
scanf("%f",&Base);
printf("\n\n\t\t Enter
length of altitude of triangle ...... ");
scanf("%f",&alti);
printf("\n\n\t\t Enter
length of radius of sphere ...........");
scanf("%f",&radius);
area=0.5*(Base*alti);
vol=(4.0/3.)*pi*pow(radius,3);
if(area>vol)
{
printf("\n\n\t\t Area of
triangle is ....= %.2f",area);
printf("\n\n\t\t volume
of sphere is ....= %.2f",vol);
}
else
printf("\n\n\t\t volume
of sphere is .....%.2f",vol);
printf("\n\n\t\t Area of
triangle is .....%.2f",area);
getch();
}
/*4. A program which prints multiplication table from
2 to 20 on each key press using nested for loop. */
void main(void)
{
int i,j;
clrscr();
for(i=2;i<=20;i++)
{
clrscr();
for(j=1;j<=10;j++)
{
printf("\n\n\t\t %d * %d =
%d",i,j,i*j);
}
printf("\n\n\t\tPress
any key to see next table.");
getch();
}
printf("\n\n\t\tPress
any key to exit");
getch();
}
/*5. A program
which uses while loop, for loop and
nested for
Loop to take the marks of five subjects. */
void main(void)
{
int i,marks,sum=0;
float ave;
char ch='y';
while(ch=='y'||ch=='Y')
{
clrscr();
printf("\n\n\t\tEnter
marks for five subjects(Between 33 to 100)");
for(i=2;i<=6;i++)
{
scanf("%d",&marks);
while(marks< 33 ||
marks>100)
{
printf("\n\n\tInvalid
marks. Try again.");
scanf("%d",&marks);
}
sum=sum+marks;
}
ave=sum/6;
printf("\n\n\t\t Total
marks.......... = %d",sum);
printf("\n\n\t\t
Average...............=%.3f",ave);
printf("\n\n\t\tDo you
want to calculate more student's data(Y/N).");
ch=getche();
getch();
}
}
/*6. Program to
find out the factoial of a given number. */
void main(void)
{
long number=1;
long answer;
clrscr();
while(number!=0)
{
printf("\n\n\t\t Enter a
number .....");
scanf("%d",&number);
answer=1;
while(number>1)
answer=answer*number--;
printf("\n\t\t Factorial
= %ld",answer);
getch();
}}
/* 7. Program
to display a checker board using if- else */
void main(void)
{
int row,column;
clrscr();
for(column=1;column<9;column++)
{
for(row=1;row<9;row++)
if((column+row)%2 ==0)
printf("\xDB\xDB");
else
printf(" ");
printf("\n");
}
getch();
}
/* 8. A Program to draw a checker board using nested
if-else statements */
void main(void)
{
int row,column;
clrscr();
for(column=1;column<16;column++)
{
for(row=1;row<16;row++)
if(row==column)
printf("\xDB");
else
if(column==16-row)
printf("\xDB ");
else
printf("\xCE");
printf("\n");
}
getch();
}
/* 9. A program for temperature conversion */
void main(void)
{
int op;
float kel,cel,fah;
clrscr();
printf("\n\n\t\t *****
Temperature conversion selection menu. ****");
printf("\n\n\t\t Kelvin
to Celius scale .........1");
printf("\n\n\t\t Celius
ot Kelvin scale .........2");
printf("\n\n\t\t Celius
to fahrenheit scale......3");
printf("\n\n\t\t
Fahrenheit to Celius scale......4");
printf("\n\n\t\t Enter
your choice.............");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n\n\t\t
***Conversion from Kelvin to Celius scale ***");
printf("\n\n\t\t Enter
temperature in kelvin scale.....");
scanf("%f",&kel);
cel=kel-273.0;
printf("\n\n\t\t %.2f
Kelvin = %.2f Celius ",kel,cel);
break;
case 2:
printf("\n\n\t\t
***Celius to Kelvin scale ***");
printf("\n\n\t\t Enter
temperature in Celius scale.....");
scanf("%f",&cel);
kel=cel+273;;
printf("\n\n\t\t %.2f
Celius = %.2f kelvin ",cel,kel);
break;
case 3:
printf("\n\n\t\t ***Conversion
from Celius to Fahrenheit scale
***");
printf("\n\n\t\t Enter
temperature in Celius scale.....");
scanf("%f",&cel);
fah=32.0+(9.0/5.0)*cel;
printf("\n\n\t\t %.2f
Celius = %.2f Fahrenheit ",cel,fah);
break;
case 4:
printf("\n\n\t\t
***Conversion from Fahrenheit to Celius scale ***");
printf("\n\n\t\t Enter
temperature in Fahrenheit scale.....");
scanf("%f",&fah);
cel=(5.0/9.0)*(fah-32.0);
printf("\n\n\t\t %.2f
Fahrenheit = %.2f Celius ",fah,cel);
break;
default:
printf("\n\n\n\t\t\tInvalid
option");
}
getch();
}
/* 10. A program for quiz game in which user can
answer the four questions and win 1000 per correct answer*/
/* The Quiz proram */
#include<stdio.h>
#include<conio.h>
char quiz1();
char quiz2();
char quiz3();
char quiz4();
void main(void)
{
char ans1,ans2,ans3,ans4;
clrscr();
printf("\t\t\t<<<...Do
You Think YOu Can Play...>>>\n");
printf("\n<<<...Give
Answers to Four Questions and Win 1000 Per Correct
Answer...>>>\n");
printf("\n\n\n\n\t\t<<<...Your
First Question Is...>>>");
ans1=quiz1();
if(ans1=='a')
printf("\n\t\t\t<<<...Correct
Answer Three Question to Go...>>>");
else
printf("\n\t\t\tOh!
Incorrect Answer...Better Luck Next time");
printf("\n\n\t\t<<<...Your
Second Question Is...>>>");
ans2=quiz2();
if(ans2=='b')
printf("\n\t\t\t<<<...Correct
Answer, Two Questions to Go...>>>\n");
else
printf("\n<<<...Oh!
Incorrect Answer ...Better Luck Next Time...>>>");
printf("\n\n\t\t<<<...Your
Third Question Is...>>>");
ans3=quiz3();
if(ans3=='c')
printf("\n\t\t\t<<<...Correct
Answer, one Question to Go...>>>");
else
printf("\n<<<...Oh!
Incorrect Answer ...Better Luck Next Time...>>>");
printf("\n\n\t\t<<<...Your
Fourth Question Is...>>>");
ans4=quiz4();
if(ans4=='d') {
printf("\n\t\t\t<<<...Correct
Answer...>>>");
printf("\n\n<<...Thank
You For Playing cash Has Been Transfered To Your
Account...>>>\n");
}
else
printf("\n<\t\t\t<<...Incorrect
Answer ...Thank You For Playing...>>>");
getch();
}
char quiz1()
{
char an1;
printf("\n\n\n\t\tQ. How
much types of loops does C support?");
printf("\n\n(a)\t
3\n(b)\t 4\n(c)\t 5\n(d)\t 6\n\n\nAnd your answer is:");
an1=getche();
return (an1);
}
char quiz2()
{
char an2;
printf("\n\n\n\t\tQ.Why
we use scanf function?");
printf("\n\n(a)\t make
decisions\n(b)\t take input\n(c)\t repeat code\n(c)\t break sequence\n(d)\t for
output\n\n\nAnd your answer is:");
an2=getche();
return (an2);
}
char quiz3()
{
char an3;
printf("\n\n\n\tQ. Why
we use escape sequences?");
printf("\n\n(a)\t Escape
from condition\n(b)\t for display\n(c)\t to break sequence of string\n(d)\t to
print characters\n\n\nAnd your answer is:");
an3=getche();
return (an3);
}
char quiz4()
{
char an4;
printf("\n\n\n\tQ.What
is shortcut key for saving c program?");
printf("\n\n\n\n(a)\t
Ctrl+s\n(b)\t Alt+s\n(c)\t ctrl+f\n(d)\t F2\n\n\nAnd your answer is:");
an4=getche();
}
/* 11. A program for custom function pow(x,yt) to
calculate value of x raised to y.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
long int power(int,int);
void main(void)
{
int num[2],i;
char var;
clrscr();
for(i=0;i<=1;i++)
{
if(i==0)
var='x';
else
var='y';
printf("\n\n\tEnter the
value of %c",var);
scanf("%d",&num[i]);
}
gotoxy(17,20);
printf("The value of x
Raised to Power y is %d",power(num[0],num[1]));
getch();
}
long int power(int no1,int
no2)
{
long int p=pow(no1,no2);
return(p);
}
/* 12. A program which receives 5 integers and returns
the product,sum and average of these numbers*/
#include<stdio.h>
#include<conio.h>
func(int *no,int *ps,long int
*pp,int *pa);
void main(void)
{
int num[5],sum=0,ave=0;
long int product=1;
int i;
clrscr();
for(i=0;i<5;i++)
{
gotoxy(25,(i+1)*2);
printf("Enter Value of
%d st.NO:",i+1);
scanf("%d",&num[i]);
func(&num[i],&sum,&product,&ave);
}
clrscr();
gotoxy(20,4);
printf("The sum of
%2d,%2d,%2d,%2d and %2d is:%8d:",num[0],num[1],num[2],num[3],num[4],sum);
gotoxy(20,8);
printf("The product of
%2d,%2d,%2d,%2d and %2d
is:%8d:",num[0],num[1],num[2],num[3],num[4],product);
gotoxy(20,12);
printf("The average of
%2d,%2d,%2d,%2d and %2d is:%8d:",num[0],num[1],num[2],num[3],num[4],ave);
getche();
}
func(int *no,int *ps,long int
*pp,int *pa)
{
*ps=*no+*ps;
*pp=*no* *pp;
*pa=*ps/5;
}
/* 13. A program of custom function that reads two integers array x and y
having four elements each prints the sum of products. */
#include<stdio.h>
#include<conio.h>
int op(int x,int y);
void main(void)
{
int x[4],y[4],sum,res,i;
clrscr();
for(i=0;i<4;i++)
{
gotoxy(25,(i+1)*2);
printf("Enter %dst
Element of Array 1:",i+1);
scanf("%d",&x[i]);
}
for(i=0;i<4;i++)
{
gotoxy(25,2*(i+7));
printf("Enter
%dstElement of Array 2: ",i+1);
scanf("%d",&y[i]);
}
for(i=0;i<4;i++)
{
res =op(x[i],y[i]);
sum=sum+res;
}
gotoxy(25,27);
printf("The sum of products 1s %d",sum);
getch();
}
op(int a,int b)
{
return (a*b);
}
/* 14. A program that reads the numbers entered by
user and reports if any of them matches, also prints the addresses of that
element using linear search.*/
void main(void)
{
int *num[10];
int i,j;
clrscr();
for(i=0;i<10;i++)
{
gotoxy(20,(2*i)+1);
printf("Enter number:
%d",i+1);
scanf("%d",num[i]);
}
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if((*num[i]==*num[j]&&num[i]!=num[j]))
{
printf("\n\n\tnumber: %d
matches with number: %d",i+1,j+1);
printf("\n\n\tAddress of
number: %d is %u & Value is %d",i+1,num[i],*num[i]);
printf("\n\n\tAddress of
number: %d is %u & Value is %d",j+1,num[j],*num[j]);
printf("\n\n");
}
getch();
}
15. /* A program that reads ten numbers from keyboard
and sort data into ascending order */
void main(void)
{
int num[10];
int i,j,max;
clrscr();
for(i=0;i<10;i++)
{
gotoxy(30.2*(1+i));
printf(" ");
printf("Enter number
%d",i+1);
scanf("%d",&num[i]);
}
clrscr();
max=num[0];
for(i=0;i<10;i++)
if(max<num[i])
max=num[i];
gotoxy(25,4);
printf("<<<The
Ascending Order Is>>>\n\n");
for(j=0;j<max;j++)
for(i=0;i<10;i++)
{
if(j==num[i])
printf("\t\t\t\t%d\n\n",num[i]);
}
getch();
}
/*16. A program that subtract two matrices.*/
void main(void) {
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
printf("\n\n\t\tEnter
the element a[%d][%d]",i+1,j+1);
scanf("%d",&a[i][j]);
}
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
printf("Enter the
element b[%d][%d]:",i+1,j+1);
scanf("%d",&b[i][j]);
}
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
c[i][j]=a[i][j]-b[i][j]; }
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
gotoxy(10+(5*j),12+(2*i));
printf("%4d",a[i][j]);
gotoxy(30+(5*j),12+(2*i));
printf("%4d",b[i][j]);
gotoxy(50+(5*j),12+(2*i));
printf("%4d",c[i][j]);
}
gotoxy(28,14);
printf("-");
gotoxy(48,14);
printf(" ");
getch();
}
/* 17. A program that multiply two matrices.*/
void main(void) {
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
printf("\n\n\t\tEnter
the element a[%d][%d]",i+1,j+1);
scanf("%d",&a[i][j]); }
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
printf("Enter the
element b[%d][%d]:",i+1,j+1);
scanf("%d",&b[i][j]);
}
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
c[i][j]=a[i][j]*b[i][j];
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
gotoxy(10+(5*j),12+(2*i));
printf("%4d",a[i][j]);
gotoxy(30+(5*j),12+(2*i));
printf("%4d",b[i][j]);
gotoxy(50+(5*j),12+(2*i));
printf("%4d",c[i][j]);
}
gotoxy(28,14);
printf("*");
gotoxy(48,14);
printf(" ");
getch();
}
]
//18. A program
to delete extra spaces from string
#include<stdio.h>
#include<conio.h>
void main(void)
{
char str[30];
int i;
printf("\n\n\t\tEnter a
string");
gets(str);
clrscr();
for(i=0;i<30;i++)
{
int num1=str[i];
int num2=str[i];
if(num1==32&num2==32)
str[i+1]='\b';
}
printf("\n\n\t\tThe
string <<<%s>>>",str);
getch();
}
/* 19. A program that copies a part of string
depending upon the numbers of character to be copied as input by user. */
#include<stdio.h>
#include<conio.h>
void main(void)
{
char str1[40];
char str2[40];
int i=0,stt,end,num;
clrscr();
printf("\n\n\t\tEnter a
String:");
while((str1[i]=getche())!='\r')
i++;
printf("\n\n\tEnter the
position from where you want to copy the string:");
scanf("%d",&stt);
stt=stt-1;
printf("\n\n\tEnter the
position Till where you want to copy the string:");
scanf("%d",&end);
end=end-1;
num=(stt-1);
for(i=stt;i<=end;i++)
{
str2[i-stt]=str1[num];
num++;
}
for(i=0;i<=(end-stt);i++)
putchar(str2[i]);
getch();
}
/* 20. A program to input a word from keyboard and
then print the number of vowels with appropriate messages */
#include<stdio.h>
#include<conio.h>
void main(void)
{
int vowel=0,i;
char str[30];
clrscr();
printf("\n\n\t\tEnter a
string");
gets(str);
clrscr();
for(i=0;i<30;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
vowel++;
}
printf("\n\n\t\tThere
are %d vowels in the string <<<%s>>>",vowel,str);
getch();
}
/*21. A Data
Base program to keep the record of Books using Binary files */
#include<stdio.h>
#include<conio.h>
typedef struct booksDB
{
char name[20];
char author[20];
char publisher[20];
int id;
int price;
}booksDB;
int main(void)
{
char bookname[20];
int id;
FILE *fp,*ft;
char another,choice;
booksDB books ;
char name[20];
char author[20];
long int recsize;
clrscr();
fp=fopen("db2.DAT","rb+");
if(fp==NULL)
{
fp=fopen(
"db2.DAT","wb+");
if(fp==NULL)
{
printf("Can't Open
File");
exit();
}
}
recsize=sizeof(books);
while(1) {
gotoxy(20,6);
printf("<<<===--RASHID
RAJPER AND BROTHERS BOOK STORE--===>>>");
gotoxy(26,8);
printf("<<<--SABZI
MARKET N.FEROZE-->>>");
gotoxy(14,10);
textcolor(GREEN+BLINK);
textbackground(BLUE);
cprintf("<<--DATA
BASE PROGRAM FOR KEEPING THE RECORD OF BOOKS-->>");
gotoxy(33,12);
textcolor(9);
textbackground(15);
cprintf("1.Add
Record");
gotoxy(33,14);
cprintf("2.Delete
Record");
gotoxy(33,16);
cprintf("3.Modify
Record");
gotoxy(33,18);
cprintf("4.View
Records");
gotoxy(33,20);
cprintf("5.To
Exit");
gotoxy(33,30);
cprintf("Select your
choice");
fflush(stdin);
scanf("%c",&choice);
clrscr();
textcolor(5);
textbackground(11);
switch(choice)
{
case'1':
fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y'||
another=='y')
{
printf("Enter book
name:");
scanf("%s",books.name);
printf("Enter Author of
book:");
scanf("%s",books.author);
printf("Enter the price
of book");
scanf("%d",&books.price);
printf("Enter the id of
book");
scanf("%d",&books.id);
fwrite(&books,recsize,1,fp);
printf("Add another
Record (Y/N): ");
fflush(stdin);
another=getchar();
}
break;
case '2':
another='Y';
while(another=='Y'||
another=='y')
{
printf("Enter the id of
the book to be deleted : ");
scanf("%d",&id);
ft=fopen("TEMPR.DAT","wb");
rewind(fp);
while(fread(&books,recsize,1,fp)==1)
{
if(strcmp(books.id,id)!=0)
fwrite(&books,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("db2.DAT");
rename("TEMPR.DAT","db2.DAT");
fp=fopen("db2.DAT","rb+");
printf("Delete another
Record(Y/N): ");
fflush(stdin);
another=getchar();
}
break;
case '3':
another='Y';
while(another=='Y'||
another=='y')
{
printf("Enter name of
book to modify : ");
scanf("%s",bookname);
rewind(fp);
while(fread(&books,recsize,1,fp)==1)
{
if(strcmp(books.name,bookname)==0)
{
printf("Enter new
name,author,publisher,price and quantity : ");
scanf("%s%s%s%d%d",books.name,books.author,books.publisher,&books.price,&books.id);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&books,recsize,1,fp);
break;
}
}
printf("Want to Modify
another record(Y/N): ");
fflush(stdin);
another=getchar();
}
break;
case '4':
rewind(fp);
while(fread(&books,recsize,1,fp)==1)
printf("Id =>
%d\nbook => %s\nauthor
=> %s\npublisher =>
%s\nprice =>
%d\n***************\n\n\n",books.id,books.name,books.author,books.publisher,books.price);
break;
case '5':
gotoxy(20,16);
printf("<<<--THANK
YOU FOR USING MY SOFTWARE-->>>");
gotoxy(20,18);
printf("<<<--WISH
YOU GOOD LUCK-->>>");
gotoxy(20,20);
printf("<<<--Press
any key to Exit-->>>");
getch();
fclose(fp);
exit();
default:
clrscr();
sound(500);
delay(200);
nosound();
gotoxy(30,32);
printf("<<<---WRONG
CHOICE--->>>");
gotoxy(29,34);
printf("<<--ENTER
FROM 1-5 PLEASE-->>");
}
}
}
/* 22. A
program for multiplication table using nested for loop */
void main(void)
{
int rows,cols;
clrscr();
printf("*******************************************************************************\n");
printf("\t\t <<<....MULTIPLICATION
TABLE....>>>\n\n");
printf("1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n");
printf("*******************************************************************************\n");
for(rows=1;rows<=10;rows++)
{
for(cols=1;cols<=10;cols++)
printf("%d\t",rows*cols);
printf("\n");
}
getch();
}
/*23. A simple
program for creating circle */
#include<graphics.h>
void main(void)
{
int driver=DETECT,mode=DETECT;
initgraph(&driver,&mode,"c:\\tc\\bgi");
setfillstyle(7,2);
circle(50,50,25);
floodfill(50,50,WHITE);
getch();
}
/*24. A program
to fill different colors with different styles on pressing 'y' */
#include<stdio.h>
#include<graphics.h>
void main(void)
{
int
driver=DETECT,mode=DETECT,a;
char ch='y';
initgraph(&driver,&mode,"c:\\tc\\bgi");
while(ch=='y')
{
for(a=0;a<=15;a++) {
if(ch=='n')
exit();
setfillstyle(a,a );
circle(50,50,25);
floodfill(50,50,WHITE) ;
ch=getch();
}}
}
/*25. A program
for simple animation */
#include<graphics.h>
#include<conio.h>
void main(void)
{
int
driver=DETECT,mode=DETECT;
int
area,x=25,y=25,ch,xdir=1,ydir=1,maxx,maxy;
int *buff;
initgraph(&driver,&mode,"c:\\tc\\bgi");
setcolor(WHITE);
setfillstyle(SOLID_FILL,RED);
circle(50,50,25);
floodfill(50,50,WHITE);
area=imagesize(25,25,75,75);
buff=malloc(area);
getimage(25,25,75,75,buff);
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,20,maxx,maxy);
outtextxy(250,10,"Animation");
while(1)
{
if(kbhit()) {
ch=getch();
/*If ENTER hit reverse the
direction movement*/
if(ch=='\r')
{
xdir=1;
ydir=1;
}
else
{
if(ch==27)
break;
}
}
putimage(x,y,buff,XOR_PUT);
delay(0);
x=x+(xdir*5);
y=y+(ydir*5);
putimage(x,y,buff,XOR_PUT);
if(x>maxx-50||x<0)
{
sound(50);
delay(10);
nosound();
xdir *=-1;
}
if(y>maxy||y<-1)
{
sound(50);
delay(10);
nosound();
ydir *=-12;
}}
getch();
closegraph();
restorecrtmode();
}
/*26. Simple
Drawing Sample Program */
#include<dos.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y;
int
array[]={540,220,590,270,570,320,510,320,490,270,540,220};
initgraph(&gd,&gm,"C:\\tc\\bgi");
x=getmaxx();
y=getmaxy();
setcolor(WHITE);
rectangle(x/30,y/20,x/5,y/4);
outtextxy(x/30+15,y/8+5,"Rectangle");
circle(x/2,y/6,75);
putpixel(x/2,y/6,WHITE);
outtextxy(x/2-textwidth("Circle")/2,y/6+10,"Circle");
arc(x/1.2,y/6,300,90,80);
outtextxy(x/1.2,y/6,"Arc");
line(x/30,10*y/15,x/6,10*y/15);
outtextxy(x/30+10,10*y/15+10,"Line");
ellipse(x/2,10*y/17,0,360,100,50);
putpixel(x/2,10*y/17,WHITE);
outtextxy(x/2-textwidth("Ellipse")/2,10*y/17+10,"Ellipse");
drawpoly(6,array);
outtextxy(515,270,"Polygon");
getch();
closegraph();
restorecrtmode();
}
/*27. A mouse
demonstration progam */
#include<graphics.h>
#include<conio.h>
#include<dos.h>
union REGS i,o;
void main(void) {
int
driver=DETECT,mode=DETECT,maxx,maxy,x,y,button;
clrscr();
initgraph(&driver,&mode,"c:\\tc\\bgi");
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,56,maxx,maxy);
setviewport(1,57,maxx-1,maxy-1,1);
gotoxy(26,1);
printf("Mouse
Demonstration Program");
if(initmouse()==0)
{
closegraph();
restorecrtmode();
printf("\nMouse driven
not loaded");
exit();
}
restrictmouseptr(1,57,maxx-1,maxy-1);
showmouseptr();
gotoxy(1,2);
printf("Left
Button");
gotoxy(15,2);
printf("Right
Button");
gotoxy(55,3);
printf("Press any key to
exit");
while(!kbhit())
{
getmousepos(&button,&x,&y);
gotoxy(5,3);
(button
&1)==1?printf("Down"):printf("Up");
gotoxy(20,3);
(button &2) ==2 ?
printf("DOWN"):printf("Up");
gotoxy(65,2);
printf("X=%03d
y=%03d",x,y);
}
}
//intialises mouse
initmouse() {
i.x.ax=0;
int86(0x33,&i,&o);
return (o.x.ax);
}
//display mouse pointer
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
//Restrict mouse movement
restrictmouseptr(int x1,int
y1,int x2, int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
//gets mouse coordinates and
button status
getmousepos(int *button,int
*x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
C Program on Conditions
Program#1: Simple if: C program to check marks , if marks will be greater than or equal to 8oo then it display Pass.
main()
{
int marks;
clrscr();
printf("Enter Marks of Student");
scanf("%d",&marks);
if(marks>=800)
printf("\nPass");
printf("\nPress any key to exit");
getch();
}
Program#2- If Else: C program to check marks if student, if marks will be greater then or equal to 800 it shows pass otherwise fail, if and else contain 1 statement.
main()
{
int marks;
clrscr();
printf("Enter Marks of Student");
scanf("%d",&marks);
if(marks>=800)
printf("\nPass");
else
printf("\nFail");
printf("\nPress any key to exit");
getch();
}
Program#3- If Else 2: Multiple statements in if and else
main()
{
int marks;
clrscr();
printf("Enter Marks of Student");
scanf("%d",&marks);
if(marks>=800)
{
printf("\nPass");
printf("\nExcellent");
}else {
printf("\nFail");
printf("\nTry Later!");
}
printf("\nPress any key to exit");
getch();
}
Program#4 - If Else : C Program to calculate discount ( 5%), if quantity is greater than or equal to 20
main()
{
char item[30];
int qty,price, total,dis;
clrscr();
printf("<<<<<<<<<<Sales Report>>>>>>>>>>>\n\n");
printf("Enter Item");
scanf("%s",&item);
printf("\nPrice of Item");
scanf("%d",&price);
printf("\nQty of Items");
scanf("%d",&qty);
total = qty * price;
if(qty>20)
dis = total*5/100;
else dis=0;
clrscr();
printf("<<<<<<<<<<Sales Report>>>>>>>>>>\n\n");
printf("??????? Output Result ??????????\n");
printf("\n\nItem: \t%s",item);
printf("\nRate:\t%d",price);
printf("\nQuantity:\t%d",qty);
printf("\nTotal is:\t%d",total-dis);
getch();
}
Program#4- If Else: Program to check , either number is odd or even.
main()
{
int n=2;
clrscr();
if(n%2==0)
printf("Number is Even");
else
printf("Number is Odd");
getch();
}
Program#5 - If Else : Program to perform calculations.
main()
{
char op;
int n1,n2;
clrscr();
printf("<<<<<<<<<<SIMPLE CALCULATOR >>>>>>>>>>>>\n\n");
printf("Please enter an operator:(+,-,*,/):\n");
scanf("%c",&op);
printf("\nEnter value 1:");
scanf("%d",&n1);
printf("\nEnter value 2:");
scanf("%d",&n2);
if(op=='+')
printf("\nAdditions of Values is:\t%d",n1+n2);
else if(op=='-')
printf("Subtraction of Values is:\t%d",n1-n2);
else if(op=='*')
printf("Multiplication of Values is:\t%d",n1*n2);
else if(op=='/')
printf("Division of Values is:\t%d",n1/n2);
else
printf("Wrong Operator");
getch();
}
Program#6- C program to demonstrate switch case default selection structure.
main()
{
char op;
int n1,n2;
clrscr();
printf("Enter Operator (+,-,*,/):\n");
scanf("%c",&op);
printf("Enter Value1:");
scanf("%d",&n1);
printf("Enter Value 2:");
scanf("%d",&n2);
switch(op)
{
case '+':
printf("\nAddition is:\t%d",n1+n2);
break;
case '-':
printf("\nSubtraction is:\t%d",n1-n2);
break;
case '*':
printf("\tMultiplication is:\t%d",n1*n2);
break;
case '/':
printf("Division is:\t%d",n1/n2);
break;
default:
printf("Wrong Operator");
}
getch();
}
C Programming Sample Programs |
0 Comments:
Post a Comment