83
kala Experiment No. 1 Date: AIM: TO FIND SIMPLE INTEREST AND COMPOUND INTEREST ALGORITHM: Step 1: Start Step 2: Declare variables p,r,t,si,ci,amount as float. Step 3: Accept values for p,r,t,n. Step 4: Calculate si=p*t*r/100 Step 5: Calculate amount=p* pow((1+r/n*100),n*t) Step 6: Calculate ci=amount-p Step 7: Print Step 8: Stop C’ software lab - 1 - MCA’05

C lab-programs

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: C lab-programs

kala

Experiment No. 1

Date:

AIM: TO FIND SIMPLE INTEREST AND COMPOUND INTEREST

ALGORITHM:

Step 1: Start

Step 2: Declare variables p,r,t,si,ci,amount as float.

Step 3: Accept values for p,r,t,n.

Step 4: Calculate si=p*t*r/100

Step 5: Calculate amount=p* pow((1+r/n*100),n*t)

Step 6: Calculate ci=amount-p

Step 7: Print

Step 8: Stop

C’ software lab - 1 - MCA’05

Page 2: C lab-programs

/*Simple interest and compound interest*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

float p,r,t,si,ci,amount;

int n;

clrscr();

printf("\nEnter principle :");

scanf("%f",&p);

printf("\nEnter rate :");

scanf("%f",&r);

printf("\nEnter time :");

scanf("%f",&t);

printf("\nEnter compoundings per year :");

scanf("%d",&n);

si=(p*t*r)/100;

printf("\nSimple Interest =%7.2f",si);

amount=p*pow((double)(1+r/(n*100)),(double)(n*t));

ci=amount-p;

printf("\nCompond Interest=%7.2f\n",ci);

getch();

}

C’ software lab - 2 - MCA’05

Page 3: C lab-programs

OUTPUT :

Enter principle :10000

Enter rate :8

Enter time :4

Enter compoundings per year :2

Simple Interest =3200.00

Compond Interest=3685.69

C’ software lab - 3 - MCA’05

Page 4: C lab-programs

Experiment No. 2Date:

AIM : TO FIND THE AMSTRONG NUMBER AMONG THE GIVEN NUMBERS

ALGORITHM :

Step1: Start

Step2: Accept a Number

Step3: Assign the number to another variable

Step4: Check whether the no is greater than Zero go to step 8

Step5: Extract the last digit and add the cube to a sum variable

Step6: Divde the number by 10

Step7: Goto step 4

Step8: Check whether the sum = temporary variable and print result

Step9: Stop

C’ software lab - 4 - MCA’05

Page 5: C lab-programs

/*Armstrong number*/

#include<stdio.h>

#include<conio.h>

void main()

{

int num,arms=0,tmp,mod;

clrscr();

printf("\n Enter the number :");

scanf("%d",&num);

tmp=num;

while(num>0)

{

mod=num%10;

arms+=mod*mod*mod;

num=num/10;

}

if(tmp==arms)

printf("\n Number %d is armstrong",arms);

else

printf("\n Number %d is not armstrong",tmp);

getch();

}

C’ software lab - 5 - MCA’05

Page 6: C lab-programs

OUTPUT :

1. Enter the number :153

Number 153 is armstrong

2. Enter the number :111

Number 111 is not armstrong

C’ software lab - 6 - MCA’05

Page 7: C lab-programs

Experiment No. 3Date:

AIM : TO FIND THE ROOTS OF A QUADRATIC EQUATION

ALGORITHM :

Step1: Start

Step2: Accept values for the variables x2 x and constant

Step3: Apply the formula b2-4ac (b=cof x2,a=cof x,c=constant) as save to a variable

Step4: Check whether the value is less than 0 then print roots are imaginary

Step5: If value=0 then print the roots are equal –b/2a

Step6: If value greater than 0, 1st root is (-b+sqrt(value))/(2*a),

2nd root is (-b-sqrt(value))/(2*a).

Step7: Print the result

Step8: Stop

C’ software lab - 7 - MCA’05

Page 8: C lab-programs

/*Quadratic Equation*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

float a,b,c,d,r1,r2;

clrscr();

printf("\n Enter the Coeff. of the Eq. :");

scanf("%f%f%f",&a,&b,&c);

d=(b*b)-(4*a*c);

if(d==0)

{

printf("\n Roots are REAL and EQUAL");

r1=-b/(2*a);

printf("\n Root Is : %.2f",r1);

}

else if (d>0)

{

printf("\n Roots are Real and Distinct");

r1=(-b+sqrt(d))/(2*a);

r2=(-b-sqrt(d))/(2*a);

printf("\n Root 1 : %.2f ..... Root 2 : %.2f",r1,r2);

}

else

{

printf("\n Roots are imaginary");

}

getch();

}

C’ software lab - 8 - MCA’05

Page 9: C lab-programs

OUTPUT :

1.Enter the Coeff. of the Eq. :1 -5 6

Roots are Real and Distinct

Root 1 : 3.00 ..... Root 2 : 2.00

2.Enter the Coeff. of the Eq. :4 4 1

Roots are REAL and EQUAL

Root Is : -0.50

C’ software lab - 9 - MCA’05

Page 10: C lab-programs

Experiment No. 4Date:

AIM : TO PERFORM MATRICS ADDITION, MULTIPLICATION. TRANSPOSE.

ALGORITHM :

Step 1: Start

Step 2: Declare three integer 2 dimensional array of order 3*3 .

Step 3: Accept a choice from user

Step 4: If choice = 1 : goto Step 9

Step 5: If choice = 2 : goto Step 10

Step 6: If choice = 3 : goto step 11

Step 7: else if choice = 4 : goto Step 12

Step 8: Accept the elements of the two 2 D arrays

Step 9: If choice = 1

a. Add the elements that belong corresponding cells in the arrays and store in

third array’s respective cell.

b. Display the result.

Step 10 : If choice = 2

a. Multiply the elements of the first two matrices and store the result in third

array.

b. Display the result.

Step 11 : If choice = 3

a. Accept the elements of the matrics one.

b. Display the matrics in the order column*row format.

Step 13 : Repeat step 3 to 11.

Step 14 : Stop.

C’ software lab - 10 - MCA’05

Page 11: C lab-programs

/*Matrics Addition, Multiplication, Transpose*/

#include<stdio.h>

#include<conio.h>

int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j,k,ch;

void add();

void mul();

void tran();

void accept(int [][10],int,int);

void display(int [][10],int,int);

void main()

{

clrscr();

do

{

clrscr();

printf("\n Matrix Manipulator");

printf("\n 1.Addition");

printf("\n 2.Multiplication");

printf("\n 3.Transpose");

printf("\n press any other key to exit...");

printf("\n Enter U'r choice :");

scanf("%d",&ch);

if (ch>=1&&ch<=3)

{

printf("\n Enter order of the matrices :");

printf("\n Mat 1 :");

scanf("%d%d",&r1,&c1);

printf("\n Mat 2 :");

scanf("%d%d",&r2,&c2);

}

switch(ch)

{

C’ software lab - 11 - MCA’05

Page 12: C lab-programs

case 1:

if((r1==r2)&&(c1==c2))

add();

else

printf("\n addition not possible...");

getch();

break;

case 2:

if(c1==r2)

mul();

else

printf("\n multiplication not possible...");

getch();

break;

case 3: tran();getch();break;

default: printf("\n Wrong choice Exit...");

}

}while(ch>0&&ch<4);

getch();

}

void add()

{

printf("\n Matrics addition");

printf("\n Enter Matrics 1:");

accept(a,r1,c1);

printf("\n Enter Matrics 2:");

accept(b,r2,c2);

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

c[i][j]= a[i][j]+b[i][j];

printf("\n Matrics A :\n");

C’ software lab - 12 - MCA’05

Page 13: C lab-programs

display(a,r1,c1);

printf("\n Matrics B :\n");

display(b,r2,c2);

printf("\n Matrics C :\n");

display(c,r1,c2);

}

void mul()

{

printf("\n Matrics multiplication");

printf("\n Enter Matrics 1:");

accept(a,r1,c1);

printf("\n Enter Matrics 2:");

accept(b,r2,c2);

for(i=0;i<r1;i++)

for(j=0;j<c2;j++)

{

c[i][j]=0;

for(k=0;k<c1;k++)

c[i][j] += a[i][k]*b[k][j];

}

printf("\n Matrics A :\n");

display(a,r1,c1);

printf("\n Matrics B :\n");

display(b,r2,c2);

printf("\n Matrics C :\n");

display(c,r1,c2);

}

void tran()

{

printf("\n Matrics Transpose");

C’ software lab - 13 - MCA’05

Page 14: C lab-programs

printf("\n Enter Matrics :\n");

accept(a,r1,c1);

display(a,r1,c1);

printf("\n Transposed Matrics :\n");

for(i=0;i<r1;i++)

{

for(j=0;j<c1;j++)

{

printf("%d\t",a[j][i]);

}

printf("\n");

}

}

void display(int x[][10],int m,int n)

{

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

printf("%d\t",x[i][j]);

}

printf("\n");

}

}

void accept(int x[][10],int m,int n)

{

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

C’ software lab - 14 - MCA’05

Page 15: C lab-programs

scanf("%d",&x[i][j]);

}

}

}

C’ software lab - 15 - MCA’05

Page 16: C lab-programs

OUTPUT :

Matrix Manipulator

1.Addition

2.Multiplication

3.Transpose

press any other key to exit...

Enter U'r choice :3

Enter order of the matrices :

Mat 1 :3 3

Mat 2 :2 2

Matrics Transpose

Enter Matrics :

1 2 3

2 3 4

3 4 5

1 2 3

2 3 4

3 4 5

Transposed Matrics :

1 2 3

2 3 4

3 4 5

C’ software lab - 16 - MCA’05

Page 17: C lab-programs

Experiment No. 5Date:

AIM : TO FIND OUT REVERSE OF A GIVEN NUMBER

ALGORITHM :

Step 1: Start

Step 2: Declare variables n, dig, rev=0 as integer.

Step 3: Input the number, n.

Step 4: If n>0 then goto step 5 else goto step 8

Step 5: Modular division is done on the number with 10 .The value obtained

is assigned to variable

Step 6: The variable, rev is multiplied by 10 and then added with the variable dig,

thisvalue is assigned to rev.

Step 7: The variable, n is divided by 10 and the result is stored in n. Goto step 4

Step 8: The result is displayed on the screen

Step 9: Stop

C’ software lab - 17 - MCA’05

Page 18: C lab-programs

/*Reverse of a Number*/

#include<stdio.h>

#include<conio.h>

void main()

{

int num,rnum=0,mod;

clrscr();

printf("\n Enter the Number :");

scanf("%d",&num);

while(num>0)

{

mod=num%10;

rnum=rnum*10+mod;

num=num/10;

}

printf("\n reverse of number is %d",rnum);

getch();

}

C’ software lab - 18 - MCA’05

Page 19: C lab-programs

OUTPUT :

Enter the Number :1234

reverse of number is 4321

C’ software lab - 19 - MCA’05

Page 20: C lab-programs

Experiment No. 6 Date:

AIM : PERFORM SORTING A ONE DIMENSIONAL

ALGORITHM :

Step 1 : Start

Step 2 : Read the limit, N

Step 3 : Read elements of A[N] using for loop

Step 4 : Using for loop check whether A[I]>A[J] if yes then proceed

Step 5 : tA[I]

Step 6 : A[I]A[J]

Step 7 : A[J]t

Step 8 : End If

Step 9 : End for

Step 10 : Print Sorted array A[N]

Step 11 : Stop

C’ software lab - 20 - MCA’05

Page 21: C lab-programs

/*Sorting a 1-D array*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[15],i,tmp,j,k;

clrscr();

printf("\n Enter the Size :");

scanf("%i",&i);

printf("\n Enter the elements in the array : \n");

for(j=0;j<i;j++)

{

scanf("%d",&a[j]);

}

printf("\n Entered Elements are : \n");

for(j=0;j<i;j++)

{

printf("%d\t",a[j]);

}

for(j=0;j<i;j++)

{

for(k=j;k<i;k++)

{

if(a[j]>a[k])

{

tmp = a[j];

a[j]= a[k];

a[k]=tmp;

}

}

}

printf("\n Sorted Elements are : \n");

C’ software lab - 21 - MCA’05

Page 22: C lab-programs

for(j=0;j<i;j++)

{

printf("%d\t",a[j]);

}

getch();

}

C’ software lab - 22 - MCA’05

Page 23: C lab-programs

OUTPUT :

Enter the Size :8

Enter the elements in the array :

3 7 5 9 7 4 3

8

Entered Elements are :

3 7 5 9 7 4 3 8

Sorted Elements are :

3 3 4 5 7 7 8 9

C’ software lab - 23 - MCA’05

Page 24: C lab-programs

Experiment No. 7Date:

AIM : TO PROGRAM FOR SORT THE ENTERED STRINGS IN ALPHABETIC

ORDER.

ALGORITHM :

Step 1: Start

Step 2: Accept a single dimensional array, also declare another temporary array

Step 3: Sort the elements of the vector in ascending order

Step 4: Extract numbers one by one from main vector and check whether

the number is equal to the previous number if no’s are different insert it into

the

temporary array.

step 5: Print the temporary Array to see the result

step 6: Stop

C’ software lab - 24 - MCA’05

Page 25: C lab-programs

/*Deleting Duplicates in a 1-D array*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[15],i,tmp,j,k;

clrscr();

printf("\n Enter the Size :");

scanf("%i",&i);

printf("\n Enter the elements in the array : \n");

for(j=0;j<i;j++)

{

scanf("%d",&a[j]);

}

printf("\n Entered Elements are : \n");

for(j=0;j<i;j++)

{

printf("%d\t",a[j]);

}

for(j=0;j<i;j++)

{

for(k=j;k<i;k++)

{

if(a[j]>a[k])

{

tmp = a[j];

a[j]= a[k];

a[k]=tmp;

}

}

}

for(j=0;j<i;j++)

C’ software lab - 25 - MCA’05

Page 26: C lab-programs

{

if(a[j]==a[j+1])

{

for(k=j;k<i;k++)

{

a[k]=a[k+1];

}

i--;j--;

}

}

printf("\n Sorted Elements are : \n");

for(j=0;j<i;j++)

{

printf("%d\t",a[j]);

}

getch();

}

C’ software lab - 26 - MCA’05

Page 27: C lab-programs

OUTPUT :

Enter the Size :8

Enter the elements in the array :

6

5

8

4

2

6

8

5

Entered Elements are :

6 5 8 4 2 6 8 5

Sorted Elements are :

2 4 5 6 8

C’ software lab - 27 - MCA’05

Page 28: C lab-programs

Experiment No. 8Date:

AIM : WRITE A PROGRAM TO PRINT FIBONACCI SERIES.

ALGORITHM :

Step 1: Start

Step 2: Declare variables n,a=0,b=1,c,i=1 as integer.

Step 3: Input value of n.

Step 4: Value of n is decremented by 1.

Step 5: The sum of a and b is assigned to c.

Step 6: The value c is displayed.

Step 7: The value of b is assigned to a and value of c is assigned to b.

Step 8: value of i is incremented by 1

Step 9: If i<n then goto step 6 Else goto step 10.

Step 10: Stop.

C’ software lab - 28 - MCA’05

Page 29: C lab-programs

/*Fibonacci Series*/

#include<stdio.h>

#include<conio.h>

void main()

{

int f=1,s=0,t,r,i;

clrscr();

printf("\n Enter the range :");

scanf("%d",&t);

printf("\n Fibonacci Series...in %d numbers is...",t);

printf("\n%d ",s);

for(i=0;i<t-1;i++)

{

r=f+s;

f=s;

s=r;

printf(" %d ",r);

}

getch();

}

C’ software lab - 29 - MCA’05

Page 30: C lab-programs

OUTPUT :

Enter the range :8

Fibonacci Series...in 8 numbers is...

0 1 1 2 3 5 8 13

C’ software lab - 30 - MCA’05

Page 31: C lab-programs

Experiment No. 9Date :

AIM : TO PERFORM THE FUNCTIONS OF A CALCULATOR USING SWITCH

CASE AND FUNCTIONS.

ALGORITHM :

Step 1 : Start

Step 2 : Read choice 1.Add 2.Sub 3.Mul 4.Div.

Step 3 : If choice =1 then add the numbers

Step 4 : If choice =2 then subtract the numbers.

Step 5 : If choice = 3 then multiply the numbers.

Step 6 : If choice = 4 then divide the numbers

Step 7 : Display respective output.

Step 8 : Stop

C’ software lab - 31 - MCA’05

Page 32: C lab-programs

/*Calculator*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

int a,b;

int ch;

do

{

clrscr();

printf("\n\n\n\n\n\n\n");

printf("\t\t\t\tCALCULATOR\n\n\n\n");

printf("\t\t\t1.Addition\n");

printf("\t\t\t2.Substraction\n");

printf("\t\t\t3.Multiplication\n");

printf("\t\t\t4.Division\n");

printf("\t\t\t5.Exit");

printf("\n\n\n\t\t\tEnter U'r choice :");

scanf("%d",&ch);

if(ch<5)

{ printf("\n\t\t\tEnter two values:");

scanf("%d %d",&a,&b);

}

else exit(0);

switch(ch)

{

case 1:

printf("\t\t\tThe sum is :%d",a+b); getch();break;

case 2:

printf("\t\t\tThe diference is :%d",a-b);getch();break;

case 3:

C’ software lab - 32 - MCA’05

Page 33: C lab-programs

printf("\t\t\tThe multiplication is :%d",a*b);getch();break;

case 4:

if(b==0) printf("\t\t\tDivide by zero error");

else

printf("\t\t\tTHe division is :%d",a/b);getch();

break;

case 5: exit(0);

}

}while(ch<6);

getch();

}

C’ software lab - 33 - MCA’05

Page 34: C lab-programs

OUTPUT :

CALCULATOR

1.Addition

2.Substraction

3.Multiplication

4.Division

5.Exit

Enter U'r choice :1

Enter two values:1 18

The sum is :19

CALCULATOR

1.Addition

2.Substraction

3.Multiplication

4.Division

5.Exit

Enter U'r choice :4

Enter two values:18 1

THe division is :18

C’ software lab - 34 - MCA’05

Page 35: C lab-programs

Experiment No.10

Date:

AIM : TO WRITE A PROGRAM TO CREATE AND MANIPULATE STUDENTS

RECORD USING STRUCTURES AND POINTERS.

ALGORITHM :

Step 1 : Start

Step 2 : Declare structure Stud

Roll_no as Integer

Name as Character array

Grade as Character

Mark, as an integer array

Step 3 : Create student object as an array as st[10], and also a pointer object *sd

Step 4 : Declare tot_mark, n

Step 5 : Accept number of entries as n

Step 6 : Accept student details

Step 7 : Calculate the average of students as sum of marks / 3

Step 8 : Calculate the grades and store to structure as on the conditions

if average > = 80 : grade = A

if average > = 60 and average < 80 : grade = B

if average > = 50 and average < 60 : grade = c

else if average < 50 : grade = Failed

Step 9 : Print details of students as per fields in the structure

Step 10 : Stop

C’ software lab - 35 - MCA’05

Page 36: C lab-programs

/*Student’s Record Using Poinetrs*/

#include<stdio.h>

#include<conio.h>

struct stud

{

int regno;

char name[20];

int mark[3];

char grade;

}s[10],*st;

void main()

{

int i,avg=0,j,num;

clrscr();

printf("\n Enter the number of students...");

scanf("%d",&num);

st = s;

for(i=0;i<num;i++)

{

printf("\n Enter student's Details :");

printf("\n Roll no :");

scanf("%d",&st->regno);

printf(" Name :");

scanf("%s",st->name);

for(j=0;j<3;j++)

{

printf(" mark %d :",j+1);

scanf("%d",&st->mark[j]);

avg+=st->mark[j];

}

printf("Average of %s is %d",st->name,avg=avg/3);

C’ software lab - 36 - MCA’05

Page 37: C lab-programs

if(avg>=80)

st->grade='A';

else if(avg>=60 && avg<80)

st->grade='B';

else if(avg>=50 && avg<60)

st->grade='C';

else

st->grade='F';

st++;

avg=0;

}

st=s;

printf("\n Student details...");

printf("\nReg_no\tName\t\tMarks\t1\t2\t3\tgrade");

for(i=0;i<num;i++)

{

printf("\n%d\t%s\t\t\t%d\t%d\t%d\t%c",st->regno,st->name,st->mark[0],st-

>mark[1],st->mark[2],st->grade);

st++;

}

getch();

}

C’ software lab - 37 - MCA’05

Page 38: C lab-programs

OUTPUT :

Enter the number of students...2

Enter student's Details :

Roll no :1

Name :Sachin

mark 1 :75

mark 2 :85

mark 3 :80

Average of Sachin is 80

Enter student's Details :

Roll no :2

Name :Rohit

mark 1 :80

mark 2 :65

mark 3 :82

Average of Rohit is 75

Student details...

Reg_no Name Marks 1 2 3 grade

1 Sachin 75 85 80 A

2 Rohit 80 65 82 B

C’ software lab - 38 - MCA’05

Page 39: C lab-programs

Experiment No. 11Date:

AIM : TO PERFORM, SORT A CHARACTER ARRAY IN ACSENDING ORDER

ALGORITHM :

Step 1: Start

Step 2: Accept a character array

Step 3: use a for loop to extract characters from the array and check it with each

characters of the array if value is greater interchange the value in the positon

Step 4: Display the result

Step 5: Stop

C’ software lab - 39 - MCA’05

Page 40: C lab-programs

/*Sorting a 1-D Charater array*/

#include<stdio.h>

#include<conio.h>

void main()

{

char str[20],tmp;

int len,i,j;

clrscr();

printf("\n Enter the Char. array...");

scanf("%s",str);

for(len=0;str[len]!='\0';len++);

printf("\n Entered Char.array is...%s",str);

printf("\n Length of entered string is...%d\n",len);

for(i=0;i<len;i++)

for(j=i;j<len;j++)

if(str[i]<str[j])

{

tmp=str[i];

str[i]=str[j];

str[j]=tmp;

}

printf("\n Sorted Char.array is...%s",str);

getch();

}

C’ software lab - 40 - MCA’05

Page 41: C lab-programs

OUTPUT :

Enter the Char. array...chinagate

Entered Char.array is...chinagate

Length of entered string is...9

Sorted Char.array is...tnihgecaa

C’ software lab - 41 - MCA’05

Page 42: C lab-programs

Experiment No. 12Date:

AIM : TO PROGRAM TO CHECK THE OCCURRENCE OF A PARTICULAR

NUMBER IN AN ARRAY.

ALGORITHM :

Step 1: Start

Step 2: Declare array a[25], variables n,i,num,count=0 as integer.

Step 3: Store the limit of the array in the variable n.

Step 4: Store the values of the array.

Step 5: Enter the value to be checked and store it in the variable, num.

Step 6: Start a for loop with loop variable, i.Compare each element of the array with

variable num. For each a[i]=num increment the counter count by 1.

Step 7: Display count on the output screen as the number of occurrence.

Step 8: Stop.

C’ software lab - 42 - MCA’05

Page 43: C lab-programs

/*occurance of particular number in an array*/

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,a[25],n,num,count=0,temp,flag=0,ch;

clrscr();

do

{

clrscr();

printf("\n\nEnter number of elements:");

scanf("%d",&n);

printf("\nEnter %d array elements:\n",n);

for(j=0;j<n;j++)

scanf("%d",&a[j]);

printf("\nEnter the number to be searched:");

scanf("%d",&num);

printf("\nThe positions of the given number are: ");

for(i=0;i<n;i++)

{

if(a[i]==num)

{

printf("%5d",i+1);

count++; flag=1;

}

}

if(flag==1)

{

printf("\n\n%d is repeated %d times in the array",num,count);

count = 0;

flag = 0;

}

C’ software lab - 43 - MCA’05

Page 44: C lab-programs

else

printf("0 \n\nNo element found");

printf("\n\n\n\n\n\t\tDo you want to continue?(Press 1 to continue):");

scanf("%d",&ch);

}while(ch==1);

getch();

}

C’ software lab - 44 - MCA’05

Page 45: C lab-programs

OUTPUT :

Enter number of elements:5

Enter 5 array elements:

3

5

4

3

6

Enter the number to be searched:3

The positions of the given number are: 1 4

3 is repeated 2 times in the array

Do you want to continue?(Press 1 to continue):1

Enter number of elements:3

Enter 3 array elements:

6

5

23

Enter the number to be searched:8

The positions of the given number are: 0

No element found

Do you want to continue?(Press 1 to continue):1

C’ software lab - 45 - MCA’05

Page 46: C lab-programs

Experiment No. 13

Date:

AIM : TO CHECK WHETHER A GIVEN STRING IS PALINDROME OR NOT

ALGORITHM :

Step 1: Start

Step 2: Initialize a string array a[100],flag=0

Step 3: Calculate length of the string using strlen() function

Step 4: Using for loop from I=0,J=len-1 to j<len/2 perform step 5

Step 5: Check whether A[I] != A[J] if false Set flag=1

Step 6: End for

Step 7: If flag=0 print Palindrome else Not Palindrome

Step 8: Stop

C’ software lab - 46 - MCA’05

Page 47: C lab-programs

/*palindrome*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char str[10],str1[10];

int i,n,j;

clrscr();

printf("Enter a string:");

scanf("%s",str);

n=strlen(str);

for(i=0,j=n-1;i<n/2;i++,j--)

str[i]=tolower(str[i]);

str[j]=tolower(str[j]);

if(str[i]!=str[j])

{

printf("\n String is Not a palindrome") ;

getch();

exit(0);

}

printf("\n String is a Palindrome");

getch();

}

C’ software lab - 47 - MCA’05

Page 48: C lab-programs

OUTPUT :

1. Enter a string:INDIAN

String is Not a palindrome

2. Enter a string:Malayalam

String is a Palindrome

C’ software lab - 48 - MCA’05

Page 49: C lab-programs

Experiment No.14

Date:

AIM : TO COUNT THE VOWELS, CONSONANTS AND DIGITS.

ALGORITHM :

Step 1: Start

Step 2: Read str, vowc,conc,I,dig

Step 3: vowc o, conc0,dig0

Step 4: Input the line

Step 5: Print the given line

Step 6: lenlength of the string.

Step 7: Using for loop perform the step 7 to step 10 until len.

Check whether the a[I] is any of a,e,i,o,u if yes

vowcvowc+1 else go to step 8

Step 8: check whether the a[I]is between A and Z.

if yes concconc+1

Step 9: check whether the a[I]is between 0 and 9

If yes digdig+1

Step 10: print the vowc, conc, dig.

Step 11: Stop

C’ software lab - 49 - MCA’05

Page 50: C lab-programs

/*count vowels*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

int i=0,j;

char a[100],c;

int chr=0,vow=0,dig=0,word=0,whites=0,others=0,cons=0;

clrscr();

printf("Enter a text: ");

scanf("%[^\n]",a);

while((c=tolower(a[i++]))!='\0')

{

chr++;

if(c== 'a' || c== 'e' || c== 'i'|| c=='o'|| c=='u') vow++;

else if(c >='a' && c<='z') ++cons;

else if(c>='0' && c<='9') ++dig;

else if(c==' ')

{

++word;

++whites;

while((a[i]==' ') || (a[i]=='\t'))

{ i++;

whites++;

}

}

}

word++;

printf("\n Entered String Consists of :\n");

printf("\nVowels =%d",vow);

C’ software lab - 50 - MCA’05

Page 51: C lab-programs

printf("\nconsonants =%d",cons);

printf("\ndigits =%d",dig);

printf("\nwords =%d",word);

printf("\nwhite spaces =%d",whites);

printf("\nothers =%d",others);

printf("\nit takes %d bytes for Storage",chr);

getch();

}

C’ software lab - 51 - MCA’05

Page 52: C lab-programs

OUTPUT :

Enter a text: Believe in LOVE

Entered String Consists of :

Vowels =7

consonants =6

digits =0

words =3

white spaces =2

others =0

it takes 15 bytes for Storage

C’ software lab - 52 - MCA’05

Page 53: C lab-programs

Experiment No. 15

Date:

AIM : TO SWAP THE VALUE OF TWO VARIABLES USING POINTERS.

ALGORITHM :

Step1: Start

Step2: Accept two numbers

Step 3: Declare two pointer variables

Step4: Intialise the pointer variable with address of variable

Step5: Use a temporary variable to interchange the address of variable

Step6: Print the Result

Step7: Stop

C’ software lab - 53 - MCA’05

Page 54: C lab-programs

/*Swapping Values Using Pointers*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

void swap(int *,int *);

clrscr();

printf("\n Enter the values of A & B :");

scanf("%d%d",&a,&b);

printf("\n the values entered are A : %d & B : %d",a,b);

swap(&a,&b);

printf("\n the swapped values are A : %d & B : %d",a,b);

getch();

}

void swap(int *x,int *y)

{

int *temp;

*temp=*x;

*x=*y;

*y=*temp;

}

C’ software lab - 54 - MCA’05

Page 55: C lab-programs

OUTPUT :

Enter the values of A & B :1 18

the values entered are A : 1 & B : 18

the swapped values are A : 18 & B : 1

C’ software lab - 55 - MCA’05

Page 56: C lab-programs

Experiment No. 16

Date:

AIM : TO FIND FACTORIAL OF A NUMBER USING RECURSION.

ALGORITHM :

Step1 : Start

Step2 : Declare variables and the function Fact();

Step3 : Accept the values, as num

Step4 : Print outputs as fact(num)

Step5 : In fact check for num to be 1

If true return(1)

If false return(num*fact(num))

Step6 : Stop

C’ software lab - 56 - MCA’05

Page 57: C lab-programs

/*Factorial Using Recursion*/

#include<stdio.h>

#include<conio.h>

void main()

{

int fact(int);

int num;

clrscr();

printf("\n Enter the number :");

scanf("%d",&num);

printf("\n Factorial of %d is %d",num,fact(num));

getch();

}

int fact(int x)

{

if(x==1)

return(1);

else

return(x*fact(x-1));

}

C’ software lab - 57 - MCA’05

Page 58: C lab-programs

OUTPUT :

Enter the number :6

Factorial of 6 is 720

C’ software lab - 58 - MCA’05

Page 59: C lab-programs

Experiment No.17

Date:

AIM : PROGRAM TO CONVERT NUMBERS INTO DIGITS

ALGORITHM :

Step 1 : Start

Step 2 : Declare separate 2D arrays for representing

characters from : one, two, three, …….,nine.

eleven, twelve, …………, nineteen

ten, twenty, ……………., hundred

Step 3 : Declare variables required

Step 4 : Accept the number less than 10000

Step 5 : Extract the left most digits by modular division of the number by 1000 to a

variable num

a. if num less than 10 give corresponding output

b. if num between eleven and nineteen both inclusive give corresponding

output

c. if number is a multiple of 10 give the result.

d. else split num as one’s and tens’s digit

repeat the step 5.a and 5.c

Step 7 : Add to the output a thousand

Step 8 : Extract the digits in hundreds the number by 1000 to a variable nnum

Step 9 : Extract the left most digits by modular division of the number by 100 to a

variable nunum

a. the number nunum is less than 10, hence give corresponding output

Step 10 : Add to the output a ‘hundred’.

Step 11 : Extract the right most digits by division of the number by 100 to a variable

nunum

a. the number nunum is less than 10, hence give corresponding output

C’ software lab - 59 - MCA’05

Page 60: C lab-programs

b. if num between eleven and nineteen both inclusive give corresponding

output

c. if number is a multiple of 10 give the result.

d. else split num as one’s and tens’s digit

repeat the step 11.a and 11.c

Step 12 : Display the output

Step 13 : Stop

C’ software lab - 60 - MCA’05

Page 61: C lab-programs

/*convert figure into word*/

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

{

char a[10][10]={"one","two","three","four",

"five","six","seven","eight","nine","ten"};

char b[10][10]={"eleven","twelve","thirteen","fourteen",

"fifiteen","sixteen","seventeen","eighteen",

"nineteen"};

char c[10][10]={"ten","twenty","thirty","forty","fifty",

"sixty","seventy","eighty","ninty","hundred"};

int r,s,t;

long n;

clrscr();

printf("Enter no.");

scanf("%ld",&n);

if(n>9999)

{

r=n/1000;

if(r>10 && r<20)

{

r=r%10;

printf("%s thousand ",b[r-1]);

}

else

{

s=r/10;

t=r%10;

printf("%s",c[s-1]);

printf("%s thousand ",a[t-1]);

C’ software lab - 61 - MCA’05

Page 62: C lab-programs

}

n=n%1000;

}

if(n>1000)

{

r=n/1000;

printf("%s thousand ",a[r-1]);

n=n%1000;

}

if(n>100)

{

r=n/100;

printf("%s hundred ",a[r-1]);

n=n%100;

}

if(n>10 && n<20)

{

r=n%10;

printf("%s ",b[r-1]);

}

if(n>19 && n<=100)

{

r=n/10;

n=n%10;

printf("%s",c[r-1]);

}

if(n>0 && n<=10)

printf("%s ",a[n-1]);

getch();

}

C’ software lab - 62 - MCA’05

Page 63: C lab-programs

OUTPUT :

Enter no.15018

fifiteen thousand eighteen

Enter no.44444

fortyfour thousand four hundred fortyfour

C’ software lab - 63 - MCA’05

Page 64: C lab-programs

Experiment No. 18

Date:

AIM : CONCATENATING TWO STRINGS USING A CHARACTER POINTER.

ALGORITHM :

Step 1: Start

Step 2: Declare 3 character array and 2 character pointer

Step 3: Accept 2 Strings

Step 4: Save the address of the strings to the pointers

Step 5: Save character by character of the first array to new array till the length by

incrementing address

Step 6: Save character by character of the second array by incrementing address of

2nd variable to the resultant array

Step 7: Add a null character at the end of resultant array

Step 8: Print the new array

Step 9: Stop

C’ software lab - 64 - MCA’05

Page 65: C lab-programs

/*String Concatenation using Pointers*/

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

#define LEN 10

void main()

{

char *s1,*s2,*s3;

int i=0,j=0;

char c;

clrscr();

s1=(char *)malloc(LEN * sizeof(char));

s2=(char *)malloc(LEN * sizeof(char));

s3=(char *)malloc(LEN * sizeof(char));

printf("\n Enter the First String :");

scanf("%s",s1);

printf("\n Enter the Second String :");

scanf("%s",s2);

while((c=s1[i])!='\0')

{

s3[i]=c;

i++;

}

while((c=s2[j])!='\0')

{

s3[i+j]=c;

j++;

} s3[i+j]='\0';

printf("\n Entered Strings '%s' and '%s' are concatenated to ...%s",s1,s2,s3);

getch();

}

C’ software lab - 65 - MCA’05

Page 66: C lab-programs

OUTPUT :

Enter the First String :Lovingly

Enter the Second String :Your's

Entered Strings 'Lovingly' and 'Your's' are concatenated to ...LovinglyYour's

C’ software lab - 66 - MCA’05

Page 67: C lab-programs

Experiment No. 19Date:

AIM : TO PROGRAM TO CREATE AND COUNT NUMBER OF CHARACTERS

IN A FILE.

ALGORITHM :

Step 1: start

Step 2: Declare fp as file pointer

Step 3: Declare count=0 as integer and c as character variable.

Step 4: Open the file charcount.txt in write mode (w) as fp.

Step 5: Read characters to be written into the file from the user

Step 6: Write the entered characters into the file using putc( ).

Step 7: close the file charcount.txt and reopen in read mode (r).

Step 8: Read the characters one by one from the file using getc( ) upto EOF and

Increment the count by 1.

Step 9: print the count as number of characters in the file.

Step 10: stop.

C’ software lab - 67 - MCA’05

Page 68: C lab-programs

/*create and count number of charachers in the file*/

#include<stdio.h>

void main()

{

FILE *fp;

char c;

int count =0;

clrscr();

printf("Enter characters:");

fp=fopen("chcount","w");

while((c=getchar())!=EOF)

putc(c,fp);

fclose(fp);

fp=fopen("chcount","r");

printf("String in the file is:");

while((c=getc(fp))!=EOF)

{

printf("%c",c);

++count;

}

fclose(fp);

printf("\nNumber of characters in the file is %d\n",count);

getch();

}

C’ software lab - 68 - MCA’05

Page 69: C lab-programs

OUTPUT :

Enter characters:Arun is a

goob boy.

^Z

String in the file is:Arun is a

goob boy.

Number of characters in the file is 20

C’ software lab - 69 - MCA’05

Page 70: C lab-programs

Experiment No. 20Date:

AIM : TO READ A FILE AND WRITE THE ODD VALUE TO A ODDFILE AND

EVEN VALUES TO A EVEN FILE.

ALGORITHM :

Step1: Start

Step2: Declare 3 File pointers (mainfile,oddfile,evenfile)

Step 3: Open the first file in w mode,oddfile and evenfile in write mode

Step4: Write the integer values in the file

Step5: Close the file

Step6: Reopen the file in read mode

Step7: Retrieve the value one by one from the file

Step8: Check whether there is any remainder when divided by two

Step9: If the remainder =0 Write the value to the even file

Step10: Otherwise write the value to the odd file

Step11: Stop

C’ software lab - 70 - MCA’05

Page 71: C lab-programs

/*even and odd file*/

#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp1,*fp2,*fp3;

int c,num,n;

clrscr();

fp1=fopen("number","w");

printf("Input numbers:");

while((scanf("%d",&c))!=EOF)

putw(c,fp1);

fclose(fp1);

fp1=fopen("number","r");

fp2=fopen("even","w");

fp3=fopen("odd","w");

printf("\n\nContents of file 'number':\n ");

while((c=getw(fp1))!=EOF)

{

printf("%d\t",c);

num=c%2;

if(num==0)

putw(c,fp2);

else

putw(c,fp3);

}

fclose(fp1);

fclose(fp2);

fclose(fp3);

fp2=fopen("even","r");

fp3=fopen("odd","r");

printf("\n\nContents of file 'even':\n ");

C’ software lab - 71 - MCA’05

Page 72: C lab-programs

while((c=getw(fp2))!=EOF)

printf("%d\t",c);

printf("\n\nContents of file 'odd':\n ");

while((c=getw(fp3))!=EOF)

printf("%d\t",c);

getch();

}

C’ software lab - 72 - MCA’05

Page 73: C lab-programs

OUTPUT :

Input numbers:3

2

4

5

6

7

8

2

1

^Z

Contents of file 'number':

3 2 4 5 6 7 8 2 1

Contents of file 'even':

2 4 6 8 2

Contents of file 'odd':

3 5 7 1

C’ software lab - 73 - MCA’05

Page 74: C lab-programs

C’ software lab - 74 - MCA’05