Final C Programs
Final C Programs
#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;
}
#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);
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i;
double *data;
printf("Enter the total number of elements: ");
scanf("%d", &n);
#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
#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;
}
printf("Sum=%d",sum);
}
//Dellocating memory of matrix
for(i=0; i<m; i++)
{
free(a[i]);
}
free(a);
return 0;
}
#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;
#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);
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;
}
#include<stdio.h>
int main()
{
int num, rem;
int *pnum;
pnum= #
rem= *pnum % 2;
if(rem==0)
printf("%d is even.\n", *pnum);
else
printf("%d is odd.\n", *pnum);
return 0;
}
#include<stdio.h>
int main()
{
float x, y, z;
float *px, *py, *pz;
px=&x, py=&y, pz=&z;
return 0;
}
#include<stdio.h>
#define MAX 30
int main()
{
int size, i, arr[MAX];
int *ptr;
ptr = &arr[0];
return 0;
}
#include <stdio.h>
main()
{
int array[100], *minimum, size, c, location = 1;
perimeter).****************************************************************************
**********/
#include<stdio.h>
int main()
{
float radius, area, perimeter;
return 0;
}
#include <stdio.h>
int main()
{
int a[1000],i,n,sum;
/**************************************************************************************
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).
**************************************************************************************/
#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;
}
#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);
}
#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;
return(0);
}
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void main()
{
char str[100];
printf("\n Enter any sentence:-");
fgets(str,100,stdin);
Stral(str);
}
/**************************************************************************************
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;
}
#include<stdio.h>
#include <string.h>
int main()
{
int i, j;
char str[10][50], temp[50];
#include <stdio.h>
#include <string.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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;
}
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;
}
#include<stdio.h>
// Prototype Declaration
int FindLength(char str[]);
int main()
{
char str[100];
int length;
length = FindLength(str);
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int i;
#include <stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("\nEnter the string\n");
gets(str);
printf("\nThe reverse of given string is %s",strrev(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;
}
#include <stdio.h>
int main()
{
char str[1000], ch;
int count = 0,i;
#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;
//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);
printf("\n");
}
Employee 1:-
Name: rahul
Id: 101
Salary: 50000
Employee 2:-
Name: pawar
Id: 102
Salary: 60000
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];
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 main()
{
int i;
input(s,3);
output(s,3);
}
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;
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);
}
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
#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);
}
#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];
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);
if (ch1 == ch2)
printf("Files are identical n");
else if (ch1 != ch2)
printf("Files are Not identical n");
fclose(fp1);
fclose(fp2);
}
return (0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char c;
char fname1[40], fname2[40],fname3[40];
int ch;
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);
}
if (fp1 == NULL)
{
puts("Could not open file1");
exit(0);
}
if(fp2 == NULL)
{
puts("Could not open file2");
exit(0);
}
#include <stdio.h>
#define PI 3.14f
int main()
{
float rad,area, perm;
area=PI*rad*rad;
perm=2*PI*rad;
/*************************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>
int main()
{
int a,b,min;
min=MIN(a,b);
printf("Minimum number is: %d\n",min);
return 0;
}
#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>
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);
}
#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;
}
}
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>
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;
}
//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