Pcps
Pcps
1
PREPARED BY JYOTHI DHULIPALLA
2. Write a C program to convert Celsius to Fahrenheit and vice versa.
AIM:To convert Celsius to Fahrenheit and vice versa.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float f, c;
clrscr();
printf("\n Enter Temperature in Celsius: ");
scanf("%f",&c);
f = c * 1.8 +32;
printf(“Temperature in Fahrenheit is : %f”,f);
printf("\n Enter Temperature in Fahrenheit: ");
scanf("%f",&f);
c=(f-32)/1.8;
printf(" Temperature in Celsius: %f",c);
getch();
}
Output
Enter Temperature in Celsius: 37
Temperature in Fahrenheit is : 98.600000
Enter Temperature in Fahrenheit: 98
Temperature in Celsius: 36.666668
Results: Celsius to Fahrenheit and vice versa converted successfully.
2
PREPARED BY JYOTHI DHULIPALLA
3. Write a C Program to Find Whether the given Year is a Leap Year or not.
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
Output:
Enter a year: 2012
2012 is a leap year
Results: Successfully computed the given year is leap year or not.
3
PREPARED BY JYOTHI DHULIPALLA
4. Write a C Program to add digits and multiplication of a number.
4
PREPARED BY JYOTHI DHULIPALLA
5. Write a C Program to find whether the given numbers is
a. Prime Number b. Armstrong Number
Program:
#include <stdio.h>
int main( )
{
inti,n,c;
printf(“enter a number:”);
scanf(“%d”,&n);
for(c=0,i=1;i<=n;i++)
if(n%i==0)
c++;
if (c == 2)
printf(“%d is a prime number”,n);
else
printf(“%d is not a prime number”,n);
return 0;
}
Output:
Enter a number: 97
97 is a prime number
Result: Successfully computed the given number is prime or not.
5
PREPARED BY JYOTHI DHULIPALLA
Enter the value 153
153 is Armstrong
Result: Successfully computed the given number is Armstrong number or not.
6
PREPARED BY JYOTHI DHULIPALLA
6. Write a C Program to print Pascal Triangle.
Aim: To print Pascal Triangle
Program:
#include<stdio.h>
void main()
{
int p=1,y=0,i,r,x;
printf("\n Rows you want to input: ");
scanf("%d",&r);
printf("\n Pascal's Triangle:\n");
while(y<r)
{
for(i=40-3*y;i>0;i--)
printf(" ");
for(x=0;x<=y;x++)
{
if((x==0)||(y==0))
p=1;
else
p=(p*(y-x+1))/x;
printf("%6d",p);
}
printf("\n");
y++;
}
}
OUTPUT:
Rows you want to input: 6
Pascal's Triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Results: Successfully created the Pascal triangle
7
PREPARED BY JYOTHI DHULIPALLA
7. Write a C Program demonstrating of parameter passing in functions and returning
values.
AIM: To demonstrate parameter passing in functions and returning values
Program:
Call by Value:
#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
OUTPUT:
8
PREPARED BY JYOTHI DHULIPALLA
8. Write a C Program illustrating Fibonacci, Factorial with Recursion without Recursion.
Aim: To write a C Program for finding a Factorial with Recursion and Without Recursion.
Program:
#include<stdio.h>
long int fact_rec(int);
long int fact(int);
int main()
{
int n;
printf("Enter a Value to find the Factorial: ");
scanf("%d",&n);
printf("\n Factorial(Using Recursion) of %d is: %ld",n,fact_rec(n));
printf("\n Factorial(With out Recursion) of %d is: %ld",n,fact(n));
return 0;
}
long int fact_rec(int a)
{
if(a==1||a==0)
return 1;
else
return a*fact_rec(a-1);
}
long int fact(int a)
{
inti;
long int f;
if(a==1||a==0)
f=1;
else
for(f=1,i=1;i<=a;i++)
f=f*i;
return f;
}
OUTPUT:
Enter a Value to find the Factorial: 9
Factorial(Using Recursion) of 9 is: 362880
Factorial(With out Recursion) of 9 is: 362880
Result: Successfully created a C program for a Factorial with recursion and without recursion.
AIM: To write a C Program for finding a Fibonacci with Recursion and Without Recursion.
Program:
#include<stdio.h>
int Fibonacci(int);
void fibUsingGoto(int);
int main()
9
PREPARED BY JYOTHI DHULIPALLA
{
int n, i = 0, c;
printf("\n Enter value of fibanacci series:");
scanf("%d",&n);
printf("\nFibonacci series with recursion\n");
for ( c = 1 ; c <= n ; c++ )
{
printf(" %d", Fibonacci(i));
i++;
}
printf("\n Fibonacci series without recursion\n");
fibUsingGoto(n);
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
void fibUsingGoto(int n)
{
int a = 0, b = 1, sum = 0;
lableFib: if(n != 0) {
printf(" %d", a);
sum = a + b;
a = b;
b = sum;
n--;
gotolableFib;
}
}
OUTPUT:
Enter value of fibanacci series:9
Fibonacci series with recursion
0 1 1 2 3 5 8 13 21
Fibonacci series without recursion
0 1 1 2 3 5 8 13 21
RESULT:
Successfully created a C program for a FIBONACCI with recursion and without recursion.
10
PREPARED BY JYOTHI DHULIPALLA
9. Write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide
using Switch case.
AIM:To write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide
using Switch case.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,res,ch;
clrscr();
printf("\t *********************");
printf("\n\tMENU\n");
printf("\t********************");
printf("\n\t(1)ADDITION");
printf("\n\t(2)SUBTRACTION");
printf("\n\t(3)MULTIPLICATION");
printf("\n\t(4)DIVISION");
printf("\n\t(5)REMAINDER");
printf("\n\t(0)EXIT");
printf("\n\t********************");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
if(ch<=5 &ch>0)
{
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
}
switch(ch)
{
case 1: res=a+b;
printf("\n Addition:%d",res);
break;
case 2: res=a-b;
printf("\n Subtraction:%d",res);
break;
case 3: res=a*b;
printf("\n Multiplication:%d",res);
break;
case 4: res=a/b;
printf("\n Division:%d",res);
break;
case 5: res=a%b;
printf("\n Remainder:%d",res);
break;
case 0: printf("\n Choice Terminated");
exit();
break;
11
PREPARED BY JYOTHI DHULIPALLA
default: printf("\n Invalid Choice");
}
getch();
}
Output:
*********************
MENU
********************
(1)ADDITION
(2)SUBTRACTION
(3)MULTIPLICATION
(4)DIVISION
(5)REMAINDER
(0)EXIT
********************
Enter your choice:1
Enter two numbers:
34
Addition:7
Result: Successfully created a C Program to make a simple Calculator to Add, Subtract,
Multiply or Divide using Switch case.
12
PREPARED BY JYOTHI DHULIPALLA
10. Write a C Program to convert decimal to binary and hex (using switch call function the
function).
Aim:To write a C Program to convert decimal to binary and hex (using switch call function
the function).
Program:
/* Decimal to binary conversion */
#include<stdio.h> // include stdio.h library
void convert_to_x_base(int, int);
int main(void)
{
int num, choice, base;
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Decimal to hexadecimal. \n");
printf("4. Exit. \n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
base = 2;
break;
case 2:
base = 8;
break;
case 3:
base = 16;
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
printf("Enter a number: ");
scanf("%d", &num);
printf("Result = ");
convert_to_x_base(num, base);
printf("\n\n");
}
return 0; // return 0 to operating system
}
13
PREPARED BY JYOTHI DHULIPALLA
void convert_to_x_base(int num, int base)
{
int rem;
// base condition
if (num == 0)
{
return;
}
else
{
rem = num % base; // get the rightmost digit
convert_to_x_base(num/base, base); // recursive call
if(base == 16 && rem >= 10)
{
printf("%c", rem+55);
}
else
{
printf("%d", rem);
}
}
}
OUTPUT:
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
Enter your choice: 1
Enter a number: 8
Result = 1000
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
14
PREPARED BY JYOTHI DHULIPALLA
3. Decimal to hexadecimal.
4. Exit.
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
15
PREPARED BY JYOTHI DHULIPALLA
11. Write a C Program to compute the values of sin x, cos x and e^x values using series
expansion.
Aim: To write a C program to compute the value of sin x
Program:
/* program to find the value of sin x */
#include<stdio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i, n;
printf("enter the no of terms:\n");
scanf("%d",&n);
printf("enter the angle in degrees x: \n");
scanf("%f",&x);
x=(x*3.14)/180;
sum=x;
term=x;
for(i=1;i<=n;i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i+1));
sum+=term;
}
printf("sin valve of given angle is %f",sum);
}
Output:
Program:
/* program to find the value of cos x */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,sum,term;
int i,n;
clrscr();
printf("enter the no of terms:\n");
scanf("%d",&n);
printf("enter the angle in degrees x :\n");
16
PREPARED BY JYOTHI DHULIPALLA
scanf("%f",&x);
x=(x*3.14)/180;
sum=1;
term=1;
for(i=1;i<=n;i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i-1));
sum+=term;
}
printf("cos valve of given angle is %f",sum);
getch();
}
Output:
enter the no of terms: 3
enter the angle in degrees x: 30
cos valve of given angle is 0.866158
Result: Successfully created a C program to compute the value of cos x
Program:
Program:
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
OUTPUT:
Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
Program:
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elements");
scanf("%d", &n);
printf("Enter %d Numbers", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
19
PREPARED BY JYOTHI DHULIPALLA
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:");
for(i = 0; i < n; i++)
printf("%5d", a[i]);
return 0;
}
Output:
Enter number of elements5
Enter 5 Numbers89
65
90
43
76
Sorted Array: 43 65 76 89 90
Result: Successfully created a c program for selection sort using arrays
Program:
#include<stdio.h>
#include<stdlib.h>
void add(int m[3][3], int n[3][3], int sum[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
sum[i][j] = m[i][j] + n[i][j];
}
void subtract(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
result[i][j] = m[i][j] - n[i][j];
}
void multiply(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
result[i][j] = 0;
for (int k = 0; k < 3; k++)
result[i][j] += m[i][k] * n[k][j];
}
20
PREPARED BY JYOTHI DHULIPALLA
}
}
void transpose(int matrix[3][3], int trans[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
trans[i][j] = matrix[j][i];
}
void display(int matrix[3][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
printf("%d\t",matrix[i][j]);
printf("\n");
}
}
int main()
{
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];
printf("First Matrix:\n");
display(a);
printf("Second Matrix:\n");
display(b);
int choice;
do
{
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
21
PREPARED BY JYOTHI DHULIPALLA
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);
return 0;
}
OUTPUT:
First Matrix:
5 6 7
8 9 10
3 1 2
Second Matrix:
1 2 3
4 5 6
7 8 9
22
PREPARED BY JYOTHI DHULIPALLA
12 14 16
10 9 11
23
PREPARED BY JYOTHI DHULIPALLA
Choose the matrix operation,
----------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------------
Enter your choice: 5
Thank You.
24
PREPARED BY JYOTHI DHULIPALLA
13. Write a C Program to access elements of an Array using pointer.
PROGRAM:
#include<stdio.h>
int i,l;
int search(int ,int *,int);
int main(){
int n,m;
printf("enter the size of array:");
scanf("%d",&n);
int a[n];
printf("enter the elements:\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("enter the element to be searched:");
scanf("%d",&m);
search(n,a,m);
return 0;
}
int search(int n,int *a,int m){
for(i=0;i<n;i++){
if66==a[i]){
l=1;
break;
}
}
if(l==1){
printf("%d is present in the array",m);
} else {
printf("%d is not present in the array",m);
}
}
OUTPUT:
25
PREPARED BY JYOTHI DHULIPALLA
14. Write a C Program to find the sum of numbers with arrays and pointers.
AIM: To write a C Program to find the sum of numbers with arrays and pointers.
PROGRAM:
#include<stdio.h>
void main() {
int numArray[20];
int i,n, sum = 0;
int *ptr;
printf("\n Enter array size");
scanf("%d",&n);
printf("\nEnter elements : ");
for (i = 0; i < n; i++)
scanf("%d", &numArray[i]);
ptr = numArray;
for (i = 0; i < n; i++) {
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
}
OUTPUT:
Enter elements : 5 7 8 9 10
The sum of array elements : 39
RESULT:
Successfully created a C Program to find the sum of numbers with arrays and pointers
26
PREPARED BY JYOTHI DHULIPALLA
15. Write a C Program to store information of a movie using structure.
PROGRAM:
#include <stdio.h>
struct movie
{
char movieName[50];
int roll;
int year;
char director[50];
float rating;
};
int main()
{
int i,n;
struct movie s[10];
printf("\n Enter no.of movies");
scanf("%d", &n);
printf("Enter information of movie\n");
for (i = 0; i <n;i++) {
s[i].roll = i + 1;
printf("\nFor movie number%d,\n", s[i].roll);
printf("Enter movie name: ");
scanf("%s",s[i].movieName);
printf("Enter year: ");
scanf("%d",&s[i].year);
printf("\n Enter director name");
scanf("%s",s[i].director );
printf("Enter rating:");
scanf("%f",&s[i].rating);
}
printf("\nDisplaying Information:\n");
for (i = 0; i <n; i++) {
printf("\nMovie number: %d\n",s[i].roll);
printf("Movie name: %s \n",s[i].movieName);
printf("Year: %d\n", s[i].year);
printf("Director name: %s \n",s[i].director);
printf("Rating of Movie: %f",s[i].rating);
}
return 0;
}
OUTPUT:
Enter no.of movies2
Enter information of movie
27
PREPARED BY JYOTHI DHULIPALLA
For movie number1,
Enter movie name: titanic
Enter year: 1997
Enter director namejames
Enter rating:4.6
For movie number2,
Enter movie name: Aavatar
Enter year: 2009
Enter director namecameroon
Enter rating:4.75
Displaying Information:
Movie number: 1
Movie name: titanic
Year: 1997
Director name: james
Rating of Movie: 4.600000
Movie number: 2
Movie name: Aavatar
Year: 2009
Director name: cameroon
Rating of Movie: 4.750000
RESULT: Succssfully created a C Program to store information of a movie using structure.
28
PREPARED BY JYOTHI DHULIPALLA
16. Write a C Program to store information using structures with dynamically memory
allocation.
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
struct course
{
int marks;
char subject[30];
};
int main()
{
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records: ");
scanf("%d", &noOfRecords);
ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));
for(i = 0; i < noOfRecords; ++i)
{
printf("Enter name of the subject and marks respectively:\n");
scanf("%s %d", &(ptr+i)->subject, &(ptr+i)->marks);
}
printf("Displaying Information:\n");
for(i = 0; i < noOfRecords ; ++i)
printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks);
return 0;
}
OUTPUT:
Enter number of records: 2
Enter name of the subject and marks respectively:
pcps 67
Enter name of the subject and marks respectively:
pcpslab 24
Displaying Information:
pcps 67
pcpslab 24
RESULT:
Successfully created a C Program to store information using structures with dynamically
memory allocation.
29
PREPARED BY JYOTHI DHULIPALLA
17. Implementation of String manipulation operations with library function.
a. Copy b. Concatenate c. length d. compare
PROGRAM:
#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:Length of String");
printf("\n2:Concatenate Strings");
printf("\n3:Copy String ");
printf("\n4:Compare Strings");
printf("\n5: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("\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 3:
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);
30
PREPARED BY JYOTHI DHULIPALLA
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 4:
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 5:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.\n\n ");
}
}while(ch!=5);
return 0;
}
OUTPUT:
MENU
------------------------------
1:Length of String
2:Concatenate Strings
3:Copy String
4:Compare Strings
5:Exit
------------------------------
MENU
------------------------------
1:Length of String
2:Concatenate Strings
31
PREPARED BY JYOTHI DHULIPALLA
3:Copy String
4:Compare Strings
5:Exit
------------------------------
MENU
------------------------------
1:Length of String
2:Concatenate Strings
3:Copy String
4:Compare Strings
5:Exit
------------------------------
MENU
------------------------------
1:Length of String
32
PREPARED BY JYOTHI DHULIPALLA
2:Concatenate Strings
3:Copy String
4:Compare Strings
5:Exit
------------------------------
MENU
------------------------------
1:Length of String
2:Concatenate Strings
3:Copy String
4:Compare Strings
5:Exit
------------------------------
RESULT:
33
PREPARED BY JYOTHI DHULIPALLA
18. Implementation of String manipulation operations without library function.
a. Copy b. Concatenate c. length d. compare
PROGRAM:
#include<stdio.h>
void main()
{
char arr[30],s1[10],s2[10],s3[10];
int opt,i=0,j,len=0;
printf("Enter any option\n\n");
printf("1:length of the string\n");
printf("2:Concatenate of the two string\n");
printf("3:String Compare\n");
printf("4:Copy of the string\n");
printf("Enter the choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
{
printf("Enter any string \n");
scanf("%s",arr);
for(i=0;arr[i]!='\0';i++);
printf("The length of the string %d",i);
break;
}
case 2:
{
printf(" String Concatenation \n");
printf("\nEnter the First string:\n");
scanf("%s", s1);
printf("\nEnter Second string");
scanf("%s",s2);
for(i=0;s1[i]!='\0';i++)
{
s3[i]=s1[i];
}
s3[i]='\0';
for(j=0;j<=i;j++)
{
s3[i+j]=s2[j];
}
printf("The Concatenated string is %s",s3);
break;
34
PREPARED BY JYOTHI DHULIPALLA
case 3:
{
printf("\n Please Enter the First String : ");
scanf("%s",s1);
break;
}
case 4:
{
printf(" String copying \n");
printf("Enter 1st string :");
scanf("%s",s1);
printf("Enter 2st string :");
scanf("%s",s2);
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
printf("%s",s1);
break;
}
default:
{
printf("Not is valid Option........");
}
}
}
}
35
PREPARED BY JYOTHI DHULIPALLA
OUTPUT:
Enter any option
36
PREPARED BY JYOTHI DHULIPALLA
String copying
Enter 1st string :qiscet
Enter 2st string :qisit
qisit
37
PREPARED BY JYOTHI DHULIPALLA
19. Write a C Programming code to open a file and print it contents on screen.
AIM: To write a C program to open a file and print its contents on screen
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
OUTPUT:
Enter the filename to open
a.txt
/*Contents of a.txt*/
RESULT:
Successfully created a C program to open a file and print its contents on the screen
38
PREPARED BY JYOTHI DHULIPALLA
20. Write a C Program merges two files and stores their contents in another file.
AIM: To write a C program which merges two files and stores their contents in another file
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
OUTPUT:
Merged file1.txt and file2.txt into file3.txt
RESULT:
Successfully created a C program which merges two files and stores their contents in
another file
39
PREPARED BY JYOTHI DHULIPALLA