Ticker

6/recent/ticker-posts

Advertisement

Responsive Advertisement

Try some basic Questions of C programming language

 

Try some basic Questions of C programming language




PRIGRAM 1


 AIM: Write a program to print HELLO FRIENDS!

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

printf(“HELLO FRIENDS!”);

getch();

}

OUTPUT:

HELLO FRIENDS!

PRIGRAM 2

AIM: Write a program that reads two nos. from key board and gives their addition, 

subtraction, multiplication, division and modulo.

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int a,b,add,sub,mul,div,mod;

printf(“Enter two numbers”);

scanf(“%d%d”,&a,&b);

add=a+b;

sub=a-b;

mul=a*b;

div=a/b;

mod=a%b;

printf(“Addition = %d \n Subtraction = %d\n Multiplication = %d\n Division = %d\n 

Modulas = %d”,add,sub,mul,div,mod);

getch();

}

OUTPUT

Enter two numbers

4

2

Addition = 6

Subtraction = 2

Multiplication = 8

Division = 2

Modulas = 0

PROGRAM 3

AIM: Write a program to calculate area of circle,use Ω as symbolic constants.

#include<stdio.h>

#include<conio.h>

#define PI 3.14

void main()

{

clrscr();

int r;

float area;

printf(“Enter Radios”);

scanf(“%d”,&r);

area=PI*r*r;

printf(“Area of Circle = %f”,area);

getch();

}

OUTPUT

Enter Radios

2

Area of Circle = 12.56

PROGRAM 4

AIM: Write a program to convert days into months and days.

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int days,month,extra_days;

printf(“Enter total number of days”);

scanf(“%d”,&days);

month=days/30;

extra_days=days%30;

printf(“Total number of months = %d\n Total number of Extra days = %d”, month, 

extra_days);

getch();

}

OUTPUT

Enter total number of days

85

Total number of months = 2

Total number of Extra days = 25

PROGRAM 5

AIM: Write a program which calculates the summation of three digits from the given 3 

digit number.

#include<stdio.h>

#include<conio.h>

void main(){

clrscr();

int a,b,c,d,e,f;

printf(“Enter 3 digit number”);

scanf(“%d”,&a);

b=a/10;

c=a%10;

d=b/10;

e=b%10;

f=c+d+e;

printf(“Summation of 3 digits = %d”,f);

getch();

}

OUTPUT

Enter 3 digit number

120

Summation of 3 digits = 3

PROGRAM 6

AIM: Write a program to demonstrate enumerates data type.

#include <stdio.h>

#include<conio.h>

enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};

void main()

{

enum week today;

today=wednesday;

printf("%d day",today+1);

getch();

}

OUTPUT

4

PROGRAM 7

AIM: Write a program to compute Fahrenheit from centigrade.

#include <stdio.h>

#include <conio.h>

void main()

{

 float celsius, fahrenheit;

 printf("Enter temperature in Fahrenheit: ");

 scanf("%f", &fahrenheit);

 celsius = (fahrenheit - 32) * 5 / 9;

 printf("Fahrenheit = %f Celsius", celsius);

 getch();

}

OUTPUT

Enter temperature in Fahrenheit: 205

Fahrenheit = 96.11 Celsius

PROGRAM 8

AIM: Write a program to calculate simple interest.

#include <stdio.h>

#include <conio.h>

void main()

{

 float principle, time, rate, SI;

 printf("Enter principle (amount): ");

 scanf("%f", &principle);

 printf("Enter time: ");

 scanf("%f", &time);

 printf("Enter rate: ");

 scanf("%f", &rate);

 SI = (principle * time * rate) / 100;

 printf("Simple Interest = %f", SI);

 getch();

}

OUTPUT

Enter principle (amount): 1200

Enter time: 2

Enter rate: 5.4

Simple Interest = 129.600006

PROGRAM 9

AIM: Read the price of item in decimal form e.g. 12.50 and separate Rs and Paise from 

the given value e.g. 12 rupees and 50 paise.

#include<stdio.h>

#include<conio.h>

void main()

{

float price,rs,paise,z;

clrscr();

printf("Enter price of an item:");

scanf("%f",&paise);

z=price*100; //Converting price in paise..

rs=z/100; //Dividing that paisa by 100 will give Rupees.

paisa=z%100; //Modulo by 1oo will give paise.

printf("Rupees:%f",rs);

printf("Paise:%f",paise);

getch();

}

OUTPUT

Enter price of an item: 12.50

Rupees: 12.00

Paise: 0.50

PROGRAM 1

AIM: Write a program to find the largest of the three nos. using Nested-If-Else 

statement.

#include <stdio.h>

#include<conio.h>

void main()

{

 int n1, n2, n3;

 printf("Enter three numbers: ");

 scanf("%d %d %d", &n1, &n2, &n3);

 if (n1>=n2)

 {

 if(n1>=n3)

 printf("%d is the largest number.", n1);

 else

 printf("%d is the largest number.", n3);

 }

 else

 {

 if(n2>=n3)

 printf("%d is the largest number.", n2);

 else

 printf("%d is the largest number.",n3);

 }

 getch();

}

OUTPUT

Enter three numbers:

5

6

9

9 is the largest number.

PROGRAM 2

AIM: Write a C program to enter a character and to check whether it is a small letter 

or it is a capital letter or it is a digit or it is a special symbol.

#include<stdio.h>

#include<conio.h>

void main()

{

char ch;

clrscr();

printf("Enter a character:");

scanf("%c",&ch);

if(ch>=65 && ch<=90)

{

 printf("\n Upper case letter");}

else if(ch>=97 && ch<=122)

{

 printf("\n Lower case letter");

}

else if(ch>=48 && ch<=57)

{

 printf("\n Digit");

}

else if((ch>=0 && ch<=47) || (ch>=58&& ch<=64) || (ch>=91 && ch<=96) || (ch>=123 && 

ch<=127))

{

 printf("\n Special symbol");

}

getch();

}

OUTPUT

Enter a Character: H

Upper case letter

PROGRAM 3

AIM: Write a program to read marks from keyboard and your program should 

display equivalent grade according to following table.

Marks Grade

100-80 Dist

60-79 First Class

35-59 Second Class

0-34 Fail

#include<stdio.h>

#include<conio.h>

void main()

{

int m;

clrscr();

printf("Enter marks:");

scanf("%d",&m);

if(m>=80 && m<=100)

{

 printf("\n Dist");

}

else if(m>=60 && m<=79)

{

 printf("\n First Class");

}

else if(m>=35 && m<=59)

{

 printf("\n Second Class");

}else{

 printf("\n Fail");

}

getch();

}

Output1:

Enter marks: 65

First Class

PROGRAM 4

AIM: Write a program to read marks of a student from keyboard and check whether 

the student is pass (if).

#include<stdio.h>

#include<conio.h>

void main()

{

int m;

clrscr();

printf(“Enter Marks”);

scanf(“%d”,&m);

if(m>=35)

{

printf(“Pass”);

}

else

{

printf(“Fail”);

}

getch();

}

OUTPUT

Enter Marks

50

Pass

PROGRAM 5

AIM: Write a program to find the sum of all odd numbers.

#include <stdio.h>

#include <conio.h>

void main()

{

 int i, n, sum=0;

 printf("Enter upper limit: ");

 scanf("%d", &n);

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

 sum += i;

 }

 printf("Sum of odd numbers = %d", sum);

 getch();

}

OUTPUT

Enter upper limit: 5

9

PROGRAM 6

AIM: Write a program using while loop construct which finds the factorial of a given 

integer number.

#include<stdio.h>

int main()

{

int a,f,i;

printf("Enter a number: ");

scanf("%d",&a);

f=1;

i=1;

while(i<=a)

{

f = f * i;

i++;

}

printf("Factorial: %d",f);

return 0;

}

OUTPUT

Enter a number: 4

Factorial: 24

PROGRAM 7

AIM: Write a C program using do…while and for loop constructs to reverse the digits 

of the number.

#include<stdio.h>

#include<conio.h>

void main()

{

 int num;

 int rev=0, rem=0;

 printf("\n Enter a number: ");

 scanf("%d",&num);

 do

 {

 rem = num%10;

 rev = rev*10+rem; num = num/10;

 }while(num>0);

 printf("\n reverse number = %d",rev);

 getch();

OUTPUT

234

432

PROGRAM 8

AIM: Write a program to demonstrate use of Switch- Break Statement.

#include<stdio.h>

void main( )

{

 int a, b, c, choice;

 while(choice != 3)

 {

 /* Printing the available options */

 printf("\n 1. Press 1 for addition");

 printf("\n 2. Press 2 for subtraction");

 printf("\n Enter your choice");

 /* Taking users input */

 scanf("%d", &choice);

 

 switch(choice)

 {

 case 1:

 printf("Enter 2 numbers");

 scanf("%d%d", &a, &b);

 c = a + b;

 printf("%d", c);

 break;

 case 2:

 printf("Enter 2 numbers");

 scanf("%d%d", &a, &b);

 c = a - b;

 printf("%d", c);

 break;

 default:

 printf("you have passed a wrong key");

 printf("\n press any key to continue");

 }

 } 

OUTPUT

1. Press 1 for addition

2. Press 2 for subtraction

Enter your choice1

Enter 2 numbers

2

3

5

PROGRAM 9

AIM: Write a program to find out all the numbers divisible by 5 and 7 between 1 to 

100.

#include <stdio.h>

int main() {

int x, i;

printf("Input an integer: ");

scanf("%d", &x);

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

{

if((i%x) == 5) {

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

}

}

return 0;

}

PROGRAM 10 

AIM: Check for Armstrong number. A number is Armstrong if sum of cube of every 

digit is same as the original number.

E.g. 153=13+53+33=153

#include <stdio.h>

int main()

{

 int number, originalNumber, remainder, result = 0;

 printf("Enter a three digit integer: ");

 scanf("%d", &number);

 originalNumber = number;

 while (originalNumber != 0)

 {

 remainder = originalNumber%10;

 result += remainder*remainder*remainder;

 originalNumber /= 10;

 }

 if(result == number)

 printf("%d is an Armstrong number.",number);

 else

 printf("%d is not an Armstrong number.",number); return 0;

}

OUTPUT

Enter a three digit integer: 371

371 is an Armstrong number.

PROGRAM 11

AIM: Write a program to print the output of bellow series. 1!+2!+3!+4!+……..n!

#include<stdio.h>

int main()

{

int num,i,j,fact,sum=0;//variables

printf("Enter the last number of series:\n");

scanf("%d",&num);//last number of series

for(i=1;i<=num;i++)//loop for finding factorial and sum

{

fact=1;

if(i!=num)

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

else

printf("%d!= ",i);

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

fact=fact*j;

sum=sum+fact;

}

printf("%d",sum);

return 0;

}

OUTPUT

Enter the last number of series:

10

1!+ 2!+ 3!+ 4!+ 5!+ 6!+ 7!+ 8!+ 9!+ 10!= 4037913

PROGRAM 12

AIM: Write a program to print the following outputs using for Loop.

(a) 1 (b) * 

12 ** 

 123 *** 

(a)

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

clrscr();

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

{

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

{printf("%d",j);

}

printf("\n");

}

getch();

}

OUTPUT

1

12

123

(b)

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,k;

clrscr();

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

{

for(j=5;j>=i;j--)

{

printf(" ");

}

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

{

printf("*");

}

printf("\n");

}

getch();

}

OUTPUT

*

**

***

PROGRAM 13

AIM: Write a program to print the following outputs using for Loop.

(a) 1 (b)321

21 21

 321 1 

PROGRAM 1

AIM: Write a program which sorts 10 numbers into ascending order.

#include <stdio.h> //including stdio.h for printf and other functions

#include <conio.h> //including conio.h for _getch() and other functions

int main() //default function for call

{

int a[10] = { 3,4,7,6,5,1,2,8,10,9 }; //Array declaration size-10

int n = 10; //Temporary number for array size

printf("\n\nArray Data : "); //Printing message

for (int i = 0; i < n; i++) //Loop for displaying the data of array

{

printf(" %d ", a[i]); //Printing data

}

for (int i = 0; i < n; i++) //Loop for ascending ordering

{

for (int j = 0; j < n; j++) //Loop for comparing other values

{

if (a[j] > a[i]) //Comparing other array elements

{

int tmp = a[i]; //Using temporary variable for storing last 

value

a[i] = a[j]; //replacing value

a[j] = tmp; //storing last value

}

}

printf("\n\nAscending : "); //Printing message

for (int i = 0; i < n; i++) //Loop for printing array data after sorting

{

printf(" %d ", a[i]);

}

getch();

}

OUTPUT

Array Data : 3 4 7 6 5 1 2 8 10 9

Ascending : 1 2 3 4 5 6 7 8 9 10

PROGRAM 2

AIM: Write a program to find maximum element from 1-D array.

#include <stdio.h>

int main()

{

 int array[100], maximum, 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]);

 maximum = array[0];

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

 {

 if (array[c] > maximum)

 {

 maximum = array[c];

 location = c+1;

 }

 }

 printf("Maximum element is present at location %d and it's value is %d.\n", location, 

maximum);

 return 0;

}

OUTPUT

Enter the number of elements in an array

5

Enter 5 integers

1

2

3

4

5

Maximum element is present at location 4 and it's value is 5.

PROGRAM 3

AIM: Write a program to find number of odd and even elements from the 1-D array.

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the array

int main()

{

 int arr[MAX_SIZE];

 int i, size, even, odd;

 /* Input size of the array */

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

 scanf("%d", &size);

 /* Input array elements */ printf("Enter %d elements in array: ", size);

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

 {

 scanf("%d", &arr[i]);

 }

 /* Assuming that there are 0 even and odd elements */

 even = 0;

 odd = 0;

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

 {

 /* If the current element of array is even then increment even count */

 if(arr[i]%2 == 0)

 {

 even++;

 }

 else

 {

 odd++;

 }

 }

 printf("Total even elements: %d\n", even);

 printf("Total odd elements: %d", odd);

 return 0;

}

OUTPUT

Enter size of the array: 10

Enter 10 elements in array: 5 6 4 12 19 121 1 7 9 63

Total even elements: 3

Total odd elements: 7

PROGRAM 4

AIM: Write a program add two 2x2 matrices.

#include <stdio.h>

int main()

{

 int m, n, c, d, first[10][10], second[10][10], sum[10][10];

 printf("Enter the number of rows and columns of matrix\n");

 scanf("%d%d", &m, &n);

 printf("Enter the elements of first matrix\n");

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

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

 scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n");

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

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

 scanf("%d", &second[c][d]);

 printf("Sum of entered matrices:-\n");

 for (c = 0; c < m; c++) {

 for (d = 0 ; d < n; d++) {

 sum[c][d] = first[c][d] + second[c][d];

 printf("%d\t", sum[c][d]);

 }

 printf("\n");

 }

 return 0;

}

OUTPUT

Enter the number of rows and columns matrix

2

2

Enter the elements of 1st matrix

1 1

1 1

Enter the elements of 2nd matrix

1 1

1 1

Sum of entered matrices:

2 2

2 2

PROGRAM 5

AIM: Write a program to count number of positive, negative and zero elements from 

3x3 matrix.

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int countp=0, countn=0, countz=0, arr[10], i;

printf("Enter 10 numbers : ");

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

{

scanf("%d",&arr[i]);

}

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

{

if(arr[i]<0)

{countn++;

}

else if(arr[i]==0)

{

countz++;

}

else

{

countp++;

}

}

printf("Positive Numbers = %d\n",countp);

printf("Negative Numbers = %d\n",countn);

printf("Zero = %d",countz);

getch();

}

OUTPUT

Enter 10 numbers:

12

-5

6

7

0

0

3

0

2

-1

Positive numbers: 5

Negative numbers: 2

Zero: 3

PROGRAM 6

AIM: Write a function for the following operations on string:

Copy one string to another

Comparing two strings

Adding a string to the end of another.

Example of strcmp:

#include <stdio.h>

#include <string.h>

int main()

{

 char s1[20] = "BeginnersBook";

 char s2[20] = "BeginnersBook.COM";

 if (strcmp(s1, s2) ==0)

 {

 printf("string 1 and string 2 are equal");

 }else

 { printf("string 1 and 2 are different");

 }

 return 0;

}

OUTPUT

string 1 and 2 are different

Example of strcat:

#include <stdio.h>

#include <string.h>

int main()

{

 char s1[10] = "Hello";

 char s2[10] = "World";

 strcat(s1,s2);

 printf("Output string after concatenation: %s", s1);

 return 0;

}

OUTPUT

Output string after concatenation: HelloWorld

Example of strcpy:

#include <stdio.h>

#include <string.h>

int main()

{

 char s1[30] = "string 1";

 char s2[30] = "string 2 : I’m gonna copied into s1";

 /* this function has copied s2 into s1*/

 strcpy(s1,s2);

 printf("String s1 is: %s", s1);

 return 0;

}

OUTPUT

String s1 is: string 2: I’m gonna copied into s1

PROGRAM 7

AIM: Write a program to count vowels from a entered String.

#include <stdio.h>

int main()

{

 int c = 0, count = 0;

 char s[1000];

 printf("Input a string\n");

 gets(s);

 while (s[c] != '\0') { if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == 'I' || s[c] =='o' || 

s[c]=='O' || s[c] == 'u' || s[c] == 'U')

 count++;

 c++;

 }

 printf("Number of vowels in the string: %d", count);

 return 0;

}

OUTPUT

Input a String

Hello How are you?

Number of vowels in entered string: 7

PROGRAM 8

Write a program which finds whether a string is a palindrome or not.

#include <stdio.h>

#include <string.h>

int main()

{

 char a[100], b[100];

 printf("Enter a string to check if it is a palindrome\n");

 gets(a);

 strcpy(b,a);

 strrev(b);

 if (strcmp(a,b) == 0)

 printf("Entered string is a palindrome.\n");

 else

 printf("Entered string isn't a palindrome.\n");

 return 0;

}

OUTPUT

Enter the string to check whether it is palindrome or not

Hi

Entered string is not palindrome

PROGRAM 1

Write a program to find factorial of a number using recursion.

#include<stdio.h>

int find_factorial(int);int main()

{

 int num, fact;

 //Ask user for the input and store it in num

 printf("\nEnter any integer number:");

 scanf("%d",&num);

 //Calling our user defined function

 fact =find_factorial(num);

 //Displaying factorial of input number

 printf("\nfactorial of %d is: %d",num, fact);

 return 0;

}

int find_factorial(int n)

{

 //Factorial of 0 is 1 

 if(n==0)

 return(1);

 //Function calling itself: recursion

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

}

OUTPUT

Enter any integer number: 4

factorial of 4 is: 24

PROGRAM 2

AIM: Write a program that used user defined function Swap ( ) and interchange the 

value of two variable.

#include <stdio.h>

//#include <string.h>

void swap (double *d1, double *d2);

int main ()

{

double num1, num2;

clrscr();

printf("Type in 2 Numbers : ");

scanf("%lf%lf", &num1,&num2);

swap(&num1,&num2);

printf("\nThe two Numbers Swapped are %lf, %lf", num1, num2);

getch();

return 0;

}

void swap (double *d1, double *d2)

{

double temp;

temp=*d1;

*d1=*d2;*d2=temp;

}

OUTPUT

Enter 2 numbers:10 20

The two numbers are swapped 20 10

PROGRAM 3

AIM: Write a function to return 1 if the number is prime otherwise return 0.

#include

#include

int prime(int);

int main()

{

int n,p;

clrscr();

printf(“Enter a number : “);

scanf(“%d”,&n);

p=prime(n);

if(p==1)

printf(“%d is prime\n”,n);

else

printf(“%d is not prime\n”,n);

getch();

return 0;

}

int prime(int n)

{

int i;

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

{

if(n%i==0)

return 0;

}

return 1;

}

OUTPUT

Enter a number : 18

18 is not prime

Enter a number : 5

5 is prime

PROGRAM 1

Define a structure type, personal that would contain person name, date of joining 

and salary.

#include<stdio.h>

//declaration of structure struct personal

int no; 

char name[20];

char doj[20]; 

int salary; 

}; 

int main() 

//Structure variable declaration 

struct personal person;

//to read details and assign it to structure variable 

printf("\n Enter Name : "); 

gets(person.name); 

printf("\n Enter Date of Joining (dd/mm/yyyy): ");

gets(person.doj); 

printf("\n Enter Salary in INR : "); 

scanf("%d",&person.salary); 

//to display result 

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

printf("\n Person's information is\n"); 

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

printf("\n Name : %s \n DOJ : %s \n salary : %d ",person.name,person.doj,person.salary); 

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

return 0; 

}

PROGRAM 2

Define a structure called cricket that will describe the following information:

Player name 

Team name 

Batting average

#include<stdio.h>

//declaration of structure 

struct cricket 

{ char team_name[20]; 

char cricketer_name[20]; 

//using date structure as datatype 

float bat_avg; 

}; 

int main() 

int n,i; 

printf("\nHow Many Player's Details You Want To Enter : "); 

scanf("%d",&n); 

//Structure variable declaration 

struct cricket player[n]; 

//to read details and assign it to structure variable 

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

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

printf("\nEnter Information For %d Player",i+1);

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

printf("\n Enter The Name of Player : ");

scanf("%s",&player[i].cricketer_name);

printf("\n Enter The Name of Team: ");

scanf("%s",&player[i].team_name);

printf("\n Enter Player's Batting Average : ");

scanf("%f",&player[i].bat_avg);

}

//to display result

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

printf("\n Player's information is\n");

printf("----------------------------------------------------------");

printf("\n%25s%15s%15s","Player Name","Team Name","Batting Average");

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

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

{

printf("\n%25s%15s%15f",player[i].cricketer_name,player[i].team_name,player[i].bat_avg);

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

}

return 0;

}

PROGRAM 1

AIM: Write a program to add two numbers using pointers.

#include <stdio.h>

int main()

{

 int first, second, *p, *q, sum;

 printf("Enter two integers to add\n");

 scanf("%d%d", &first, &second);

 p = &first;

 q = &second;

 sum = *p + *q;

 printf("Sum of the numbers = %d\n", sum);

 return 0;

}OUTPUT

Enter 2 integers to add

1

2

Sum of the numbers: 3

PROGRAM 2

Write a program to swap two numbers using pointer.

#include<stdio.h>

void swap(int *num1, int *num2) {

int temp;

temp = *num1;

*num1 = *num2;

*num2 = temp;

}

int main() {

int num1, num2;

printf("\nEnter the first number : ");

scanf("%d", &num1);

printf("\nEnter the Second number : ");

scanf("%d", &num2);

swap(&num1, &num2);

printf("\nFirst number : %d", num1);

printf("\nSecond number : %d", num2);

return (0);

}

OUTPUT

Enter the first number : 12

Enter the Second number : 22

First number : 22

Second number : 12

PROGRAM 1

Write a program to illustrate reading files contents.

#include <stdio.h>

#include <stdlib.h>

int main()

{

 char ch, file_name[25]; FILE *fp;

 printf("Enter name of a file you wish to see\n");

 gets(file_name);

 fp = fopen(file_name, "r"); // read mode

 if (fp == NULL)

 {

 perror("Error while opening the file.\n");

 exit(EXIT_FAILURE);

 }

 printf("The contents of %s file are:\n", file_name);

 while((ch = fgetc(fp)) != EOF)

 printf("%c", ch);

 fclose(fp);

 return 0;

}

OUTPUT

Enter the name of file you wish to see

F1.txt

The contents of F1.txt file are:

Hare Krishna

Process returned 0 Execution time: 8.697 s

Press any key to continue..


PROGRAM 2

Write a program to illustrate the use of fgets( ).

#include <stdio.h>

int main () {

 FILE *fp;

 char str[60];

 /* opening file for reading */

 fp = fopen("file.txt" , "r");

 if(fp == NULL) {

 perror("Error opening file");

 return(-1);

 }

 if( fgets (str, 60, fp)!=NULL ) {

 /* writing content to stdout */

 puts(str);

 }

 fclose(fp);

 

 return(0);

Post a Comment

0 Comments