Wednesday, February 2, 2011

c-programming

/* COSINE SERIES */

#include
#include
#include
#define pi 3.14

void main()
{
int n,i,j;
float x,x1,sum,p,f,k;
clrscr();
printf("\n\tCOSINE SERIES");
printf("\n\t~~~~~~~~~~~~~~~");
printf("\nEnter the value of n:");
scanf("%d",&n);
printf("\nEnter the value for x:");
scanf("%f",&x);
x1=x*(pi/180);
sum=1;

for(i=2;i<=n*2;i+=2)
{
p=pow(x1,i);
f=1;

for(j=1;j{
f=f*j;
}
k=1;
k=k*(-1);
sum=sum+(p/f)*k;
}
printf("\n The sum of series %f",sum);
printf("\n The cosine series:%f",cos(x1));
getch();
}









OUTPUT:

COSINE SERIES
~~~~~~~~~~~~~~
Enter the value of n: 2

Enter the value for x: 4

The sum of series: 0.995127
The cosine series: 0.997567



/* EXPONENTIAL SERIES*/
#include
#include
#include
#define pi 3.14

void main()
{
int n,i,j;
float x,sum,p,f;
clrscr();
printf("\n\tEXPONENTIAL SERIES");
printf("\n\t~~~~~~~~~~~~~~~~~~");
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the value for x:");
scanf("%f",&x);
x=x*(pi/180);
sum=1;

for(i=1;i<=n;i++)
{
p=1;
f=1;

for(j=1;j<=i;j++)
{
p=p*x;
f=f*j;
}
sum=sum+(p/f);
}
printf("\n The sum of series: %f",sum);
printf("\n The cosine series: %f",exp(x));
getch();
}

OUTPUT:

EXPONENTIAL SERIES
~~~~~~~~~~~~~~~~~~~~~~
Enter the value of n: 4

Enter the value for x: 6

The sum of series: 1.110340
The cosine series: 1.110340


/* SINE SERIES*/


#include
#include
#include

void main()
{
int n,i,j,k,f;
float x,x1,sum,p;
clrscr();
printf("\n\t SINE SERIES\n");
printf("\n\t~~~~~~~~~~~~~~~");
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the value of x:");
scanf("%f",&x);
x1=x*(3.14/180);
sum=x1;
for(i=3;i<=n*2;i+=2)
{
p=pow(x1,i);
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
k=-1;
k=k*(-1);
sum=sum+(p/f)*k;
}
printf("\n Sum of the series:%f",sum);
printf("\n Sine series value:%f",sin(x1));
getch();
}

OUTPUT:

SINE SERIES
~~~~~~~~~~~
Enter the value of n:2

Enter the value of x:3

Sum of the series:0.052357

Sine series value:0.052309


/* TOWERS OF HANOI */

#include
#include

void transfer(int t,char,char,char);
void main()
{
int n;
clrscr();
printf("\n\n\t\t TOWERS OF HANOI");
printf("\n\n\t\t ~~~~~~~~~~~~~~~~");
printf("\n\n Enter the no of disk:");
scanf("%d",&n);
transfer(n,'l','r','c');
getch();
}

void transfer(int n,char from,char to,char temp)
{
if(n>0)
{
transfer(n-1,from,temp,to);
printf("\n Move disk %d From %c To %c",n,from,to);
transfer(n-1,temp,to,from);
}
}
OUTPUT:

TOWERS OF HANOI

~~~~~~~~~~~~~~~~

Enter the no of disk:4

Move disk 1 From l To c
Move disk 2 From l To r
Move disk 1 From c To r
Move disk 3 From l To c
Move disk 1 From r To l
Move disk 2 From r To c
Move disk 1 From l To c
Move disk 4 From l To r
Move disk 1 From c To r
Move disk 2 From c To l
Move disk 1 From r To l
Move disk 3 From c To r
Move disk 1 From l To c
Move disk 2 From l To r
Move disk 1 From c To r





/* FLOYD’S TRIANGLE */



#include
#include

void main()
{
int i,j,rows;
clrscr();
printf("\n\t\tFLOYD'S TRIANGLE");
printf("\n\t\t~~~~~~~~~~~~~~~~");
printf("\nEnter the rows:");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}


OUTPUT:

FLOYD'S TRIANGLE


Enter the rows: 5
*
**
***
****
*****




/* PASCAL TRIANGLE*/
#include
#include

void main()
{
int a[10][10],r,i,j,t;
clrscr();
printf("\n\t\t PASCAL TRIANGLE");
printf("\n\t\t ~~~~~~~~~~~~~~~~");
printf("\n Enter the row values:");
scanf("%d",&r);

for(i=1;i<=r;i++)
{
a[i][i]=1;
a[i][1]=1;
}
i=3;
while(i<=r)
{
t=2;
while(t{
a[i][t]=a[i-1][t-1]+a[i-1][t];
t++;
}
i++;
}
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n\n");
}
getch();
}







OUTPUT:

PASCAL TRIANGLE
~~~~~~~~~~~~~~~~
Enter the row values: 5
1

1 1

1 2 1

1 3 3 1

1 4 6 4 1


/* FIBONNACI SERIES */


#include
#include

void main()
{
int i,a=0,b=1,t=0,n;
clrscr();
printf("\n\t\tFIBONNACI SERIES");
printf("\n\t\t~~~~~~~~~~~~~~~~");
printf("\nEnter the number of terms:");
scanf("%d",&n);
printf("%d\t%d",a,b);

for(i=2;i{
t=a+b;
printf("\t%d",t);
a=b;
b=t;
}
getch();
}

OUTPUT:


FIBONNACI SERIES
~~~~~~~~~~~~~~~~

Enter the number of terms:8


0 1 1 2 3 5 8 13


/* FIBONNACCI USING RECURSION */

# include
# include

void main()
{
void fib(int);
int n;
clrscr();
printf("\n\t\tFIBONNACCI USING RECURSION");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\nEnter the number of terms : ");
scanf("%d",&n);
fib(n);
getch();
}

void fib(int d)
{
int i,t,a=0,b=1;
if(d==1)
{
printf("%d",a); }

else if (d==2)
{
printf("%d \t %d",a,b);
}
else
{
printf("%d \t %d",a,b);
for(i=2;i{
t=a+b;
printf("\t%d",t);
a=b;
b=t;
}
}
}






OUTPUT:

FIBONNACCI USING RECURSION
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter the number of terms: 6

0 1 1 2 3 5




/* FACTORIAL USING RECURSION */

#include
#include

void main()
{
int n,f;
int fact(int);
clrscr();
printf("\n\t\t FACTORIAL USING RECURSION");
printf("\n\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n Enter the numbers:");
scanf("%d",&n);
f=fact(n);
printf("\n The factorial :%d",f);
getch();
}

int fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}



FACTORIAL USING RECURSION
Enter the numbers: 5

The factorial: 120







/* ARMSTRONG NUMBER */

#include
#include
#include

void main()
{
int s,x,i,n;
clrscr();
printf("\n\t\tARMSTRONG NUMBER");
printf("\n\t\t~~~~~~~~~~~~~~~~");
printf("Armstrong no's are\n");

for(i=100;i<=1000;i++)
{
s=0;
x=i;

while(x>0)
{
n=x%10;
x=x/10;
s+=pow(n,3);
}

if(i==s)
printf("%d\n\n",i);
}
getch();
}


OUTPUT:



ARMSTRONG NUMBER

Armstrong no's are:

153

370

371

407


/* PRIME NUMBER GENERATION */



#include
#include
void main()
{
int a,b,c=0,r;
clrscr();
printf("\n\t\tPRIME NUMBER");
printf("\n\t\t~~~~~~~~~~~~~");
printf("\nEnter range:");
scanf("%d",&r);
a=1;
while(a<=r)
{
b=1;c=0;
while(b<=a)
{
if(a%b==0)
c=c+1;
b++;
}
if(c==2)
printf("\n %d is a prime number",a);
a++;
}
getch();
}


OUTPUT:



PRIME NUMBER
~~~~~~~~~~~~~
Enter range: 50

2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number






/*FINDING LENGTH OF THE STRING*/

#include
#include

void main()
{
char str[50];
int k;
clrscr();
printf("\n\t FINDING LENGTH OF THE STRING");
printf("\n\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n Enter the string:");
gets(str);
k=0;
while(str[k]!='\0')
k++;
printf("\n The length is %d",k);
getch();
}

OUTPUT:
FINDING LENGTH OF THE STRING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Enter the string: rajkumar

The length is : 8




/* CONCATENATING TWO STRING */

#include
#include

void main()
{
char s[30],s1[30],s2[30];
int i,j;
clrscr();
printf("\n\t CONCATENATING TWO STRING");
printf("\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("enter the first string");
scanf("%s",s);
printf("enter the second string");
scanf("%s",s1);
for(i=0;s[i]!='\0';i++)
s2[i]=s[i];
for(j=0;s1[j]!='\0';j++)
s2[i++]=s1[j];
s2[i]='\0';
printf("the concatenated string is %s",s2);
getch();
}






OUTPUT:



CONCATENATING TWO STRING

Enter the first string: RAJ.

Enter the second string: KUMAR

The concatenated string is: RAJKUMAR


/* REVERSING THE STRING */
#include
#include
#include

void main()
{
int n,i;
char a[20];
clrscr();
printf("\n\t\tREVERSING THE STRING");
printf("\n\t\t--------------------");
printf("\n\t\tINPUT");
printf("\n\t\t-----");
printf("\nEnter the string:");
scanf("%s",a);
n=strlen(a);
printf("\n\t\tOUTPUT");
printf("\nThe result is:");

for(i=n;i>=0;i--)
{
printf("%c",a[i]);
}
getch();
}


OUTPUT:


REVERSING THE STRING
--------------------
INPUT
Enter the string:RAJKUMAR
OUTPUT
The result is: RAMUKJAR





/* REPLACING STRING*/

#include
#include
#include

void main()
{
int slen,sublen,newlen,reslen,m,k,i,j,temp;
char str[40],substr[20],newstr[20],resstr[25];
clrscr();
printf("\n\t\t STRING REPLACING");
printf("\n\t\t ~~~~~~~~~~~~~~~~~");
printf("\n Enter any string:");
gets(str);
slen=strlen(str);
printf("\n Enter the substring:");
gets(substr);
sublen=strlen(substr);
printf("\n Enter the replacement string:");
gets(resstr);
reslen=strlen(resstr);
j=0;
k=0;
for(i=0;i{
newstr[k]=str[i];
k++;
if(str[i]==substr[j])
j++;
else
j=0;
if(j==sublen)
{
k=k-sublen;
j=0;
for(m=0;m{
newstr[k]=resstr[m];
k++;
}
}
}
newstr[k]='\0';
printf("\n The replaced string is %s",newstr);
getch();
}


OUTPUT:

STRING REPLACING
~~~~~~~~~~~~~~~~~
Enter any string: rajkumar

Enter the substring: j

Enter the replacement string: m

The replaced string is: ramkumar


/* STUDENTS MARKSHEET USING STRUCTURES */

#include
#include

struct stu
{
int rn,grade,a[5];
float avg;
}s[2];

void main()
{
int i,j,sum,n;
float avg;
clrscr();
printf("\t STUDENT MARKSHEET USING STRUCTURES\n\n");
printf("Enter the no of students");
scanf("%d",&n);

for(i=0;i{
scanf("%d",s[i].rn);

for(j=0;j<=5;j++)
{
scanf("%d",&s[i].a[j]);
}
}

for(i=0;i{
sum=0;

for(j=0;j<5 data-blogger-escaped-br="br" data-blogger-escaped-j="j">{
sum=sum+s[i].a[j];
s[i].avg=(float)(sum/5);
if(s[i].avg>=60)
s[i].grade=1;
else if(s[i].avg>50&&s[i].avg<60 data-blogger-escaped-br="br"> s[i].grade=2;
else
s[i].grade=3;
}
}

printf("****************************************\n");
printf("rn\ts1\ts2\ts3\ts4\ts5\tavg\t grade\n");
printf("*****************************************\n");

for(i=0;i{
printf("%d\t",s[i].rn);

for(j=0;j<5 data-blogger-escaped-br="br" data-blogger-escaped-j="j">{
printf("%d\t",s[i].a[j]);
}
printf("%f\t%d\n",s[i].avg,s[i].grade);
}
getch();
}




OUTPUT:
STUDENTS MARKSHEET USING STRUCTURES

Enter the no of students: 5
1
89 90 90 99 82

2
67 73 77 60 88

3
56 54 59 76 66

4
45 44 56 34 65

5
56 76 90 85 64

*********************************************************
Rn s1 s2 s3 s4 s5 avg grade
*********************************************************
1 89 90 90 99 82 90.000000 1
2 67 73 77 60 88 71.000000 1
3 56 54 53 76 66 62.000000 1
4 45 44 56 34 65 48.000000 3
5 56 76 90 35 64 74.000000 1


/* BANK OPERATION USING FUNCTIONS*/


#include
#include
int amt=10000,b,w,d,n;
void deposit()
{
printf("Enter amount\n");
scanf("%d",&d);
amt=amt+d;
printf("Your amount is:%d\n",amt);
}

void withdraw()
{
printf("Enter amount\n");
scanf("%d",&w);
if(amt>=100)
{
amt=amt-w;
printf("Your amt is:%d\n",amt);
}
else
printf("You have below 100 Rs\n");
}

void balance()
{
printf("The balance amount is :%d\n",amt);
}


void main()
{
int ba;
clrscr();
printf("\t\t BANKING OPERATION USING FUNCTION \n\n");
printf("1.Deposit\n2.Withdraw\n3.Balance\n4.Exit\n");
do
{

printf("\n Enter any choice:");
scanf("%d",&ba);
switch(ba)
{
case 1:
deposit();
break;
case 2:
withdraw();
break;
case 3:
balance();
break;
case 4:
getch();
break;
}
}while(ba!=4);
getch();
}



OUTPUT:


BANKING OPERATION USING FUNCTION


1. Deposit
2. Withdraw
3. Balance
4. Exit

Enter any choice: 1
Enter amount:
1200
Your amount is: 11200

Enter any choice: 2
Enter amount:
11200
Your amt is: 0

Enter any choice: 2
Enter amount:
1000
You have below 100 rs

Enter any choice: 1
Enter amount:
15000
Your amount is: 15000

Enter any choice: 3
The balance amount is: 15000

Enter any choice: 4


/*BOOK SEARCHING AND SORTING*/


#include
#include
#include
void newentry();
void sort();
void search();
void list();
struct books
{
char authname[50];
char title[50];
}b[10];
int c=1;
void main()
{
int k,i,ch,n;
char w[30];
clrscr();
printf("\n\t\t\tBOOK OPERATION\n");
printf("\n\t\t\t--------------\n");
printf("\nEnter how many book you want to enter:");
scanf("%d",&n);
for(i=0;inewentry();

while(1)
{
clrscr();
printf("\nMain Menu");
printf("\n1.Newentry\n");
printf("\n2.Search");
printf("\n3.Sort");
printf("\n4.List");
printf("\n5.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
newentry();
break;
case 2:
search();
break;
case 3:
sort();
break;
case 4:
list();
break;
case 5:
exit();
break;
}
}
}


void search()
{
char tit[44],w[22],d,authname[44];
int i,a=0,z;
clrscr();
gets(w);
printf("\nDo you want to search using author name(Y/N)!");
scanf("%c",&d);

if(d=='y'||d=='n')
{
gets(w);
printf("\nEnter any author name you want to search:");
gets(authname);

for(i=0;i<=c;i++)
{
z=strcmp(authname,b[i].authname);
if(z==0)
{
printf("\n\nThe book have been found");
printf("\n\nThe book title is %s", b[i].title);
printf("\n\nThe book author name is : %s",b[i].authname);
a=1;
}
}
if(a==0)
printf("\n\nThe book was not found:");
}
getch();
}


void sort()
{
char temp[50],t_tit[44][44];
int i,j,z;
clrscr();
printf("\nThe Sorted book are");
for(i=0;i<=c;i++)
strcpy(t_tit[i],b[i].title);
for(i=0;i<=c;i++)
{
for(j=0;j<=c;j++)
{
z=strcmp(t_tit[i],t_tit[j]);





if(z>0)
{
strcpy(temp,t_tit[i]);
strcpy(t_tit[i],t_tit[j]);
strcpy(t_tit[j],temp);
}
}
}
for(i=0;i<=c;i++)
printf("%s\n",t_tit[i]);
getch();
}


void newentry()
{
char titles[20],author[50],was[22];
clrscr();
gets(was);
c++;
printf("Enter any author:");
gets(b[c].authname);
printf("Enter any title:");
gets(b[c].title);
printf("%s",b[c].title);
printf("\n%s",b[c].authname);
printf("\n%s",b[c].authname);
getch();
}


void list()
{
int i;
clrscr();
printf("AUTHOR NAME \t\tTITLES");
for(i=0;i<=c;i++)
printf("\n\n%s\t\t%s",b[i].authname,b[i].title);
getch();
}


OUTPUT


BOOK OPERATION


Enter how many book you want to enter: 2

Enter any author: R.Balaguruswamy

Enter any title: Programming in C

Programming in C
R.Balaguruswamy

Enter any author: Morris Mano

Enter any title: Computer System Architecture

Computer System Architecture
Morris Mano

Main Menu

1. New entry
2. Search
3. Sort
4. List
5. Exit

Enter your choice: 1

Enter any author: Problem Solving Techniques

Enter any title: R.G.Dromey


Problem Solving Techniques
R.G.Dromey


Main Menu

1. New entry
2. Search
3. Sort
4. List
5. Exit

Enter your choice: 2

Do you want to search using author name(Y/N)! Y

Enter any author name you want to search: R.Balaguruswamy

The book have been found

The book title is Programming in C

The book author name is: R.Balaguruswamy

Main Menu

1. New entry
2. Search
3. Sort
4. List
5. Exit

Enter your choice: 2

Do you want to search using author name(Y/N)! Y

Enter any author name you want to search: Richard hall

The book was not found

Main Menu

1. New entry
2. Search
3. Sort
4. List
5. Exit




Enter your choice: 3

The Sorted books are

Programming in C
Problem Solving Techniques
Computer System Architecture



Main Menu

1. New entry
2. Search
3. Sort
4. List
5. Exit

Enter your choice: 4


AUTHOR NAME TITLES
---------------------- ----------

R.Balaguruswamy Programming in C

Morris Mano Computer System Architecture

R.G.Dromey Problem Solving Techniques

Main Menu

1. New entry
2. Search
3. Sort
4. List
5. Exit

Enter your choice: 5




/* SALES REPORT */


#include
#include
#include

void main()
{
int sal[5][5],s,p,i,j,row,col,temp,per,avg;
char s_men[10][30],pr[10][30],waste[33];
clrscr();
printf("\n\t\t SALES REPORT");
printf("\n\t\t -------------");
printf("Enter how many salesman:");
scanf("%d",&s);
printf("Enter how many products:");
scanf("%d",&p);
clrscr();
gets(waste);
for(i=1;i<=s;i++)
{
printf("\n Enter the %d name of salesman:",i);
gets(s_men[i]);
}
for(j=1;j<=p;j++)
{
printf("\n Enter the %d name of products:",j);
gets(pr[j]);
}
for(i=1;i<=s;i++)
for(j=1;j<=p;j++)
sal[i][j]=0;
clrscr();
printf("\n\t The salesman and products data:");
while(1)
{
clrscr();
for(i=1;i<=s;i++)
printf("\n%d salesman :%s",i,s_men[i]);
for(i=1;i<=p;i++)
{
gotoxy(35,i+1);
printf("%d Products:%s",i,pr[i]);
}



gotoxy(5,10);
printf("\n\n Enter which salesman connected with which products");
printf("\n Enter the salesman no");
scanf("%d",&row);
printf("\n Enter the product no:");
scanf("%d",&col);
printf("\n Enter the sales rupees:");
scanf("%d",&per);
sal[row][col]=per;
printf("\n\n Do you want to continue (1 for yes,2 for no):\n");
scanf("%d",&temp);
if(temp==1)
continue;
else
break;
}
clrscr();
printf("\n\n The salesman and products report:\n\n");
gotoxy(8,5);
for(i=1;i<=p;i++)
printf("\t Products %d",i);
printf("\n");
for(i=1;i<=s;i++)
{
printf("Salesman %d",i);
for(j=1;j<=p;j++)
printf("\t%2d\t",sal[i][j]);
printf("\n\n");
}
printf("\n\t Total details:");
for(i=1;i<=s;i++)
{
avg=0;
for(j=1;j<=p;j++)
avg=avg+sal[i][j];
printf("\n The total rupees sold by salesman %d is %d",i,avg);
}
getch();
}







OUTPUT:
SALES REPORT
Enter how many salesmen:

Enter how many salesmen: 2
Enter how many products: 2

Enter the 1 name of salesman: vanu
Enter the 2 name of salesman: viki

Enter the 1 name of products: bat
Enter the 2 name of products: ball

1 salesman: vanu 1 Products: bat
2 salesmen: viki 2 Products: ball

Enter which salesman connected with which products:
Enter the salesman no: 1

Enter the product no: 1

Enter the sales rupees: 1000

Do you want to continue (1 for yes, 2 for no): 1

Enter which salesman connected with which products
Enter the salesman no: 2

Enter the product no: 2

Enter the sales rupees: 500

Do you want to continue (1 for yes, 2 for no): 2

The salesman and products report:

Products 1 Product 2
Salesman 1 1000 0

Salesman 2 0 500




Total details:

The total rupees sold by salesman 1 is 1000

The total rupees sold by salesman 2 is 500













0 comments:

Post a Comment