Sunday, November 20, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 12 - C Program Write a program which takes a file as command line argument and copy it to another file. At the end of the second file write the total i) no. of chars ii) no. of words and iii)no. of lines

Bharathiar University

Programming in C Lab
C Program 12
Write a program which takes a file as command line argument and copy it to another file. At the end of the second file write the total i) no. of chars ii) no. of words and iii)no. of lines


Source Code
//Program with File Location

#include <stdio.h>

int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
    // Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
    // Opening file2 in write mode
ptr2 = fopen("C:\\Users\\Admin\\Desktop\\sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
   letters++;
        /* Check new line */
        if (ch == '\n' || ch == '\0')
            lines++;
        /* Check words */
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
            words++;
  }
  if(letters>0)
    {
        words++;
        lines++;
    }
    fprintf(ptr2,"\nTotal Number of Characters:");
    fprintf(ptr2,"%d",letters);
    fprintf(ptr2,"\nTotal Number of Words:");
    fprintf(ptr2,"%d",words);
    fprintf(ptr2,"\nTotal Number of Lines:");
    fprintf(ptr2,"%d",lines);
    printf("Contents written in sample.txt file successfully");
// Closing the file
    fclose(ptr1);
fclose(ptr2);
    getch();
return 0;
}

To set Command line arguments

Run menu --> Arguments

Command line Argument with file location


Source Code

//Program without File Location

#include <stdio.h>

int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
    // Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
     // Opening file2 in write mode
ptr2 = fopen("sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
   letters++;
        /* Check new line */
        if (ch == '\n' || ch == '\0')
            lines++;
        /* Check words */
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
            words++;
  }
  if(letters>0)
    {
        words++;
        lines++;
    }
    fprintf(ptr2,"\nTotal Number of Characters:");
    fprintf(ptr2,"%d",letters);
    fprintf(ptr2,"\nTotal Number of Words:");
    fprintf(ptr2,"%d",words);
    fprintf(ptr2,"\nTotal Number of Lines:");
    fprintf(ptr2,"%d",lines);
    printf("Contents written in sample.txt file successfully");
// Closing the file
    fclose(ptr1);
fclose(ptr2);
    getch();
return 0;
}


Output:
Contents written in sample.txt file successfully


To set Command line arguments

Run menu --> Arguments

Command line Argument without file location

Input File



Output File

Friday, November 18, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 11 - C Program to Write a C Program which receives two filenames as arguments and check whether the file contents are same or not. If same delete the second file.

 

Program 11
Write a C Program which receives two filenames as arguments and check whether the file contents are same or not. If same delete the second file.


Source Code
#include <stdio.h>
#include <string.h>
void comparefiles(char filename1[50],char filename2[50]);
int main()
{
char f1[50],f2[50];
    printf("Enter 1st file name: ");
    gets(f1);
    printf("Enter 2nd file name: ");
    gets(f2);
comparefiles(f1,f2);

return 0;
}
void comparefiles(char filename1[50],char filename2[50])
{
FILE* ptr1, *ptr2;
char ch1,ch2;
int flag=1;
// Opening file1 in reading mode
ptr1 = fopen(filename1, "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
// Opening file2 in reading mode
ptr2 = fopen(filename2, "r");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
    
// Comparing two files.
do {
ch1 = fgetc(ptr1);
    ch2 = fgetc(ptr2);
    if(ch1!=ch2)
        {
            flag=0;
            break;
        }
  } while (ch1 != EOF || ch2 != EOF);
// Closing the file
    fclose(ptr1);
fclose(ptr2);
    if(flag==1)
    {
    
if(remove(filename2)==0)
    printf("\nContents are same. Second File Deleted successfully");
    else
    printf("\nUnable to delete the file");
    }
else
printf("\nFile Contents are not same");
}



Output


Enter 1st file name: test.txt
Enter 2nd file name: test1.txt
Contents are same. Second File Deleted successfully





Tuesday, November 15, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 10 - C Program to Write a function using pointers to add two matrices & return the resultant matrix to the calling function.

 Write a function using pointers to add two matrices & return the resultant matrix to the calling function.






Source Code

#include <stdio.h>
void matrixAdd(int m1[3][3], int m2[3][3], int res[3][3]);
int main()
{
    int r=3,c=3;
    int i,j;
    int x[3][3], y[3][3], res[3][3];
    // Input elements in first matrix
    printf("Enter elements in first matrix of size %dx%d: \n", r, c);
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            // (*(x + i) + j) is equal to &x[i][j]
            scanf("%d", (*(x + i) + j));
        }
    }
    // Input element in second matrix
    printf("\nEnter elements in second matrix of size %dx%d: \n", r, c);
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            // (*(y + i) + j) is equal to &y[i][j]
            scanf("%d", (*(y + i) + j));
        }
    }
 
    // Finc sum of both matrices and print result
    matrixAdd(x, y, res);

    printf("\nSum of first and second matrix: \n");
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            // *(*(res + i) + j) is equal to res[i][j]
            printf("%d ", *(*(res + i) + j));
        }
        printf("\n");
    }
    return 0;
}
void matrixAdd(int x[3][3], int y[3][3], int res[3][3])
{
    int i, j;
    // Iterate over each matrix elements 
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            // res[i][j] = x[i][j] + y[i][j]
            *(*(res + i) + j) = *(*(x + i) + j) + *(*(y + i) + j);
        }
    }
}
 Output



Thursday, November 10, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 9 - C Program to print the students marksheet assuming roll no, name and marks in 5 subjects in a structure. Create an array of structures and print the mark sheet in the university pattern

 

Write a C Program to print the students marksheet assuming roll no, name and marks in 5 subjects in a structure. Create an array of structures and print the mark sheet in the university pattern










Source Code


#include<stdio.h>

struct student

{

    char name[50];

    char rollno[10];

    int m1,m2,m3,m4,m5;

};

int main()

{

    struct student s[50];

    int i,n;

    printf("Enter number of students:");

    scanf("%d",&n);

    printf("Enter Records of Students\n");

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

    {

        printf("\nStudent: %d\n",i+1);

        printf("Enter Roll Number:");

        scanf("%s",s[i].rollno);

        printf("Enter Name:");

        scanf("%s",s[i].name);

        printf("Enter Mark 1:");

        scanf("%d",&s[i].m1);

        printf("Enter Mark 2:");

        scanf("%d",&s[i].m2);

        printf("Enter Mark 3:");

        scanf("%d",&s[i].m3);

        printf("Enter Mark 4:");

        scanf("%d",&s[i].m4);

        printf("Enter Mark 5:");

        scanf("%d",&s[i].m5);

    }

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

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

        printf("\n\t\tBharathiar University\n");

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

        printf("\t\tStatement of marks\n");

        printf("Branch : CS\t\t\t Student Roll number:%s",s[i].rollno);

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

        printf("Sub Code\t Subject\t Max Marks\t Passing Marks\t Awarded\t Result\n");

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

        printf("00S1\t\tSubject1\t100\t\t35\t\t%d",s[i].m1);

        if(s[i].m1>=35)

            printf("\t\tPass");

        else

            printf("\t\tFail");     

        printf("\n00S2\t\tSubject2\t100\t\t35\t\t%d",s[i].m2);

        if(s[i].m2>=35)

            printf("\t\tPass");

        else

            printf("\t\tFail");

        printf("\n00S3\t\tSubject3\t100\t\t35\t\t%d",s[i].m3);

        if(s[i].m3>=35)

            printf("\t\tPass");

        else

            printf("\t\tFail");

        printf("\n00S4\t\tSubject1\t100\t\t35\t\t%d",s[i].m4);

        if(s[i].m4>=35)

            printf("\t\tPass");

        else

            printf("\t\tFail");

        printf("\n00S5\t\tSubject5\t100\t\t35\t\t%d",s[i].m5);

        if(s[i].m5>=35)

            printf("\t\tPass");

        else

            printf("\t\tFail");

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

    }

}




Output
Enter number of students:2
Enter Records of Students
Student: 1
Enter Roll Number:19CS001
Enter Name:xxx
Enter Mark 1:56
Enter Mark 2:43
Enter Mark 3:32
Enter Mark 4:70
Enter Mark 5:85
Student: 2
Enter Roll Number:19CS002
Enter Name:yyy
Enter Mark 1:92
Enter Mark 2:85
Enter Mark 3:64
Enter Mark 4:72
Enter Mark 5:56


        ========================================
                Bharathiar University
                         Coimbatore
        ==========================================
                Statement of marks
Branch : CS                      Student Roll number:19CS001
===================================================================================   
Sub Code         Subject         Max Marks       Passing Marks   Awarded         Result
===================================================================================   
00S1            Subject1        100             35              56              Pass
00S2            Subject2        100             35              43              Pass  
00S3            Subject3        100             35              32              Fail  
00S4            Subject1        100             35              70              Pass  
00S5            Subject5        100             35              85              Pass  
================================================================================      
        ========================================
                Bharathiar University
                         Coimbatore
        ==========================================
                Statement of marks
Branch : CS                      Student Roll number:19CS002
===================================================================================   
Sub Code         Subject         Max Marks       Passing Marks   Awarded         Result
===================================================================================   
00S1            Subject1        100             35              92              Pass  
00S2            Subject2        100             35              85              Pass  
00S3            Subject3        100             35              64              Pass
00S4            Subject1        100             35              72              Pass  
00S5            Subject5        100             35              56              Pass  
================================================================================ 










Wednesday, November 2, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 8 - C Program to Find Factorial of a number using Recursive Function


Write a C Program to Find Factorial of a number using Recursive Function



Source Code

 #include<stdio.h>

long fact(int);

void main()

{

    int num;

    long fact_value;

    printf("Enter a number to find factorial: ");

    scanf("%d",&num);

    fact_value=fact(num);

    printf("Factorial of %d is  %ld",num,fact_value);

}

long fact(int n)

{

    if(n>=1)

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

    else

        return(1);

}


Output

Enter a number to find factorial: 5

Factorial of 5 is  120


Enter a number to find factorial: 8

Factorial of 8 is  40320

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...