HOME http://bool.cjb.net
WELCOME TO HASIB'S WEB WORLD
PROGRAMMING WITH C
(Some sample program code) Helpful for the beginners
SAMPLE 1 (USING SWITCH)
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
int addi(int x, int y)
{ int c;
c=x+y;
return(c);
}
int sub(int k,int t)
{ int d;
d=k-t;
return(d);
}
int multi(int q, int w)
{ int y;
y=q*w;
return(y);
}
float divi(int q, int w)
{ float y;
y=q/w;
return(y);
}
void main()
{ int j=1;
int m,p,a,b,r,e,ch;
char *u;
float di;
/*printf("\n Login Name: ");
u= (char *)malloc(10);
scanf(" %s",u); */
printf("\n Password: ");
scanf("%d",&p);
if (p==1234)
{
while (j>0)
{ clrscr();
printf("\n\n 1. Input\n");
printf(" 2. Addition\n");
printf(" 3. Subtraction\n");
printf(" 4. Multiplication\n");
printf(" 5. Division\n");
printf(" 6. Exit\n");
printf("\n Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the first value ");
scanf("%d",&a);
printf("\nEnter the second value ");
scanf("%d",&b);
printf("\n Input is complete");
printf("\n\n Press any key to continue... ");
getch();
break;
case 2:r=addi(a,b);
printf("The addition is %d ",r);
printf("\n\nPress any key to continue...");
getch();
break;
case 3:e = sub(a,b);
printf("The Subtraction is %d ",e);
printf("\n\nPress any key to continue...");
getch();
break;
case 4:m=multi(a,b);
printf("The Multiplication is %d ",m);
printf("\n\nPress any key to continue...");
getch();
break;
case 5:di=divi(a,b);
printf("The Division is %f ",di);
printf("\n\nPress any key to continue...");
getch();
break;
case 6:clrscr();
printf("\n THIS PROGRAM IS WRITTEN BY: ");
printf("\n\n\t\t MD. HASIBUR RASHID (HASIB)");
printf("\n\t\t\t\t MCSE, A+");
printf("\n B.Sc.(Hon's) in COMPUTER SCIENCE, 3RD YEAR");
printf("\n\n\t ADDRESS: ");
printf("\n\t\t 172/A, RD.-4,");
printf("\n\t\t Mohammadi Housing Ltd.");
printf("\n\t\t Mohammadpur, Dhaka-1207");
printf("\n\t\t Bangladesh.");
printf("\n\t\t email: [email protected]\n\t\tWeb:
http://bool.cjb.net\n");
getch();
j=j-1; break;
}
}
}else printf("\nInvalid Login Name or Wrong password") ;
getch();
}
SAMPLE 2 (USING STRUCTURE)
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
#include<stdlib.h>
struct st {
char *name;
int roll;
int cs201,cs202,cs203,cs204,cs205,cs206,cs207;
int tmarks;
} stud[100] ;
void main()
{
int i,j,tmp,n,rmp;
char *emp;
printf(" How many students? ");
scanf("%d",&n);
for (i=1;i<=n;++i)
{
printf("\nplease enter roll of the student ");
scanf("%d",&stud[i].roll);
stud[i].name = (char *) malloc(19);
printf("\nplease enter name of the student roll number[%d] ",stud[i].roll);
scanf(" %s",stud[i].name);
printf("\nplease enter marks of CS-201 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs201);
printf("please enter marks of CS-202 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs202);
printf("please enter marks of CS-203 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs203);
printf("please enter marks of CS-204 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs204);
printf("please enter marks of CS-205 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs205);
printf("please enter marks of CS-206 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs206);
printf("please enter marks of CS-207 for the student roll[%d] ",stud[i].roll);
scanf("%d",&stud[i].cs207);
printf("\n\tInput is complete for the current student\n");
stud[i].tmarks=stud[i].cs201+stud[i].cs202+stud[i].cs203+stud[i].cs204+stud[i].cs205+stud[i].cs206+stud[i].cs207;
} for (i=1;i<n;++i)
{ for (j=i+1;j<=n;++j)
{ if (stud[i].tmarks<stud[j].tmarks)
{
emp = (char *) malloc(19);
emp=stud[i].name;
stud[i].name=stud[j].name;
stud[j].name=emp;
tmp=stud[i].roll;
stud[i].roll=stud[j].roll;
stud[j].roll=tmp;
rmp=stud[i].tmarks;
stud[i].tmarks=stud[j].tmarks;
stud[j].tmarks=rmp;
}
}
}
printf("\n NOW DISPLAYING THE RESULTS\n") ;
printf(" ==========================\n") ;
printf(" NAME ROLL TOTAL_MARKS\n");
for (i=1;i<=n;++i)
{ printf("\n %s %d %d",stud[i].name,stud[i].roll,stud[i].tmarks);
} free(emp);
printf("\n\n\n\tPress any key to continue...");
getch();
clrscr();
printf("\n THIS PROGRAM IS WRITTEN BY: ");
printf("\n\n\t\t MD. HASIBUR RASHID (HASIB)");
printf("\n\t\t\t\t MCSE, A+");
printf("\n\t B.Sc. in COMPUTER SCIENCE 3RD YEAR");
printf("\n\n\t ADDRESS: ");
printf("\n\t\t 172/A, RD.-4,");
printf("\n\t\t MOHAMMADI HOUSING LTD.");
printf("\n\t\t MOHAMMADPUR, DHAKA-1207");
printf("\n\t\t BANGLADESH");
printf("\n\t\t email: [email protected]\n\t\tWeb:
http://bool.cjb.net\n");
getch();
}
SAMPLE 3 (SUM OF POSITIVE MUMBERS)
#include<stdio.h>
#include<conio.h>
void main()
{ int e=0;
float avg;
float a[100],b[100];
int i=1;
int j=0;
float sum=0;
int total,n;
printf(" How many no. ");
scanf("%d",&n);
while (i<=n)
{
printf(" Enter the Positive number[%d] ",i);
scanf("%f",&a[i]);
if (a[i]>=0)
{ b[++j]=a[i];
sum=sum+b[j];
i++;
}
else e++;
}
/*total=j; */
avg=sum/n;
if (e>0)
{ printf("\n You have entered %d Negative Numbers \n",e);
printf(" So you had to enter %d numbers again",e);
}
printf("\n\n\tThe sum is: %6.4f === The Average is: %6.4f",sum,avg);
printf("\n\n Press any key to continue...");
getch();
clrscr();
printf("\n THIS PROGRAM IS WRITTEN BY: ");
printf("\n\n\t\t MD. HASIBUR RASHID (HASIB)");
printf("\n\t\t\t\t MCSE, A+");
printf("\n B.Sc.(Hon's) in COMPUTER SCIENCE, 3RD YEAR");
printf("\n\n\t ADDRESS: ");
printf("\n\t\t 172/A, RD.-4,");
printf("\n\t\t Mohammadi Housing Ltd.");
printf("\n\t\t Mohammadpur, Dhaka-1207");
printf("\n\t\t Bangladesh.");
printf("\n\t\t email: [email protected]\n\t\tWeb:
http://bool.cjb.net\n");
getch();
}