0% found this document useful (0 votes)
13 views61 pages

Final C Programs

The document contains multiple C programming assignments completed by Harsh Sandip Jadhav, focusing on dynamic memory allocation, pointer usage, and basic arithmetic operations. Each assignment includes code snippets, example outputs, and a brief description of the task. The assignments cover topics such as reversing arrays, matrix operations, and calculating sums and averages.

Uploaded by

Avni Computers
Copyright
© © All Rights Reserved
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)
13 views61 pages

Final C Programs

The document contains multiple C programming assignments completed by Harsh Sandip Jadhav, focusing on dynamic memory allocation, pointer usage, and basic arithmetic operations. Each assignment includes code snippets, example outputs, and a brief description of the task. The assignments cover topics such as reversing arrays, matrix operations, and calculating sums and averages.

Uploaded by

Avni Computers
Copyright
© © All Rights Reserved
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/ 61

/**************************************************************************************

Name:Harsh Sandip Jadhav Roll No.: 3066


Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:1.
Assignment Name:Write a program to accept N integers and store them dynamically
display them in reverse order.
**************************************************************************************/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p,n,i;
printf("How many numbers you want to enter: ");
scanf("%d",&n);

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

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

for(i=0;i<n;i++)
{
scanf("%d",p+i);
}
printf("\nArray in Reverse Order: \n\n");

for(i=n-1;i>=0;i--)
{
printf(" %d",*(p+i));
}
return 0;
}

/****************** Output ****************


[ctbora@localhost dma]$ cc 3.c
[ctbora@localhost dma]$ ./a.out
How many numbers you want to enter: 5
Enter 5 Numbers:
15
46
85
96
75
Array in Reverse Order: 75 96 85 46 15
********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:2.
Assignment Name:Write a C program to accept a matrix and display it using dynamic
memory allocation.
**************************************************************************************/

#include<stdio.h>
#include<stdlib.h>
main()
{
int *a[10],r,c,i,j;
printf("Enter the order of matrix\n");
scanf("%d%d",&r,&c);

printf("Enter matrix elements\n");


for(i=0;i<r;i++)
{
/**** dynamically allocate memory for every row ****/
a[i]=(int *)malloc(c*sizeof(int));
for(j=0;j<c;j++)
{
scanf("%d",a[i]+j);
}
}

/****** Display Matrix ******/


printf("The matrix is as below\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",*(*(a+i)+j));
}
printf("\n");
}
}

/*********************** Output *******************


[ctbora@localhost dma]$ cc 2.c
[ctbora@localhost dma]$ ./a.out
Enter the order of matrix
2 3

Enter matrix elements


1
2
3
4
5
6
The matrix is as below
1 2 3
4 5 6
*********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:3.
Assignment Name:Write a C program to allocate memory dynamically for n integers
such that the memory is initialized to 0. And display the sum of n
numbers entered by the user.
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int*) calloc(n, sizeof(int));


if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");


for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);


free(ptr);
return 0;
}

/****************** Output ******************


[ctbora@localhost dma]$ cc 4.c
[ctbora@localhost dma]$ ./a.out
Enter number of elements: 5
Enter elements: 12
45
65
78
23
Sum = 223
**********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:4.
Assignment Name:Write a C program toAccept N integers in an array using dynamic
memory allocation. Find maximum from them and display it.
**************************************************************************************/

#include<stdio.h>
#include<stdlib.h>

int main()
{
int n,i;
double *data;
printf("Enter the total number of elements: ");
scanf("%d", &n);

// Allocating memory for n elements


data= (double *)calloc(n,sizeof(double));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
// Storing numbers entered by the user.
for (i = 0; i < n; ++i)
{
printf("Enter number%d: ", i + 1);
scanf("%lf", data + i);
}

// Finding the largest number


for(i = 1; i < n; ++i)
{
if (*data < *(data + i))
{
*data = *(data + i);
}
}
printf("Largest number = %.2lf", *data);
return 0;
}

/************************ Output ************************


[ctbora@localhost dma]$ cc 5.c
[ctbora@localhost dma]$ ./a.out
Enter the total number of elements: 5
Enter number1: 12
Enter number2: 32
Enter number3: 45
Enter number4: 78
Enter number5: 56
Largest number = 78.00
*********************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:5.
Assignment Name:Write a C program toaccept n integers in an array. Copy only the
non-zero elements to another array (allocated using dynamic
memory allocation). Calculate the sum and average of non-zero
elements.
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main()
{
int* ptr; //declaration of integer pointer
int limit; //to store array limit
int i; //loop counter
int sum; //to store sum of all elements

printf("Enter limit of the array: ");


scanf("%d", &limit);

//declare memory dynamically


ptr = (int*)malloc(limit * sizeof(int));

//read array elements


for (i = 0; i < limit; i++)
{
printf("Enter element %02d: ", i + 1);
scanf("%d", (ptr + i));
}

//print array elements


printf("\nEntered array elements are:\n");
for (i = 0; i < limit; i++)
{
printf("%d\n", *(ptr + i));
}

//calculate sum of all elements


sum = 0; //assign 0 to replace garbage value
for (i = 0; i < limit; i++)
{
sum += *(ptr + i);
}
printf("Sum of array elements is: %d\n", sum);
printf("Average of array elements is : %f\n",(float)sum/limit);
free(ptr);
return 0;
}

/*********************** Output **********************


[ctbora@localhost dma]$ ./a.out
Enter limit of the array: 5
Enter element 01: 12
Enter element 02: 35
Enter element 03: 25
Enter element 04: 62
Enter element 05: 78

Entered array elements are:


12
35
25
62
78
Sum of array elements is: 212
Average of array elements is : 42.400000
*****************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:6.
Assignment Name:Write a C program toaccept a matrix of order mXn (Use dynamic
memory allocation and array of pointers concept). Display trace/
sum of diagonal elts of the matrix.
**************************************************************************************/

#include<stdlib.h>
#include<stdio.h>

int main()
{
int m, n,i,j,sum=0;
printf("Enter no. of rows and columns: ");
scanf("%d%d", &m, &n);

int **a;

//Allocate memory to matrix


a = (int **) malloc(m * sizeof(int *));
for(i=0; i<m; i++)
{
a[i] = (int *) malloc(n * sizeof(int));
}

//Read elements into matrix


printf("Enter matrix elements: ");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}

printf("Diagonal elements are: \n");

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


{
for(j=0; j<n; j++)
{
if(i==j)
{
printf("%d ", a[i][j]);
sum=sum+a[i][j];
}
} printf("\n");

}
printf("Sum=%d",sum);
}
//Dellocating memory of matrix
for(i=0; i<m; i++)
{
free(a[i]);
}
free(a);
return 0;
}

/********************** Output *********************


[ctbora@localhost dma]$ cc 7.c
[ctbora@localhost dma]$ ./a.out
Enter no. of rows and columns: 2
2
Enter matrix elements:
1
2
3
4
Matrix elements are:
12
34
Diagonal elements are:
1
4
Sum=5
***************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:7.
Assignment Name:Write a C program toaccept a matrix of order mXn (Use dynamic
memory allocation and array of pointers concept). Display
columnwise sum of the matrix.
**************************************************************************************/

#include<stdlib.h>
#include<stdio.h>
int main()
{
int m, n,i,j,sum=0;
printf("Enter no. of rows and columns: ");
scanf("%d%d", &m, &n);
int **a;

//Allocate memory to matrix


a = (int **) malloc(m * sizeof(int *));
for(i=0; i<m; i++)
{
a[i] = (int *) malloc(n * sizeof(int));
}

//Read elements into matrix


printf("Enter matrix elements: ");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}

//Print elements in the matrix


printf("Matrix elements are: \n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}

printf("Column elements are: \n");


for(i=0; i<m; i++)
{
for(j=i; j<n; j++)
{
printf("%d ", a[i][j]);
sum=sum+a[i][j];
} printf("\n");
}
printf("Sum=%d",sum);
//
Dellocating memory of matrix
for(i=0; i<m; i++)
{
free(a[i]);
}
free(a);
return 0;
}

/*************** Output ***************


[ctbora@localhost dma]$ cc 7.c
[ctbora@localhost dma]$ ./a.out
Enter no. of rows and columns: 2 2
Enter matrix elements:
1
2
3
4
Matrix elements are:
12
34
Diagonal elements are:
1
4
Sum=5
***************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:8.
Assignment Name:Write a program to read two integers using pointers and perform
all arithmetic operations on them.
**************************************************************************************/

#include<stdio.h>
int main()
{
int no1,no2;
int*ptr1,*ptr2;
int sum,sub,mult;
float div;

printf("Enter number1:\n");
scanf("%d",&no1);
printf("Enter number2:\n");
scanf("%d",&no2);

ptr1=&no1;//ptr1 stores address of no1


ptr2=&no2;//ptr2 stores address of no2

sum=(*ptr1) + (*ptr2);
sub=(*ptr1) - (*ptr2);
mult=(*ptr1) * (*ptr2);
div=(float)(*ptr1) / (*ptr2);

printf("sum= %d\n",sum);
printf("subtraction= %d\n",sub);
printf("Multiplication= %d\n",mult);
printf("Division= %f\n",div);
return 0;
}

/********************** Output *********************


[student@localhost pointer]$ cc 1.c
[student@localhost pointer]$ ./a.out
Enter number1:
23
Enter number2:
34
sum= 57
subtraction= -11
Multiplication= 782
Division= 0.676471
***************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:9.
Assignment Name:Write a program to accept an integer using pointer and check
whether it is even or odd.
**************************************************************************************/

#include<stdio.h>
int main()
{
int num, rem;
int *pnum;
pnum= &num;

printf("Enter number: ");


scanf("%d",pnum); //pnum has address of num

rem= *pnum % 2;

if(rem==0)
printf("%d is even.\n", *pnum);

else
printf("%d is odd.\n", *pnum);

return 0;
}

/***************** Output ********************


[student@localhost pointer]$ cc 2.c
[student@localhost pointer]$ ./a.out
Enter number: 12
12 is even.
[student@localhost pointer]$ ./a.out
Enter number: 35
35 is odd.
***********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:10.
Assignment Name:Write a C program to find maximum from three integers using
pointers.
**************************************************************************************/

#include<stdio.h>
int main()
{
float x, y, z;
float *px, *py, *pz;
px=&x, py=&y, pz=&z;

printf("Enter three number:");


scanf("%f %f %f", px, py, pz);

if(*px > *py)


{
if(*px > *pz)
printf("Biggest = %.2f", *px);
else
printf("Biggest = %.2f", *pz);
}
else
{
if(*py > *pz)
printf("Biggest = %.2f", *py);
else
printf("Biggest = %.2f", *pz);
}

return 0;
}

/***************** Output ***************


[student@localhost pointer]$ cc 3.c
[student@localhost pointer]$ ./a.out
Enter three number:
12
67
45
Biggest = 67.00
****************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:11.
Assignment Name:Write a C program to Reverse array using
Pointers.*******************************************************************************
*******/

#include<stdio.h>
#define MAX 30

int main()
{
int size, i, arr[MAX];
int *ptr;

ptr = &arr[0];

printf("Enter the size of array :: ");


scanf("%d", &size);

printf("\nEnter %d integers into array:\n ", size);


for (i = 0; i < size; i++)
{
printf("\nEnter %d integer into array: ", i+1);
scanf("%d", ptr);
ptr++;
}

ptr = &arr[size - 1];

printf("\nElements of array in reverse order are :\n");

for (i = size - 1; i >= 0; i--)


{
printf("\n\nElement %d is %d ", i+1, *ptr);
ptr--;
}

return 0;
}

/********************* Output *******************


[student@localhost pointer]$ cc 4.c
[student@localhost pointer]$ cc 4.c
[student@localhost pointer]$ ./a.out
Enter the size of array :: 5
Enter 5 integers into array:
Enter 1 integer into array: 34
Enter 2 integer into array: 56
Enter 3 integer into array: 3
Enter 4 integer into array: 89
Enter 5 integer into array: 0
Elements of array in reverse order are :
Element 5 is 0
Element 4 is 89
Element 3 is 3
Element 2 is 56
*************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:12.
Assignment Name:Write a C program to read N integers in an array using pointers
and display minimum from
them.**********************************************************************************
****/

#include <stdio.h>

main()
{
int array[100], *minimum, size, c, location = 1;

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


scanf("%d",&size);

printf("Enter %d integers\n", size);

for ( c = 0 ; c < size ; c++ )


scanf("%d", &array[c]);
minimum = array;
*minimum = *array;

for ( c = 1 ; c < size ; c++ )


{
if ( *(array+c) < *minimum )
{
*minimum = *(array+c);
location = c+1;
}
}

printf("Minimum element is present at location number %d and it's value is %d.\


n", location, *minimum);
return 0;
}

/********************************** Output ******************************


[student@localhost pointer]$ cc 5.c
[student@localhost pointer]$ ./a.out
Enter the number of elements in array: 5
Enter 5 integers
23
45
87
47
12
Minimum element is present at location number 5 and it's value is 12.
*************************************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:13.
Assignment Name:Write a C function that takes radius of as parameter and two
variables. Set first variable to area of circle and second to
perimeter of circle. Accept radius in main and also display area and
perimeter in main using the above function. (Hint: Pass the
addresses of the variables to the function to get area and

perimeter).****************************************************************************
**********/

#include<stdio.h>

void area_peri(float, float*, float*);

int main()
{
float radius, area, perimeter;

printf("Enter radius of Circle\n");


scanf("%f", &radius);

area_peri(radius, &area, &perimeter);

printf("\nArea of Circle = %0.2f\n", area);


printf("Perimeter of Circle = %0.2f\n", perimeter);

return 0;
}

void area_peri(float r, float *a, float *p)


{
*a = 3.14 * r * r;
*p = 2 * 3.14 * r;
}

/****************** Output *******************


[student@localhost pointer]$ cc 6.c
[student@localhost pointer]$ ./a.out
Enter radius of Circle
4.5
Area of Circle = 63.58
Perimeter of Circle = 28.26
**********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:14.
Assignment Name:Write a C program to accept n integers in array A in main. Write a
function which takes this array as parameter and find minimum
and maximum from it. Display these values in main
**************************************************************************************/

#include <stdio.h>

int sumofarray(int a[],int n)


{
int min,max,i;
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
}

int main()
{
int a[1000],i,n,sum;

printf("Enter size of the array : ");


scanf("%d", &n);

printf("Enter elements in array : ");


for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sumofarray(a,n);
}

/********************* Output *********************


[student@localhost pointer]$ cc 7.c
[student@localhost pointer]$ ./a.out
Enter size of the array : 5
Enter elements in array : 12 45 67 89 43
minimum of array is : 12
maximum of array is : 89
***************************************************/

/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:15.
Assignment Name:Write a program to read a string and copy it to another sting and
display copied string. Also the length of copied string. (Use built in
functions).
**************************************************************************************/

//Not Using Standard Function

#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
gets(s1);
for (i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}

s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}

//Using Standard Function

#include<stdio.h>
#include<string.h>

void main()
{
char s1[100],s2[100];
printf("\nEnter string s1");
gets(s1);

strcpy(s2,s1);

printf("\nS1=%s\nS2=%s",s1,s2);
}

/**************** Output ****************


[ctbora@localhost string]$ ./a.out
Enter string s1rahul
S1=rahul
S2=rahul
****************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:16.
Assignment Name:Write a program to read a string and one character. Check whether
given character is present in the given string or not? (Hint: use
strchr or strrchr).
**************************************************************************************/

#include <stdio.h>
#include <string.h>

int main ()
{
int len;
char str[50];
char ch;
printf("\nEnter the string");
gets(str);
printf("\nEnter the character to find ...");
scanf("%c",&ch);

char *ret;

ret = strrchr(str, ch);


if(ret==NULL)
printf("The character not found");
else
printf("The Character %c is found at %s", ch, ret);

return(0);
}

/********************** Output *******************


[rsp@localhost string]$ ./a.out
Enter the string:rahul
Enter the character to find ...r
The Character r is found at rahul
[rsp@localhost string]$ ./a.out
Enter the string:rahul
Enter the character to find ...p
The character not found
*************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:17.
Assignment Name:Write a program which accepts a sentence from the user and alters
it as follows: Every space is replaced by *, case of all alphabets is
reversed, digits are replaced by ?.
**************************************************************************************/

#include<stdio.h>
#include<ctype.h>
#include<string.h>

void Stral(char str[])


{
int i;

// To replace space by * in sentence


for(i=0;i<=strlen(str)-1;i++)
{
if(str[i]==' ')
str[i]='*';
// To change the case of alphabets in sentence
if(islower(str[i]))
str[i]=toupper(str[i]);
else
str[i]=tolower(str[i]);
// To replace digits by ? in sentence
if(isdigit(str[i]))
str[i]='?';
}
printf("\n %s \n",str);
}

void main()
{
char str[100];
printf("\n Enter any sentence:-");
fgets(str,100,stdin);
Stral(str);
}

/***************** Output *******************


[rsp@localhost string]$ cc 4.c
[rsp@localhost string]$ ./a.out
Enter any sentence:-I 143 C Programming.
i*???*c*pROGRAMMING.
*********************************************/

/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:18.
Assignment Name:Write a program which accepts a sentence from the user. Find and
display reverse of it. (Don’t use any function).
**************************************************************************************/

#include <stdio.h>
#include <string.h>
void printReverse(char str[])
{
int length = strlen(str);
int i;
for (i = length - 1; i >= 0; i--)
{
if (str[i] == ' ')
{
str[i] = '\0';
printf("%s ", &(str[i]) + 1);
}
}
printf("%s", str);
}

int main()
{
char str[50];
printf("\nEnter the string");
gets(str);
printReverse(str);
return 0;
}

/**************** Output ***************


[rsp@localhost string]$ ./a.out
Enter the stringI love C programming
programming C love I
****************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:19.
Assignment Name:Write a program that accepts n words and outputs them in
dictionary order.
**************************************************************************************/

#include<stdio.h>
#include <string.h>

int main()
{
int i, j;
char str[10][50], temp[50];

printf("\nEnter 10 words:: \n");

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


scanf("%s[^\n]",str[i]);

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


for(j=i+1; j<10 ; ++j)
{
if(strcmp(str[i], str[j])>0)
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}

printf("\nIn lexicographical order: \n");


for(i=0; i<10; ++i)
{
puts(str[i]);
}
return 0;
}

/****************** Output ******************


[rsp@localhost string]$ ./a.out
Enter 10 words::
c
programming
dbms
rdbms
math
stat
rsp
pqr
xyz
abc
In lexicographical order:
abc
c
dbms
math
pqr
programming
rdbms
rsp
stat
xyz
*****************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:20.
Assignment Name:Write a program that accepts a string and generates all its
permutations. For example: ABC, ACB, BAC, BCA, CAB,CBA.
**************************************************************************************/

#include <stdio.h>
#include <string.h>

/* Function to swap values at two pointers */


void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}

/* Function to print permutations of string


* This function takes three parameters:
* 1. String
* 2. Starting index of the string
* 3. Ending index of the string. */

void permute(char *a, int l, int r)


{
int i;
if (l == r)
printf("%s\n", a);
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i)); //backtrack
}
}
}

/* Driver program to test above functions */


int main()
{
char str[] = "ABC";
int n = strlen(str);
permute(str, 0, n-1);
return 0;
}

/***************** Output *****************


[rsp@localhost string]$ cc 8.c
[rsp@localhost string]$ ./a.out
ABC
ACB
BAC
BCA
CBA
CAB
**************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:21.
Assignment Name:Write a menu driven program to perform the following operations
on strings using standard library functions: 1.Length
2. Copy
3.Concatenation
4. Compare.
**************************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char *strrev(char *str);

int main()
{
char str1[20],str2[20];
int ch,i,j;
do
{
printf("\tMENU");
printf("\n------------------------------\n");
printf("1:Find Length of String");
printf("\n2:Find Reverse of String");
printf("\n3:Concatenate Strings");
printf("\n4:Copy String ");
printf("\n5:Compare Strings");
printf("\n6:Exit");
printf("\n------------------------------\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1:printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %d\n\n",i);
break;
case 2:printf("Enter String: ");
scanf("%s",str1);
printf("Reverse string : %s\n\n",strrev(str1));
break;
case 3:printf("\nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %s\n\n",str1);
break;
case 4:printf("Enter a String1: ");
scanf("%s",str1);
printf("Enter a String2: ");
scanf("%s",str2);
printf("\nString Before
Copied:\nString1=\"%s\",String2=\"%s\"\n",str1,str2);
strcpy(str2,str1);
printf("-----------------------------------------------\n");
printf("\"We are copying string String1 to String2\" \n");
printf("-----------------------------------------------\n");
printf("String After Copied:\nString1=\"%s\",String2=\"%s\"\n\
n",str1,str2);
break;
case 5:printf("Enter First String: ");
scanf("%s",str1);
printf("Enter Second String: ");
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
{
printf("Strings are Same\n\n");
}
else
{
printf("Strings are Not Same\n\n");
}
break;
case 6:exit(0);
break;
default:printf("Invalid Input. Please Enter valid Input.\n\n ");
}
}while(ch!=6);
return 0;
}

char *strrev(char *str)

if (!str || ! *str)
return str;

int i = strlen(str) - 1, j = 0;
char ch;

while (i > j)
{

ch = str[i];
str[i] = str[j];
str[j] = ch;
i--;
j++;
}
return str;
}

/******************** Output ****************


[rsp@localhost string]$ ./a.out
MENU
-----------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Compare Strings
6:Exit
------------------------------
Enter your choice: 1
Enter String: programming
Length of String : 11
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Compare Strings
6:Exit
------------------------------
Enter your choice: 2
Enter String: programming
Reverse string : gnimmargorp
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Compare Strings
6:Exit
------------------------------
Enter your choice: 3
Enter First String: c
Enter Second string: programming
String After Concatenation : cprogramming
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Compare Strings
6:Exit
-----------------------------
Enter your choice: 4
Enter a String1: c
Enter a String2: programming
String Before Copied:
String1="c",String2="programming"
-----------------------------------------------
We are copying string String1 to String2"
-----------------------------------------------
String After Copied:
String1="c", String2="c"
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Compare Strings
6:Exit
------------------------------
Enter your choice: 5
Enter First String: rahul
Enter Second String: pawar
Strings are Not Same
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Compare Strings
6:Exit
------------------------------
Enter your choice: 6
***************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:22.
Assignment Name:Write a Cprogram to accept a string and find its length using user
defined function. (Don’t use pointers).
**************************************************************************************/

#include<stdio.h>

// Prototype Declaration
int FindLength(char str[]);

int main()
{
char str[100];
int length;

printf("\nEnter the String : ");


gets(str);

length = FindLength(str);

printf("\nLength of the String is : %d", length);


return(0);
}

int FindLength(char str[])


{
int len = 0;
while (str[len] != '\0')
len++;
return (len);
}

/************** Output *************


[root@localhost string]# ./a.out
Enter the String : rahul
Length of the String is : 5
***********************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:23.
Assignment Name:Write a C function that takes a string as parameterand returns the
same string in uppercase(use pointes). Accept this string in main
and display converted string in main only.
**************************************************************************************/

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str[MAX_SIZE];
int i;

/* Input string from user */


printf("Enter your text : ");
gets(str);

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


{
/*
* If current character is lowercase alphabet then
* convert it to uppercase.
*/
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;
}
}

printf("Uppercase string : %s",str);


return 0;
}

/**************** Output ******************


[root@localhost string]# ./a.out
Enter your text : c programming
Uppercase string : C PROGRAMMING
*******************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:24.
Assignment Name:Write a function to find reverse of the string and use it in main.
**************************************************************************************/

#include <stdio.h>
#include <string.h>

char *strrev(char *str);

void main()
{
char str[20];
printf("\nEnter the string\n");
gets(str);
printf("\nThe reverse of given string is %s",strrev(str));
}

char *strrev(char *str)


{
if (!str || ! *str)
return str;

int i = strlen(str) - 1, j = 0;
char ch;

while (i > j)
{
ch = str[i];
str[i] = str[j];
str[j] = ch;
i--;
j++;
}
return str;
}

/******************** Output *******************


[root@localhost string]# ./a.out

Enter the string


C Programming

The reverse of given string is gnimmargorP C


***********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:25.
Assignment Name:Write a program that will accept a string and character to search.
The program will call a function, which will search for the
occurrence of the character in the string and return its position.
Function should return –1 if the character is not found in the
string. Display this position in main() function.
**************************************************************************************/

#include <stdio.h>
int main()
{
char str[1000], ch;
int count = 0,i;

printf("Enter a string: ");


gets(str);

printf("Enter a character to find its frequency: ");


scanf("%c", &ch);

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


{
if (ch == str[i])
{
++count;
printf("\nThe position where the character %c found is %d",ch,i);
}
}
printf("\nFrequency of %c = %d", ch, count);
return 0;
}

/****************** Output *******************


[root@localhost string]# ./a.out
Enter a string: geekforgeeks
Enter a character to find its frequency: e

The position where the character e found is 1


The position where the character e found is 2
The position where the character e found is 8
The position where the character e found is 9
Frequency of e = 4
***********************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:26.
Assignment Name:Write a c program to Create a structure employee (id, name,
salary). Accept details of n employees and display display it in
summary format.
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
char name[30];
int id;
double salary;
} Employee;

int main()
{
//number of employees
int n=2,i;

/*printf("\nEnter the number of employees\n");


scanf("%d",&n);
*/
//array to store structure values of all employees
Employee employees[n];

//Taking each employee detail as input


printf("Enter %d Employee Details \n \n",n);
for(i=0; i<n; i++)
{
printf("Employee %d:- \n",i+1);

//Name
printf("Name: ");
scanf("%[^\n]s",employees[i].name);

//ID
printf("Id: ");
scanf("%d",&employees[i].id);

//Salary
printf("Salary: ");
scanf("%lf",&employees[i].salary);

//to consume extra '\n' input


char ch = getchar();

printf("\n");
}

//Displaying Employee details


printf("-------------- All Employees Details ---------------\n");
for(i=0; i<n; i++)
{
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}

/******************** Output **********************


[root@localhost structure]# ./a.out
Enter 2 Employee Details

Employee 1:-
Name: rahul
Id: 101
Salary: 50000

Employee 2:-
Name: pawar
Id: 102
Salary: 60000

-------------- All Employees Details ---------------


Name : rahul
Id : 101
Salary : 50000.00

Name : pawar
Id : 102
Salary : 60000.00
***************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:27.
Assignment Name:Create a structure item(item number, item name, rate, qty, total).
Accept details of n items (calculate total as qty * rate). And display
Bill in the following format Sr. No. Item Name Rate Qty Total 1 Pen
10 3 30.00 2 Eraser 5 1 05.00 Grand Total : 35.00.
**************************************************************************************/

#include<stdio.h>
struct in
{
int cod;
char name[10];
int p;
int q;
float cost;
float tcost;
}s[3];

void input(struct in up[],int n)


{
int i;
for(i=1;i<=n;i++)
{
printf("%d ITEM CODE:",i);
scanf("%d",&up[i].cod);

printf("ITEM NAME:");
scanf("%s",up[i].name);

printf("ITEM PRICE:");
scanf("%d",&up[i].p);

printf("ITEM QTY:");
scanf("%d",&up[i].q);
}

void output(struct in up[],int n)


{
int i;
float tcost=0.0;
for(i=1;i<=n;i++)
{
printf("\n%d ITEM CODE:%d",i,up[i].cod);
printf("\nITEM NAME:%s",up[i].name);
printf("\nITEM PRICE:%d",up[i].p);
printf("\nITEM QTY:%d",up[i].q);
up[i].cost=up[i].p*up[i].q;
tcost+=up[i].cost;
printf("\nITEM COST:%f",up[i].cost);
}
printf("\n\nTOTAL BILL:%f",tcost);
}

void main()
{
int i;
input(s,3);
output(s,3);
}

/******************** Output *******************


[root@localhost structure]# ./a.out
1 ITEM CODE:101
ITEM NAME:abc
ITEM PRICE:5
ITEM QTY:5

2 ITEM CODE:102
ITEM NAME:xyz
ITEM PRICE:12
ITEM QTY:10

3 ITEM CODE:103
ITEM NAME:pqr
ITEM PRICE:13
ITEM QTY:10

1 ITEM CODE:101
ITEM NAME:abc
ITEM PRICE:5
ITEM QTY:5
ITEM COST:25.000000

2 ITEM CODE:102
ITEM NAME:xyz
ITEM PRICE:12
ITEM QTY:10
ITEM COST:120.000000

3 ITEM CODE:103
ITEM NAME:pqr
ITEM PRICE:13
ITEM QTY:10
ITEM COST:130.000000
*************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:28.
Assignment Name:Write a program to accept two filenames. Copy the contents of the
first file to the second such that the case of all alphabets is
reversed.
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading \n");


scanf("%s", filename);

fptr1 = fopen(filename, "r");


if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);

fptr2 = fopen(filename, "w");


if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

c = fgetc(fptr1);
while (c != EOF)
{
if(islower(c))
{
c=toupper(c);
fputc(c, fptr2);
c = fgetc(fptr1);
}
else if(isupper(c))
{
c=tolower(c);
fputc(c, fptr2);
c = fgetc(fptr1);
}
else
{
fputc(c,fptr2);
c=fgetc(fptr1);
}
}

printf("\nContents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);
return 0;
}

/****************** Output ****************


[rsp@localhost File]$ ./a.out
Enter the filename to open for reading
1.c
Enter the filename to open for writing
demo.txt
Contents copied to demo.txt
******************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:29.
Assignment Name:Write a C program to accept a filename and count the number of
words, lines and characters in thefile.
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *fptr;
char ch;
int wrd=1,charctr=1;
char fname[20];
printf("\n\n Count the number of words and characters in a file :\n");
printf("---------------------------------------------------------\n");
printf(" Input the filename to be opened : ");
scanf("%s",fname);

fptr=fopen(fname,"r");
if(fptr==NULL)
{
printf(" File does not exist or can not be opened.");
}
else
{
ch=fgetc(fptr);
printf(" The content of the file %s are : ",fname);
while(ch!=EOF)
{
printf("%c",ch);
if(ch==' '||ch=='\n')
{
wrd++;
}
else
{
charctr++;
}
ch=fgetc(fptr);
}
printf("\n The number of words in the file %s are : %d\n",fname,wrd-2);
printf(" The number of characters in the file %s are : %d\n\
n",fname,charctr-1);
}
fclose(fptr);
}

/************************ Output ************************


[rsp@localhost File]$ ./a.out
Count the number of words and characters in a file :
---------------------------------------------------------
Input the filename to be opened : 1.c
The content of the file 1.c are : //Write a program to accept a file name and a single
character. Display the count indicating total number of times character occurs in the file
#include <stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE* fp;
int count = 0;
char filename[MAX_FILE_NAME];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not open file %s",filename);
return 0;
}
for (c = getc(fp); c != EOF; c = getc(fp))
count = count + 1;
fclose(fp);
printf("The file %s has %d characters\n ",filename, count);
return 0;
}

[rsp@localhost File]$ ./a.out


Enter file name: 1.c
The file 1.c has 614 characters

The number of words in the file 1.c are : 137


The number of characters in the file 1.c are : 570
**************************************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:30.
Assignment Name:Write a program which accepts a filename and an integer as
command line arguments and encrypts the file using the key. (Use
any encryption algorithm).
**************************************************************************************/

#include<stdio.h>
int main()
{
char fname[20], ch;
FILE *fps, *fpt;
printf("Enter Filename: ");
gets(fname);
fps = fopen(fname, "r");
if(fps == NULL)
return 0;
fpt = fopen("temp.txt", "w");
if(fpt == NULL)
return 0;
ch = fgetc(fps);
while(ch != EOF)
{
ch = ch+100;
fputc(ch, fpt);
ch = fgetc(fps);
}
fclose(fps);
fclose(fpt);
fps = fopen(fname, "w");
if(fps == NULL)
return 0;
fpt = fopen("temp.txt", "r");
if(fpt == NULL)
return 0;
ch = fgetc(fpt);
while(ch != EOF)
{
ch = fputc(ch, fps);
ch = fgetc(fpt);
}
fclose(fps);
fclose(fpt);
printf("\nFile %s Encrypted Successfully!", fname);
return 0;
}
/***************** Output ***************
[rsp@localhost File]$ ./a.out
Enter Filename: demo.txt
File demo.txt Encrypted Successfully!
*****************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:31.
Assignment Name:Write a program to compare two files character by character and
check whether they are same or not?
**************************************************************************************/

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[40], fname2[40];

printf("Enter name of first file :");


gets(fname1);

printf("Enter name of second file:");


gets(fname2);

fp1 = fopen(fname1, "r");


fp2 = fopen(fname2, "r");

if (fp1 == NULL)
{
printf("Cannot open %s for reading ", fname1);
exit(1);
}
else if (fp2 == NULL)
{
printf("Cannot open %s for reading ", fname2);
exit(1);
}
else
{
ch1 = getc(fp1);
ch2 = getc(fp2);

while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2))


{
ch1 = getc(fp1);
ch2 = getc(fp2);
}

if (ch1 == ch2)
printf("Files are identical n");
else if (ch1 != ch2)
printf("Files are Not identical n");

fclose(fp1);
fclose(fp2);
}
return (0);
}

/********************* Output **********************


[rsp@localhost File]$ ./a.out
Enter name of first file :1.c
Enter name of second file:1.c
Files are identical n[rsp@localhost File]$ ./a.out
Enter name of first file :1.c
Enter name of second file:2.c
Files are Not identical
**************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:32.
Assignment Name:Write a program two read text files and perform the following
actions till user selects exit.
i. Concatenate two files and save in third file.
ii. Merge the files.
iii. exit
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main()
{

FILE *fp1,*fp2,*fp3;
char c;
char fname1[40], fname2[40],fname3[40];
int ch;

printf("Enter name of first file :");


gets(fname1);

printf("Enter name of second file:");


gets(fname2);

printf("Enter name of third file :");


gets(fname3);

for(;;)
{
printf("\n\n**FILE OPERATION\n\n");
printf("\n1. Concatenate two files and save in third file.\n2. Merge the
files. 3. exit");
printf("\n\nEnter UR choice\n\n");
scanf("%d",&ch);

switch(ch)
{
case 1:fp1=fopen(fname1,"r");
fp2=fopen(fname2,"r");
fp3=fopen(fname3,"a");

if (fp1 == NULL)
{
puts("Could not open file1");
exit(0);
}
if(fp2 == NULL)
{
puts("Could not open file2");
exit(0);
}
if(fp3 == NULL)
{
puts("Could not open file3");
exit(0);
}

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


fputc(c, fp3);

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


fputc(c, fp3);

printf("Merged file1 and file2 into file3");


fcloseall();
break;
case 2:/*printf("Enter name of first file :");
gets(fname1);
printf("Enter name of second file:");
gets(fname2);
*/
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"a");

if (fp1 == NULL)
{
puts("Could not open file1");
exit(0);
}
if(fp2 == NULL)
{
puts("Could not open file2");
exit(0);
}

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


fputc(c, fp2);

printf("Merged file1 into file2");


fcloseall();
break;
case 3:exit(0);
}
}
fcloseall();
return 0;
}

/********************** Output ******************


[rsp@localhost File]$ ./a.out
Enter name of first file :file1.txt
Enter name of second file:file2.txt
Enter name of third file :file3.txt
FILE OPERATION
1. Concatenate two files and save in third file.
2. Merge the files.
3. exit
Enter UR choice: 1
Merged file1 and file2 into file3
FILE OPERATION
1. Concatenate two files and save in third file.
2. Merge the files.
3. exit
Enter UR choice: 2
Merged file1 into file2
FILE OPERATION
1. Concatenate two files and save in third file.
2. Merge the files. 3. exit
Enter UR choice: 3
***************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:33.
Assignment Name:Write aC program to find area and perimeter of circle using macro.
**************************************************************************************/

#include <stdio.h>

#define PI 3.14f

int main()
{
float rad,area, perm;

printf("Enter radius of circle: ");


scanf("%f",&rad);

area=PI*rad*rad;
perm=2*PI*rad;

printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perm);


return 0;
}

/*************************Output************************
[rsp@localhost command_processor]$ ./a.out
Enter radius of circle: 2.3
Area of circle: 16.610600
Perimeter of circle: 14.444000
********************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:34.
Assignment Name:Define a macro MIN which gives the minimum of two numbers.
Usethis macro to find the minimum of numbers.
**************************************************************************************/

#include <stdio.h>

#define MIN(x,y) ((x<y)?x:y)

int main()
{
int a,b,min;

printf("Enter first number: ");


scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);

min=MIN(a,b);
printf("Minimum number is: %d\n",min);

return 0;
}

/************************* Output ************************


[rsp@localhost command_processor]$ ./a.out
Enter first number: 12
Enter second number: 13
Minimum number is: 12
********************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:35.
Assignment Name:Write a program to display the command line arguments in reverse
order.
**************************************************************************************/

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int i;
for(i=argc-1;i>0;i--)
printf("\n Argument [%d] is : %s",i,argv[i]);
return 0;
}

/************************Output************************
p@localhost command_processor]$ ./a.out
1 str 1.2
Argument [3] is : 1.2
Argument [2] is : str
Argument [1] is : 1
********************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:36.
Assignment Name:Write a program to accept three integers as command line
arguments and find the minimum, maximum and average of the
three. Display error message if invalid number of arguments are
entered.
**************************************************************************************/

#include<stdio.h>
#include<stdlib.h>

void main(int argc , char * argv[])


{
int i,sum=0;
float avg;
int max;
int min;

if(argc!=4)
{
printf("you have forgot to type numbers.");
exit(1);
}
printf("\n\nargc count=%d",argc);
for(i=1;i<argc;i++)
{
printf("\n\nargv[%d]=%d",i,atoi(argv[i]));
sum = sum + atoi(argv[i]);
}
printf("\n\nSum=%d",sum);
avg=(float)sum/3;
printf("\n\nThe average is %f",avg);

max=atoi(argv[1]);
min=atoi(argv[1]);
for(i=1;i<argc;i++)
{
if(atoi(argv[i])>max)
{
max=atoi(argv[i]);
}

if(atoi(argv[i])<min)
{
min=atoi(argv[i]);
}
}
printf("\n the maximum number is %d",max);
printf("\n the minimum number is %d",min);
}

/*************************** Output *************************


[rsp@localhost command_processor]$ ./a.out
15 12 89
argc count=4
argv[1]=15
argv[2]=12
argv[3]=89
*************************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:37.
Assignment Name:Write a program which accepts a string and two characters as
command line arguments and replace all occurrences of the first
character by the second.
**************************************************************************************/

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
char str[100], ch, Newch;
int i;

if(argc!=4)
{
printf("\nInsufficient arguments");
exit(0);
}
else
{
ch=argv[2][0];
Newch=argv[3][0];
//printf("\nargv[2]=%c\nch=%c",argv[2],ch);
// printf("\nargv[3]=%c\nNewch=%c",argv[3],Newch);
for(i = 0; i <= strlen(argv[1]); i++)
{
if(argv[1][i] == ch)
{
argv[1][i] = Newch;
}
}

printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s
", ch, Newch, argv[1]);

return 0;
}
}

/*********************************** Output ************************************


[rsp@localhost command_processor]$ cc 5.c
[rsp@localhost command_processor]$ ./a.out
geeksforgeeks e f

The Final String after Replacing All Occurrences of 'e' with 'f' = gffksforgffks
********************************************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:38.
Assignment Name:Write a program which accepts a two integers as command line
arguments. If second number is 1 then display factorial of first
number and if second number is 2 the check whether first number
is even or odd. If the user enters invalid number of arguments,
display appropriate message.
**************************************************************************************/

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int main(int argc, char *argv[])


{
int num, fact,n;

if(argc != 3) {
printf("Invalid Usage.\n\n");
exit(0);
}

printf("\n\nargv[1]=%d\n\nargv[2]=%d",atoi(argv[1]),atoi(argv[2]));

if(atoi(argv[1])<0)
{
printf("Error: Factorial of negative number doesn't exist.");
return 1;
}

num=atoi(argv[1]);
if(atoi(argv[2])==3)
{
fact = 1;

while(num>=1)
{
fact = atoi(argv[2]) * num;
num--;
}
printf("%d\n", fact);
}
else if(atoi(argv[2])==2)
{
if(num%2==0)
printf("\n%d is even",num);
else
printf("\n%d is odd",num);
}

return 0;
}

/********************** Output ***********************


[rsp@localhost command_processor]$ ./a.out
62
argv[1]=6
argv[2]=2
6 is even

[rsp@localhost command_processor]$ ./a.out


52
argv[1]=5
argv[2]=2
5 is odd*
*******************************************************/
/**************************************************************************************
Name:Harsh Sandip Jadhav Roll No.: 3066
Class: F.Y.B.Sc.Comp.Sci. Batch: C
Assignment No.:39.
Assignment Name:Write aprogram to file copy using command line arguments.
**************************************************************************************/

//file3.txt

line 1
line 2
line 3

//filecopy.c

#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);
}
printf("File Copied Successfully ....");
fclose(fs);
fclose(ft);
return 0;
}
//filecopy.txt

line 1
line 2
line 3

/************************** Output ************************


[rsp@localhost command_processor]$ cc 8.c
[rsp@localhost command_processor]$ ./a.out
file3.txt filecopy.txt
File Copied Successfully ....
*********************************************************/

You might also like