0% found this document useful (0 votes)
60 views16 pages

Name - Memane Mahesh Balu College - Idol Class - Fymca Subject - C Programming

This document contains the details of assignments submitted by a student named Mehane Mahesh Balu for his class on C programming. It includes 12 programming assignments with the student's solutions and outputs for each one. The assignments cover topics like arrays, strings, functions, pointers, structures, files and more.

Uploaded by

Mahesh Memane
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views16 pages

Name - Memane Mahesh Balu College - Idol Class - Fymca Subject - C Programming

This document contains the details of assignments submitted by a student named Mehane Mahesh Balu for his class on C programming. It includes 12 programming assignments with the student's solutions and outputs for each one. The assignments cover topics like arrays, strings, functions, pointers, structures, files and more.

Uploaded by

Mahesh Memane
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

NAME

COLLEGE

- IDOL

CLASS

- FYMCA

SUBJECT

- C PROGRAMMING

SR. NO
1
2
3
4

ASSIGNMENT NO 3

MEMANE MAHESH BALU

DATE
13.11.2011
13.11.2011
13.11.2011
13.11.2011

ASSIGNMENT
ASSIGNMENT NO 1
ASSIGNMENT NO 2
ASSIGNMENT NO 3

SIGNATURE

Page 1

1. Write a program to implement bubble sort using function.


--------------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
void sort(int a[],int n);
void main()
{
int rj[5];
int n,i,j,temp;
clrscr();
printf("hw many elements ");
scanf("%d",&n);
printf("enter the elements");
flushall();
for(i=0;i<n;i++)
scanf("%d",&rj[i]);
sort(rj,n);
getch();
}
void sort(int rj[],int n)
{ int i,j,temp;
for(i=0;i<n;i++)
{ for(j=i+1;j<n;j++)
{ if(rj[i] > rj[j])
{ temp=rj[i];
rj[i]=rj[j];
rj[j]=temp; } }
}
printf("elements after sorting:");
for(i=0;i<n;i++)
printf("%d\n",rj[i]);
}
Output:
hw many elements 7
enter the elements1 5 -9 0 12 777 -3
elements after sorting:-9
-3
0
1
5
12
777

ASSIGNMENT NO 3

Page 2

ASSIGNMENT NO 3

Page 3

2. Write a program to perform different string functions.


---------------------------------------------------------------------------

ASSIGNMENT NO 3

Page 4

3. Write a program to calculate factorial of a no using recursion.


-------------------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num;
long f;
clrscr();
printf("Enter a Number To Find It's Factorial : ");
scanf("%d",&num);
f=fact(num);
printf("%d! = %ld",num,f);
getch();
}
int fact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
OUTPUT
Enter a Number To Find It's Factorial : 5
5! = 120

ASSIGNMENT NO 3

Page 5

4. Write a program to perform call by value and call by reference.


---------------------------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
void byvalue(int m,int n)
{
int temp;
temp=m;
m=n;
n=temp;
printf("\n\nInside call by value function:\nvalue of a=%d and b=%d",m,n);
}
void byref(int *m,int *n)
{
int temp;
temp=*m;
*m=*n;
*n=temp;
}
void main()
{
int a,b;
printf("enter two numbers :");
scanf("%d%d",&a,&b);
printf("Original values : a=%d and b=%d",a,b);
byvalue(a,b);
printf("\n\nvalues after call by balue function : a=%d and b=%d",a,b);
byref(&a,&b);
printf("\n\nvalues after call by ref function : a=%d and b=%d",a,b);
getch();
}

Output
enter two numbers :12 100
Original values : a=12 and b=100
Inside call by value function:
value of a=100 and b=12
values after call by balue function : a=12 and b=100
values after call by ref function : a=100 and b=12

ASSIGNMENT NO 3

Page 6

5. Write a program to implement operators using switch.


---------------------------------------------------------------------------------#include <stdio.h>
#include <conio.h>
void main ()
{
int a,b,c,d,e,f,g,h,i,j,k;
printf("Enter 2 no.s\n");
scanf("%d%d",&a,&b);
printf("enter 1-Arithmatic opr,\n2-Relation opr,\n3-Ternary opr,\n4-Bitwise");
scanf("%d",&c);
switch(c)
{
case 1:
printf("enter 1-add,\n2-sub,\n3-multiply,\n4-divide");
scanf("%d",&d);
switch (d)
{
case 1:
printf("addition=%d",a+b);
break;
case 2:
if (a>b)
{
e=a-b;
printf("subtraction =%d",e);
}
else
{
e=b-a;
printf("subtracton =%d",e);
}
break;
case 3:
e=a*b;
printf("prod =%d",e);
break;
case 4:
e=a/b;
printf("quot =%f",e);
}break;
case 2:
if (a>b)
{
printf("1st value is greater");
}
else if (a<b)
{
printf("2nd value is greater");
}
else
printf("both r equal");
break;
case 3:
f=(a>b?a-b:b-a);
ASSIGNMENT NO 3

Page 7

printf("ans =%d",f);
break;
case 4:
printf("enter a no ");
scanf("%d",&g);
printf("enter a no to shift");
scanf("%d",&h);
i=g<<h;
printf("\n%d left shift%d ",g,i);
j=g>>h;
printf("\n%d right shift%d ",g,j);
}
getch();
}

Output:
Enter 2 no.s
24 66
enter 1-Arithmatic opr,
2-Relation opr,
3-Ternary opr,
4-Bitwise1
enter 1-add,
2-sub,
3-multiply,
4-divide2
subtracton =42

ASSIGNMENT NO 3

Page 8

6. Write a program to delete duplicate elements from array using pointer.


--------------------------------------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int arr[50];
int *p;
int i,j,k,size,n;
printf("\nEnter size of the array: ");
scanf("%d",&n);
printf("\nEnter %d elements into the array: ",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
size=n;
p=arr;
for(i=0;i<size;i++)
{ for(j=0;j<size;j++)
{ if(i==j)
continue;
else if(*(p+i)==*(p+j))
{
k=j;
size--;
while(k < size)
{
*(p+k)=*(p+k+1);
k++;
}
j=0;
} } }
printf("\nThe array after removing duplicates is: ");
for(i=0;i < size;i++)
printf(" %d",arr[i]);
getch();
}
OUTPUT
Enter size of the array: 5
Enter 5 elements into the array: 10 10 20 30 40 50
The array after removing duplicates is: 10 20 30 40 50

ASSIGNMENT NO 3

Page 9

7.

Write a program to implement pointer to function.

-----------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
void main()
{
int n,j;
int fact(int);
int (*ptr)(int);
clrscr();
ptr=&fact;
printf("Enter the number\n");
scanf("%d",&n);
j=(*ptr)(n);
printf("The factorial of number %d is %d",n,j);
getch();
}
int fact(int x)
{
int i,f;
f=1;
for(i=1;i<=x;i++)
{
f=f*i;
}
return f;
}
Output:
Enter the number
5
The factorial of number 5 is 120

ASSIGNMENT NO 3

Page 10

8. Write a program to implement matrix multiplication.


------------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3];
int i,j,k,n;
clrscr();
printf("\n\nEnter the dimension:");
scanf("%d",&n);
printf("\n\nEnter elements for Matrix 1:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements for Matrix 2: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
printf("\nMultiplication of 2 matrices :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf(" ");
for(j=0;j<n;j++)
{
printf("%d ",b[i][j]);
}
printf(" ");
for(j=0;j<n;j++)
{
printf("%d ",c[i][j]);
}
ASSIGNMENT NO 3

Page 11

printf("\n");
}
getch();
}

OUTPUT:

Enter the dimension:2

Enter elements for Matrix 1:


12
23
Enter elements for Matrix 2:
24
10
Multiplication of 2 matrices :
12 24 44
23 10 78

ASSIGNMENT NO 3

Page 12

9. Write a program to check whether entered string is palindrome or not.


-------------------------------------------------------------------------------------------------------#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
long int n, num, rev = 0, dig;
printf("\n\n\t ENTER A NUMBER...: ");
scanf("%ld", &num);
n = num;
while(num>0)
{
dig = num % 10;
rev = rev * 10 + dig;
num = num / 10;
}
if (n == rev)
printf("\n\t GIVEN NUMBER IS A PALINDROME");
else
printf("\n\t GIVEN NUMBER NOT A PALINDROME");
getch();
}
OUTPUT
ENTER A NUMBER...: 1234321
GIVEN NUMBER IS A PALINDROME
ENTER A NUMBER...: 12345
GIVEN NUMBER NOT A PALINDROME

ASSIGNMENT NO 3

Page 13

10. WAP to pass structure to a function.


--------------------------------------------------------#include<stdio.h>
#include<conio.h>
void display(struct Book obj1);
struct Book
{
char bname[30];
char aname[30];
int pyear;
};

void main()
{
clrscr();
static struct Book obj={"Prog in C","G. S. Baluja", 2011};
display(obj);
getch();
}

void display(struct Book obj1)


{
printf("\n The Book Information is :: ");
printf("Book Name : %s", obj1.bname);
printf("Auther Name : %s ", obj1.aname);
printf("Printed Year : %d", obj1.pyear);
}
OUTPUT

The Book Information is ::


Book Name : Prog in C
Auther Name : G. S. Baluja
Printed Year : 2011

ASSIGNMENT NO 3

Page 14

11. WAP to write to & read from file.


--------------------------------------------------#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char c;
clrscr();
printf("Data input\n\n");
f1=fopen("INPUT","w");
while((c=getchar()) !=EOF)
putc(c,f1);
fclose(f1);
printf("\nData Output\n\n");
f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
printf("%c",c);
fclose(f1);
getch();
}
OUTPUT:
Data input

123asdf

bnknk
^Z

Data Output

123asdf

bnknk

ASSIGNMENT NO 3

Page 15

12. WAP to compare two files


------------------------------------------#include <stdio.h>
void main()
{
FILE *fp1, *fp2, *fopen();
int ca, cb;
char fname1[40], fname2[40] ;
printf(Enter first filename:) ;
gets(fname1);
printf(Enter second filename:);
gets(fname2);
fp1 = fopen( fname1, r );
/* open for reading */
fp2 = fopen( fname2, r ) ;
/* open for writing */
if ( fp1 == NULL )
/* check does file exist etc */
{
printf(Cannot open %s for reading \n, fname1 );
exit(1); /* terminate program */
}
else if ( fp2 == NULL )
{
printf(Cannot open %s for reading \n, fname2 );
exit(1); /* terminate program */
}
else
/* both files opened successfully */
{
ca = getc( fp1 ) ;
cb = getc( fp2 ) ;
while ( ca != EOF && cb != EOF && ca == cb )
{
ca = getc( fp1 ) ;
cb = getc( fp2 ) ;
}
if ( ca == cb )
printf(Files are identical \n);
else if ( ca != cb )
printf(Files differ \n );
fclose ( fp1 );
fclose ( fp2 );
}
}

OUTPUT:
Enter first filename:file1
Enter second filename:file2
Cannot open file1 for reading

ASSIGNMENT NO 3

Page 16

You might also like