You are on page 1of 33

V26

PRACTICAL

Sapna Kamlesh Badgujar


Sub: practical(Advance c )
COLLEGE : UCCC & SPBCBA & SDHG College of
BCA and IT
Roll No :: v26
STD : FYBCA SEM-2
Academic year :: 2017-2018

Sapna Badgujar
FYBCA Advance c V26

Question :: 1

Write a programme to generate integer number from 0 to n using udf

// a programme to generate integer number from 0 to n using udf

#include<stdio.h>

void gen(int); //function prototype

int main()

int a;

printf("enter the value of n::::");

scanf("%d",&a);

gen(a); //function calling

return(0);

void gen(int n) //function definition

int i;

printf("0 to n numbers are as following\n");

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

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

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

printf("\n");

Page 1 of 32
FYBCA Advance c V26

QUESTION ::2

Write a programme to toogle the case of given string using udf.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum string size

/* Toggle case function declaration */

void toggleCase(char * str);

int main()

char str[MAX_SIZE];

/* Input string from user */

printf("Enter any string: ");

gets(str);

printf("String before toggling case: %s", str);

toggleCase(str);

printf("String after toggling case: %s", str);

Page 2 of 32
FYBCA Advance c V26

return 0;

void toggleCase(char * str)

int i = 0;

while(str[i] != '\0')

if(str[i]>='a' && str[i]<='z')

str[i] = str[i] - 32;

else if(str[i]>='A' && str[i]<='Z')

str[i] = str[i] + 32;

i++;

Page 3 of 32
FYBCA Advance c V26

Question :: 3 Write a programme to cheak inputted word is palindrome or not using


udf

#include<stdio.h>

int pal(char []);

int main()

int i,a;

char s[10];

printf("enter the string :::::");

scanf("%s",&s);

a=pal(s);

if(a==1)

printf("string is palindrome");

if(a==0)

printf("string is not palindrome");

return(0);

int pal(char s[])

int i,f,l=0;

Page 4 of 32
FYBCA Advance c V26

for(i=0;s[i]!='\0';i++)

l++;

for(i=0;s[i]!='\0';i++)

if(s[i]==s[l-1-i])

f=1;

else

f=0;

return(f);

Question :: 4

A programme to calculate the sum of factorials from a given range

Page 5 of 32
FYBCA Advance c V26

Ans::

#include<stdio.h>

int factsum(int);

int main()

int n,a;

printf("enter the value of n:::::::::::::\n");

scanf("%d",&n);

a = factsum(n);

printf("sum of factorials from 1 To %d is::::::::::::::%d",n,a);

int factsum(int n)

int i,j,fact,factsum=0;

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

fact=1;

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

fact=fact*j;

factsum=factsum+fact;

return(factsum);

Page 6 of 32
FYBCA Advance c V26

Question 5::

Write a c programme to insert student details like id ,name


marks1,marks2,marks3 and display students records who are passed in all the
subject::::

#include<stdio.h>

struct student

int sid;

char s[20];

int marks1;

int marks2;

int marks3;

}s[5];

int main()

int i,j,n;

printf("how many records you are going to insert::");

scanf("%d",&n);

Page 7 of 32
FYBCA Advance c V26

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

printf("enter the record number %d::::\n",i+1);

printf("enter the student


id\nsname\nmarks1\nmarks2\nmarks3");

scanf("%d%s%d%d%d",&s[i].sid,s[i].s,&s[i].marks1,&s[i].marks2,&s[i].marks3)
;

printf("student who are passed\n");

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

if(s[i].marks1>=40 && s[i].marks2>=40 &&s[i].marks3>=40


)

printf("student id\n sname\n marks1\n marks2\n marks3


\n%d%s%d%d%d",s[i].sid,s[i].s,s[i].marks1,s[i].marks2,s[i].marks3);

return(0);

Page 8 of 32
FYBCA Advance c V26

QUESTION ::6

Create a structure product which contain members like product_id ,name ,price
quentity write a menu driven programe to perform following operation:::

1) insert

2)display

3)search

4)exit

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

Page 9 of 32
FYBCA Advance c V26

#define n 2

struct product

int p_id;

char name[20];

int price;

int quentity;

}p[5];

void display(struct product p[]);

void search(struct product p[]);

int main()

int c,i;

clrscr();

do{

printf("---------------------MENU------------------------------\nTo insert records press


1\nTo display records 2\nTo search record 3\nTo exit 4\n");

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

scanf("%d",&c);

switch(c)

case 1:

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

Page 10 of 32
FYBCA Advance c V26

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

printf("enter the p_id\npname\nprice\nquentity\n");

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

scanf("%d %s %d %d",&p[i].p_id,p[i].name,&p[i].price,&p[i].quentity);

break;

case 2:

display(p);

break;

case 3:

search(p);

break;

case 4:

exit(0);

}while(c<=4);

Page 11 of 32
FYBCA Advance c V26

return(0);

void display(struct product[])

int i;

printf("p_id\npname\nprice\nquentity\n");

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

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

printf("%d %s %d %d\n------------------------------
\n",p[i].p_id,p[i].name,p[i].price,p[i].quentity);

void search(struct product[])

int a,i;

printf("enter the product id to search records\n");

scanf("%d",&a);

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

if(a==p[i].p_id)

Page 12 of 32
FYBCA Advance c V26

printf(" p_id\npname\nprice\nquentity\n");

printf("%d %s %d %d\n",p[i].p_id,p[i].name,p[i].price,p[i].quentity);

}}}

Page 13 of 32
FYBCA Advance c V26

7 . write a c program for dynamic sorting in c using udf .

Ans:::::

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int i,j,k,l,m,n,*a,temp;

clrscr();

printf("enter the number of n:::::::::::::::::::");

scanf("%d",&n);

a=(int*) malloc(n *sizeof(int));

if(a==NULL)

Page 14 of 32
FYBCA Advance c V26

printf("error!!!!!!memory not allocated");

exit(0);

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

printf("enter the values %d:::::::",i+1);

scanf("%d",a+i);

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

for(j=i+1;j<n;j++)

if(*(a+i)<*(a+j))

temp=*(a+i);

*(a+i)=*(a+j);

*(a+j)=temp;

printf("%d ",*(a+i));

printf("\n");

getch(); }

Page 15 of 32
FYBCA Advance c V26

8. Write a command line programme to copy a file.

Ans:::::
/* File Copy using command line arguments */

#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}

while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}

9) Write a c programme using udf which passes two matrices using pointer add
them and finally Display the result

Page 16 of 32
FYBCA Advance c V26

Ans::

#include <stdio.h>

#define ROWS 3

#define COLS 3

void matrixInput(int mat[][COLS]);

void matrixPrint(int mat[][COLS]);

void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS]);

int main()

int mat1[ROWS][COLS], mat2[ROWS][COLS], res[ROWS][COLS];

printf("Enter elements in first matrix of size %dx%d: \n", ROWS, COLS);

matrixInput(mat1);

printf("\nEnter elemetns in second matrix of size %dx%d: \n", ROWS, COLS);

matrixInput(mat2);

matrixAdd(mat1, mat2, res);

printf("\nSum of first and second matrix: \n");

matrixPrint(res);

return 0;

void matrixInput(int mat[][COLS])

int i, j;

Page 17 of 32
FYBCA Advance c V26

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

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

// (*(mat + i) + j) is equal to &mat[i][j]

scanf("%d", (*(mat + i) + j));

void matrixPrint(int mat[][COLS])

int i, j;

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

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

printf("%d ", *(*(mat + i) + j));

printf("\n");

void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS])

int i, j;

Page 18 of 32
FYBCA Advance c V26

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

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

// res[i][j] = mat1[i][j] + mat2[i][j]

*(*(res + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j);

Page 19 of 32
FYBCA Advance c V26

10.. Write a c programme to implement library functions like


strlen,strcpy,strcat,strcmp with pointer ::

Ans ::

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void slen(char *a);

void scpy(char *b,char *c);

void scmp(char *b,char *c);

void scat(char *a,char *b);

void main()

char a[10],b[10],c[10];int n,i,ch;

clrscr();

do{

printf("----------------menu----------------------\nenter\n1 for strlen\n2 for strcopy\n3 for


strcat\n4 for strcomp\n5 for exit");

scanf("%d",&ch);

switch(ch)

Page 20 of 32
FYBCA Advance c V26

case 1:

printf("enter the string maximum 10 characters:;\n");

scanf("%s",&a);

slen(a);

break;

case 2:

printf("To perform string copy\nenter sourcestring\n");

scanf("%s",b);

scpy(b,c);

break;

case 3:

printf("enter two strings to merges:::\n");

printf("enter the first string");

scanf("%s",a);

printf("enter the second string");

scanf("%s",b);

scat(a,b);

break;

Page 21 of 32
FYBCA Advance c V26

case 4:

printf("enter the first string");

scanf("%s",a);

printf("enter the second string");

scanf("%s",b);

scmp(a,b);

break;

case 5:

exit(0);

default:

printf("enter the right choice");

}while(ch!=5);

getch();

void slen(char *a) // function strlen

int l=0,i;

Page 22 of 32
FYBCA Advance c V26

for(i=0;i<*(a+i)!=NULL;i++)

l++;

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

void scpy(char *b,char *c)

int i=0,j;

int n,m;

while(b[i]!=NULL)

c[i]=b[i];

i++;

c[i]='\0';

printf("source string is %s\n",b);

printf("target (copied) string is %s\n",c);

void scat(char *a ,char *b)

int i,j;

i=0;j=0;

Page 23 of 32
FYBCA Advance c V26

for(i=0; a[i]!='\0'; ++i);

for(j=0; b[j]!='\0'; ++j, ++i)

a[i]=b[j];

a[i]='\0';

printf("after merged strings are %s::\n",a);

void scmp(char *a,char *b)

int i=0,j=0,f,n,m;

n=strlen(a);

m=strlen(b);

while(a[i]!=n && b[j]!=m)

if(a[i]==b[j])

f=1;

else

f=0;

i++;

j++;

Page 24 of 32
FYBCA Advance c V26

if(f==1)

printf("strings are the same");

else

printf("strings are not the same");

Page 25 of 32
FYBCA Advance c V26

11. write a c programme to store the item information into a file inventory which
contain information about item_no ,item_name,qty and price ,read the information from
file and finally display into tabular format.

Ans:::

#include<stdio.h>

Page 26 of 32
FYBCA Advance c V26

#include<conio.h>

#include<stdlib.h>

void read();

void write();

struct record

int i_no;

char i_name[20];

char quality[20];

int price;

}s;

main()

int ch;

clrscr();

while(1)

printf("\n1:Write Records");

printf("\n2:Read Records");

printf("\n3:Exit");

printf("\n\tEnter Your Choice:- ");

scanf("%d",&ch);

switch(ch)

Page 27 of 32
FYBCA Advance c V26

case 1:

write();

break;

case 2:

read();

break;

case 3:

exit(1);

default:

printf("\n\tOption not Available\n");

break;

getch();

void write()

int i,n=0;

FILE *fp;

fp=fopen("f:\\item.txt","wb");

if(fp==NULL)

printf("can't create file");

getch();

Page 28 of 32
FYBCA Advance c V26

exit(1);

printf("\n\tHow Many Records You Want to Enter:=");

scanf("%d",&n);

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

printf("\nEnter item no := ");

scanf("%d",&s.i_no);

printf("\nEnter item Name := ");

scanf("%s",s.i_name);

flushall();

printf("\nEnter the quality :=");

scanf("%s",&s.quality);

printf("\nenter the price :=");

scanf("%d",&s.price);

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

fwrite(&s,sizeof(s),1,fp);

fclose(fp);

void read()

FILE *fp;

fp=fopen("f:\\item.txt","r");

Page 29 of 32
FYBCA Advance c V26

if(fp==NULL)

printf("can't read file");

getch();

exit(1);

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

printf("\nitem_no item_name item_quality item_price \n");

while(fread(&s,sizeof(s),1,fp)==1)

printf(" %d %s %d %d
\n",s.i_no,s.i_name,s.quality,s.price);

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

fclose(fp);

Page 30 of 32
FYBCA Advance c V26

Page 31 of 32
FYBCA Advance c V26

Page 32 of 32

You might also like