0% found this document useful (0 votes)
23 views

Programming in C QUESTION BANK

The document contains 8 questions related to C programming. Question 1 asks to write an algorithm and flowchart to find the largest of three numbers. Question 2 asks to write a program to generate a multiplication table for a given number up to a given limit using a for loop. Question 3 asks to write a recursive program to calculate the factorial of a given number. Question 4 asks to write a program to store numbers in an array, display them, and find the largest number. Question 5 asks to write a program to sort an array of numbers in ascending order. Question 6 is similar but asks for descending order. Question 7 asks string-related programs to find length and reverse a string. Question 8 asks to write a program for addition of

Uploaded by

muse marni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Programming in C QUESTION BANK

The document contains 8 questions related to C programming. Question 1 asks to write an algorithm and flowchart to find the largest of three numbers. Question 2 asks to write a program to generate a multiplication table for a given number up to a given limit using a for loop. Question 3 asks to write a recursive program to calculate the factorial of a given number. Question 4 asks to write a program to store numbers in an array, display them, and find the largest number. Question 5 asks to write a program to sort an array of numbers in ascending order. Question 6 is similar but asks for descending order. Question 7 asks string-related programs to find length and reverse a string. Question 8 asks to write a program for addition of

Uploaded by

muse marni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

SECTION-I (Major Experiment)

Note: (i) Answer any one Question.


(ii) Each Question carries 20 mark 1X20=20 Mark
1. (a) Write an Algorithm and draw the flow chart to find Largest of three given numbers.
Ans: Algorithm:
ALGORITHM TO FIND LARGEST OF THREE NUMBERS
Step 1: float a,b,c;
Step 2: print “Enter any three numbers:”;
Step 3 : read a,b,c;
Step 4 : if((a>b)&&(a>c))
Step 5: print ‘Largest of three numbers:’, a;
Step 6: else
Step 7: if((b>a)&&(b>c))
Step 8: print ‘Largest of three numbers:’, b;
Step 9: else
Step 10: print ‘Largest of three numbers:’, c;

(b) write a c program to print the biggest of three number.


Ans: PROGRAM TO FIND LARGEST OF THREE NUMBERS
#include<stdio.h>
#include<conio.h>
void main( )
{
float a,b,c;
printf(“Enter any three numbers:”);
1

scanf(“%f%f%f”,&a,&b,&c);
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


if((a>b)&&(a>c))
{
printf(“Largest of three numbers: %f”,a);
}
else
if((b>a)&&(b>c))
{
printf(“Largest of three numbers: %f”,b);
}
else
{
printf(“Largest of three numbers: %f”,c);
}
getch( );
}
2. Write a C program to generate a multiplication table of given number and given limit using for
loop .
Ans: /*Generates multiplication table of n */
#include<stdio.h>
void main()
{int i,n;
printf("\n enter a number for multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
printf("\n%d x %d = %d",n,i,n*i);
getch();
}

input:
enter a number for multiplication table: 5
output:
5x1= 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

3. Write a C program to Calculate of Factorial to given number using Recursion.


Ans: #include<stdio.h>
#include<conio.h>
void main()
{
2

long factorial(int);
Page

int num;
long f;

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


clrscr();
printf("Enter a number n=");
scanf("%d", &num);

if (num < 0)
printf("Negative numbers are not allowed.\n");
else
{
f = factorial(num);
printf("factorial of %ld is %ld\n", num, f);
}
getch();
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

input
Enter a number n= 5
output
Factorial of 5 is 120.

4. Write a C program to Create a single dimensional array of numbers)) and display the
contents. and find the biggest number from this array.
Ans: #include <stdio.h>
#include<conio.h>
void main()
{
int array[100], maximum, size, c, location = 1;

clrscr();
printf("Enter the number of elements in an array:");
scanf("%d", &size);

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

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


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

printf("\n given array is \n"); for(c=0;c<size;+


+c)printf("\n %d",array[c]); maximum = array[0];

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


{
3

if (array[c] > maximum)


Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


{
maximum = array[c];
location = c+1;
}
}

printf("\nMaximum element is present at location number %d and it's value is %d.\n", location,
maximum);
getch();
}
Input
Enter the number of elements in an array:5
Enter 5 integers
22
34
53
45
26
output
given array
22
34
53
45
26
Maximum element is present at location number 3 and it's value is 53.

5. Write a C program to Create a single dimensional array of numbers and display the contents and
Arrange this single dimensional array of numbers into ascending order.
Ans:
#include <stdio.h> for (c = 0 ; c < n - 1 ; c++)
#include<conio.h> {
for (d = 0 ; d < n - c - 1; d++)
void main() {
{ if (array[d] > array[d+1]) /* For decreasing
int array[100], n, c, d, order use < */
swap; clrscr(); {
swap = array[d];
printf("Enter number of elements:"); array[d] = array[d+1];
scanf("%d", &n); array[d+1] = swap;
}
printf("Enter %d integers\n", n); }
}
for (c = 0; c < n; c++)
scanf("%d", &array[c]); printf("Sorted list in ascending order:\n");

printf("\n Given Array\n"); for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);
for (c = 0; c < n; c++) printf("\n
4

%d",array[c]); getch();
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


Input: Given Array
Enter number of elements: 6 34
Enter 6 integers 56
34 45
56 21
45 89
21 78
89 Sorted list in ascending order:
78 21 34 45 56 78 89
output

6. Write a C program to Create a single dimensional array of numbers and display the contents and
Arrange this single dimensional array of numbers into descending order.
Ans:
#include <stdio.h>
#include<conio.h>

void main()
{
int array[100], n, c, d, swap;
clrscr();

printf("Enter number of elements:");


scanf("%d", &n);

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

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


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

printf("\n Given Array\n");

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


printf("\n%d",array[c]);

for (c = 0 ; c < n - 1 ; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");


5
Page

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

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


printf("%d\n", array[c]);

getch();
}
Input:
Enter number of elements: 6

7. Write a C program to input a character string and print the string


(a) Find the length of a given character string.
Ans:
/*string length*/
#include<stdio.h>
#include<string.h>

void main()
{
char arr[100];

printf("Enter a string to to find length of it: \n");


gets(arr);

printf("The string length = %d ", strlen(arr));

getch();
}
input
Enter a string to to find length of it: venkata ramana
output
The string length = 14

(b) Display the Reverse of a given character array (string).


Ans:
string reverse
/*string reverse*/
#include<stdio.h>
#include<string.h>

void main()
{
char arr[100];

printf("Enter a string to reverse:\n ");


gets(arr);

strrev(arr);

printf("Reverse of entered string is: \n%s\n ",arr);


6
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


getch();
}
input
Enter a string to reverse: ramana
output
Reverse of entered string is: anamar

8. write a C program for Addition of two dimensional matrices.


Ans: #include<stdio.h>
#include<conio.h>
#include<process.h>
void main( )
{
int x [5] [5], y [5] [5], z [5] [5], m, n, p, q, i,
j; clrscr ( );
printf (“Enter number of rows and columns of matrix x \n”);
scanf (“%d %d”, &m, &n);
printf (“Enter number of rows and columns of matrix y \n”);
scanf (“%d %d”, &p, &q);
if ((m ! =P) | | (n!=q))
{
printf (“Matrices not compatible for addition \n”);
exit(1);
}
printf (“Enter the elements of x \n”);
for (i=0; i<m; i++)
for (j=0; j<n; j++)
scanf (“%d”, &x *i+*j+);
printf (“Enter the elements of x \n”);
for (i=0; i<p; i++)
for (j=0; j<n; j++)
scanf (“%d”, &y *i+ *j+);
/* Summation begins */
for (i=0; j<m; i++)
for (j=0; j<n; j++)
z[i] [j] = x [i] [j] + y [i] [j];
/* summation ends */
printf (“Sum matrix z \n);
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
printf (“%d”, z*i+ *j+);
printf (“\n”);
}
7

getch( );
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


9. Write a C program for Multiplication of Two matrices
Ans:
/*matrix multiplication*/

#include<stdio.h>
#include<conio.h>
void main()
{
int ar,ac,br,bc, c, d, e, a[10][10], b[10][10], mul[10][10];

clrscr();
printf("Enter the number of rows in matrix-A: ");
scanf("%d", &ar);
printf("Enter the number of columns in matrix-A: ");
scanf("%d", &ac);

printf("Enter the number of rows in matrix-B: ");


scanf("%d", &br);
printf("Enter the number of columns in matrix-B: ");
scanf("%d", &bc);

if ( ac != br) goto lastpara;


printf("Enter %d elements of first matrix-A\n",ar*ac);

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


for ( d = 0 ; d < ac ; d++
)
scanf("%d", &a[c][d]);

printf("Enter %d elements of second matrix-B\n",br*bc);

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


for ( d = 0 ; d < bc ; d++ )
scanf("%d", &b[c][d]);

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


for ( d = 0 ; d <bc ; d++ ){
mul[c][d] = 0;
for(e = 0;e<ac;++e)
mul[c][d] = mul[c][d] + a[c][e] * b[e][d];}

printf("\n matrix-A is \n");


for ( c = 0 ; c < ar ; c++ )
{
for ( d = 0 ; d < ac; d++ )
printf("%d\t", a[c][d]);

printf("\n");
}
8

printf("\n matrix-B is\n");


Page

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


{

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


for ( d = 0 ; d < bc; d++ )
printf("%d\t", b[c][d]);

printf("\n");
}

printf(" A x B :-\n");

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


{
for ( d = 0 ; d < bc; d++ )
printf("%d\t", mul[c][d]);

printf("\n");
}
goto endpara;
lastpara:printf("\n columns in matrix -A is not equal to rows in matrix -B, hence can not
multiply");
endpara:
getch();

}
input
Enter the number of rows in matrix-A:2
Enter the number of columns in matrix-A: 2
Enter the number of rows in matrix-B:2
Enter the number of columns in matrix-B: 2
Enter 4 elements of first matrix-A
1
2
3
4

Enter 4 elements of second matrix-B


5
6
7
8

output
matrix-A is
1 2
3 4

matrix-B is
5 6
7 8
A x B :-
19 22
43 50
9
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


10 write a C program to generate Fibonacci series using Recursion for given limit.
Ans:
#include<stdio.h>
void main()

{ int Fibonacci(int);
int n, i = 0, c;
printf("Enter the number of terms:\n");
scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);


for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}

getch();
}

int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Input
Enter the number of terms: 10
Output
First 10 terms of Fibonacci series are :-
0
1
1
2
3
5
8
13
21
34
10
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


SECTION-II (Minor Experiment)
Note: (i) Answer any One Question
(ii) Each Question carries 10 marks 1X10=10 Marks
11. Write a C program for the following
(a) To Perform Addition, Subtraction, Multiplication, Division and Modulus operation on
two integers.
Ans:
#include<stdio.h>
void main()
{ int a,b;
printf("\n enter two values: ");
scanf("%d %d",&a,&b);
printf("\n a + b = %d", a+b);
printf("\n a - b = %d", a-b);
printf("\n a * b = %d", a*b);
printf("\n a / b = %d", a/b);
printf("\n a modulus b = %d", a%b);
getch();
}

input:

enter two values: 13 5

output:

a + b = 18
a-b=8
a * b = 65
a/b=2
a modulus b = 3

(b) Find the Area and Circumference of a Circle.


Ans:
Area of a circle = ∏r2
Circumference of a circle = 2∏r
/* finds area and circumference of a circle */
#include<stdio.h>
void main()
{ float rad,area,circum;
printf("\n enter radius: ");
scanf("%f",&rad); printf("\n
radius = %f",rad); area =
22.0/7.0 * rad * rad;
circum = 2.0 * 22.0/7.0 * rad;
printf("\n area of a circle = %f", area);
11

printf("\n circumference of a circle = %f", circum);


getch();
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


input
enter radius: 7
radius = 7.000000
output
area of a circle = 154.000000
circumference of a circle = 44.000000
12. Write a C program for the following
(a) Calculate simple and compound interest.
Ans:
Simple interest = PNR/100
Compound interest = P(1+r/100)n – P
/*finds simple interest and compound interest*/
#include<stdio.h>
#include<math.h>
void main()
{ float si,ci,p,n,r;
printf("\n enter principal:");
scanf("%f",&p);
printf("\n enter no of years:");
scanf("%f",&n);
printf("\n enter rate of interest:");
scanf("%f",&r);
si= p*n*r/100.0;
ci = p*pow((1.0 + r/100.0), (int) n)-p;
printf("\n simple interest = %f",si);
printf("\n compound interest = %f", ci);
getch();
}
input:
enter principal: 1000
enter no of years: 2
enter rate of interest: 10
output:
simple interest = 200.000000
compound interest = 210.000000

(b) Convert temperature in Celsius to Fahrenheit


Ans:
1 Fahrenheit degree = 32.0 + 9 C / 5 = 33.8 celcius(centigrade) degrees
#include <stdio.h>
main()
{
float celsius, fahrenheit;
printf("\n Please Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = ((celsius * 9)/5) + 32;
printf("\n %.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
12

}
Page

input:

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


Please Enter temperature in Celsius: 32
output:
32.00 celsius =89.60 Fahrenheit
13. Write a C program for the following.
(a) Find the Average of the 5 subject marks of a student Find a student’s result based on
marks Ans: #include<stdio.h>
#include<conio.h>
void main()
{
char stname[10];
int gfcm, engm, cfmsom, cprogm, acctm, total;
float avg;
clrscr();
printf("\n enter student name: ");
scanf("%s", stname);
printf("\n enter gfc marks(0-50):");
scanf("%d", &gfcm);
printf("\n enter english marks(0-50):");
scanf("%d" ,&engm);
printf("\n enter cfmso marks(0-50):");
scanf("%d", &cfmsom);
printf("\n enter c programming marks(0-50):");
scanf("%d", &cprogm);
printf("\n enter accountancy & tally marks(0-50):");
scanf("%d", &acctm);
printf("\n student name is %s " ,stname);
total = gfcm+engm+cfmsom+cprogm+acctm;
avg = float (total/5.0);
printf("\n total marks = %d",total);
printf(" \n average marks are = %.2f",avg);
if(gfcm>=18 && engm >=18 && cfmsom >=18 && cprogm >=18 && acctm >= 18) printf("\n
Result is passed");
else printf("\n Result is failed");
getch();
}

input:

enter student name: surya


enter gfc marks(0-50): 35
enter english marks(0-50): 30
enter cfmso marks(0-50): 25
enter c programming marks(0-50):40
enter accountancy & tally marks(0-50):30
13

output:
Page

student name is surya

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


total marks = 160
average marks are = 32.00
Result is passed

(b) Find a number is even or


odd. Ans:
#include<stdio.h>
void main()
{
int n;

printf("\n Enter an integer: ");


scanf("%d",&n);
if ( n%2 == 0 )
printf("\n %d is an Even”,n);
else
printf("\n %d is an Odd",n);

getch();
}
input
Enter an integer: 5
Output
5 is an Odd number
14. Write a C program to
(a) Write a C program to find whether given number is Prime number or not
Ans:
/* A prime no is positive numbers that have just two factors that is 1 & itself */

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,flag=0;
clrscr();
printf("\n Enter any number : ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i= =0)
{
flag=1;
break;
}
}
if(flag= =1)
14

printf("\n Given Number is not prime \n");


Page

else

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


printf("\n Given number is prime ");
getch();
}
input
Enter any number : 29
Output
Given number is prime
Prime number: A number which is only divisible by one and itself is called a prime number. Divisible
means remainder is zero. 5 is a prime number because it is only divisible by one and five. 6 is not a
prime number because it is divisible by one, two, three and six.

/*finds prime or not */


#include<stdio.h>
void main()
{
int c=0,i,n;
printf("enter the number to be checked: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

if(n%i==0)
{
c=c+1;
}
}
if (c==2)
printf("%d is a prime",n);
else
printf("%d is not a prime",n);
getch();
}
input
enter the number to be checked: 5
output
5 is a prime
Input
enter the number to be checked: 6
output
6 is not a prime

(b). Write a function and call a function to perform add, subtract and multiply two numbers.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int add(int x , int y);
15

int sub(int x , int y);


Page

int mul(int x , int


y);

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


int m,n;
clrscr();
printf("\nenter two integers ");
scanf("%d %d",&m,&n);
printf("\n %d + %d is %d",m,n,add(m,n));
printf("\n %d - %d is %d",m,n,sub(m,n));
printf("\n %d * %d is %d",m,n,mul(m,n));
getch();
}
int add(int x,int y)
{ int p;
p=x+y;
return(p);}

int sub(int x,int y)


{ int p;
p=x-y;
return(p);}
int mul(int x,int y)
{ int p;
p=x*y;
return(p);}

Input
enter two integers 3 5
Output
3 + 5 is 8
3 - 5 is -2
3 * 5 is 15
15. (a). Write a C program to Create a structure( struct) by name “book” containing book no., book
name, author and cost as members. Create book1 and book2 as copies of this structure and display
the values for two books. Display the total cost of the books.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{ int bookno;
char bookname[20];
char author[20];
int cost;
} book1 , book2;
int total;
16

clrscr();
Page

printf("\n Enter Book No : ");

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


scanf("%d",&book1.bookno);
printf("\n Enter Book Name : ");
scanf("%s",book1.bookname);
printf("\n Enter author name : ");
scanf("%s",book1.author);
printf("\n Enter Book cost : ");
scanf("%d" ,&book1.cost);
printf("\n Enter Book No : ");
scanf("%d",&book2.bookno);
printf("\n Enter Book Name : ");
scanf("%s",book2.bookname);
printf("\n Enter author name : ");
scanf("%s",book2.author);
printf("\n Enter Book cost : ");
scanf("%d" ,&book2.cost);

printf("\n Book No : %d" , book1.bookno);


printf("\n Book Name : %s", book1.bookname);
printf("\n author name : %s",book1.author);
printf("\n Book cost : %d",book1.cost); printf("\
n");

printf("\n Book No : %d" , book2.bookno);


printf("\n Book Name : %s", book2.bookname);
printf("\n author name : %s",book2.author);
printf("\n Book cost : %d",book2.cost); printf("\
n");
total = book1.cost + book2.cost; printf("\
n Total cost of books = %d",total);

getch();
}
Input
Enter Book No : 9440
Enter Book Name : MS-Office
Enter author name : Sathish
Enter Book cost : 350

Enter Book No : 5595


Enter Book Name :C-Language
Enter author name : Ramana
Enter Book cost : 150

output

Book No : 9440
Book Name : MS-Office
author name : Sathish
Book cost : 350
17
Page

Book No : 5595

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


Book Name : C-Language
author name : Ramana
Book cost : 150
Total cost of books = 500

(b).Create a structure by name “employee” with necessary data members and create an array of
5 employees and display the values.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
struct employee
{ int empidno;
char empname[20];
char designation[20];
int salary;
} emp[5];

int i,n;

clrscr();
printf("\n how many employees are to be entered");
scanf("%d",&n);
for (i=0;i<n;++i){

printf("\n Enter Employee IDNo : ");


scanf("%d",&emp[i].empidno);
printf("\n Enter Employee Name : ");
scanf("%s",emp[i].empname);
printf("\n Enter designation : ");
scanf("%s",emp[i].designation);
printf("\n Enter salary : ");
scanf("%d" ,&emp[i].salary);
printf("\n");}
printf("\n employee list\n");

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

printf("\n Employee IDNo : ");


printf("%d",emp[i].empidno);
printf("\n Employee Name : ");
printf("%s",emp[i].empname);
printf("\n Designation : ");
printf("%s",emp[i].designation);
printf("\n Salary : ");
printf("%d" ,emp[i].salary);
printf("\n");}
18

getch();
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


}

Output:
employee list
Employee IDNo : 101
Employee Name : YOUNUS
Designation : JUDGE
Salary : 45000

Employee IDNo : 102


Employee Name : VENKATA RAMANA
Designation : LECTURER
Salary : 42000

Employee IDNo : 103


Employee Name : SATHISH
Designation : ENGINEER
Salary : 60000

16 (a) Use file operation functions to read, write and append the data to and from files Use file
operation functions
Ans:
a) Read a file.
#include<stdio.h>
#include<stdlib.h>

void main()
{
char ch, file_name[25];
FILE *fp;

printf("Enter the name of file you wish to see ");


gets(file_name);

fp = fopen(TEST.C,"r"); /* read mode */

if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}

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

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


printf("%c",ch);

fclose(fp);
getch();
19

}
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


b) File copying

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

void main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;

printf("Enter name of file to copy\n");


gets(source_file);

source = fopen(source_file, "r");

if( source == NULL )


{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

printf("Enter name of target file\n");


gets(target_file);

target = fopen(target_file, "w");

if( target == NULL )


{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

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


fputc(ch, target);

printf("File copied successfully.\n");

fclose(source);
fclose(target);

getch();
}

(b). Write a program to create a simple text file and write and read data from using functions like
fopen() etc.
Ans:
a) write a file
20

/*write a file(file creation) with some names*/


#include <stdio.h>
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


#include<conio.h>
void main()
{
FILE *fp;
int i,n;
char sname[20];
clrscr();
fp = fopen("file.txt","w");
/*Create a file and add text*/
printf("\n how many names you want to write into a file");
scanf("%d",&n);
for(i=1;i<=n;++i){ printf("\
enter a name :");
scanf("%s",sname);
fprintf(fp,"\n %s",sname); /*writes data to the file*/
}
fclose(fp); /*done!*/
getch();
}
input
how many names you want to write into a file 3
enter a name :Dharmaraja
enter a name :Bheema
enter a name :Arjuna
Output
Output is stored in the file named file.txt.
so to get the output
give the following command at the location where file is stored

Type file.txt

Dharmaraja
Bheema
Arjuna

b) append a file
/*append a file(add some names to a file)*/
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int i,n;
char sname[20];
clrscr();
fp = fopen("file.txt","a");
/*Create a file and add text*/
printf("\n how many names you want to add(append) to a file:");
21

scanf("%d",&n);
for(i=1;i<=n;++i){
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


printf("\enter a name :");
scanf("%s",sname);
fprintf(fp,"\n %s",sname); /*writes data to the file*/
}
fclose(fp); /*done!*/
getch();
}

input
how many names you want to add(append) to a file: 2
enter a name : Nakula
enter a name : Sahadeva

Output
Output is stored in the file named file.txt.
so to get the output
give the following command at the location where the file is stored

Type file.txt
Dharmaraja
Bheema
Arjuna
Nakula
Sahadeva

c) Read a file
/*reads a file */
#include <stdio.h>
#include<conio.h>
void main( )
{
FILE *fp;
char c;
clrscr();
fp = fopen("file.txt", "r");
if (fp == NULL)
printf("File doesn't exist\n");
else
do {
c = getc(fp); /* get one character from the file */
putchar(c);} /* display it on the monitor */
while(c != EOF);
fclose(fp);
getch();
}

Output
Dharmaraja
Bheema
22

Arjuna
Nakula
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


Sahadeva

17. Write a C program to find whether given number is perfect or not.


Ans: Perfect number is a positive integer which is equal to the sum of its proper positive divisors.
For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.
Logic to check Perfect number
Step by step descriptive logic to check Perfect number.
1. Input a number from user. Store it in some variable say num.
2. Initialize another variable to store sum of proper positive divisors, say sum = 0.
3. Run a loop from 1 to num/2, increment 1 in each iteration. The loop structure should look like
for(i=1; i<=num/2; i++). Why iterating from 1 to num/2, why not till num? Because a number does
not have any proper positive divisor greater than num/2.
4. Inside the loop if current number i.e. i is proper positive divisor of num, then add it to sum.
5. Finally, check if the sum of proper positive divisors equals to the original number. Then, the given
number is Perfect number otherwise not.

#include <stdio.h>
int main()
{
int i, num, sum = 0;

/* Input a number from user */


printf("Enter any number to check perfect number: ");
scanf("%d", &num);

/* Calculate sum of all proper divisors */


for(i=1; i<num; i++)
{
/* If i is a divisor of num */
if(num%i == 0)
{
sum += i;
}
}

/* Check whether the sum of proper divisors is equal to num */


if(sum == num)
{
printf("%d is PERFECT NUMBER", num);
}
else
{
printf("%d is NOT PERFECT NUMBER", num);
}
23

return 0;
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


input
Enter any number to check perfect number: 6
Output
6 is PERFECT NUMBER

18. Write a C program to perform string handling functions


Ans:
#include<stdio.h>
#include<string.h>
main()
{
char name[10];
int len;
clrscr();
printf("Enter your name: ");
scanf("%s",name);
len=strlen(name);
printf("\n\nThe string length is %d",len);
strupr(name);
printf("\n\n The name is %s",name);
strlwr(name);
printf("\n\n The name is %s",name);
strrev(name);
printf("\n\n The string reverse order is %s",name);
getch();
}

String concatenation
/*string concatenation */
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char a[100], b[100];

printf("Enter the first string\n");


gets(a);

printf("Enter the second string\n");


gets(b);

strcat(a,b);

printf("String obtained on concatenation is %s\n",a);

getch();
24

}
input
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


Enter the first string Venkata
Enter the second string Ramana
output
String obtained on concatenation is VenkaraRamana

string copy
/*string copy */
#include<stdio.h>
#include<string.h>

void main()
{
char source[] = "abc";
char destination[]="xyz";

printf("\n Before copy\n");


printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);

strcpy(destination, source);
printf("\n After copy\n");

printf("Source string: %s\n", source);


printf("Destination string: %s\n", destination);
getch();

}
output
Before copy
Source string: abc
Destination string: xyz

After copy
Source string: abc
Destination string: abc

19. Write a C program to transpose a given matrix


Ans:
[The transpose of a matrix is obtained by switching the rows and columns of matrix].
For example, consider the following 3 X 2 matrix:
12
34
56
Transpose of the matrix:
135
246
When we transpose a matrix then its order changes, but for a square matrix it remains the same.
#include<stdio.h>
25

#include<conio.h>
void main ( )
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


{
int a[3] [3], b[3] [3], i, j;
clrscr ( );
printf (“Enter the elements of the matrix : \n”);
for (i=0; i<3; i++)
for (j=0; j<3; j++)
scanf (“%d”, &a*i+ *j+);
printf (“given matrix is : \n”);
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
printf (“%d”, a*i+ *j+);
printf (“\n”);
}
printf (“transpose of given matrix is : \n”);
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
b [i] [j] = a [j] [i];
printf (“%d”, b*i+ *j+);
printf (“\n”);
}
}
getch( );
}
Output of program:

20. write a C program to check given year is Leap year or not


Ans:
#include <stdio.h>
int main()
{
int year;

printf("Enter a year: ");


scanf("%d",&year);
26

if(year%4 == 0)
{
Page

if( year%100 == 0)

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);

return 0;
}
Output 1 Output 2
Enter a year: 1900 Enter a year: 2012
1900 is not a leap 2012 is a leap year
year.

SECTION-III (Identification/ Spotting)


Note: (i) Answer any Five Questions.
(ii) Each Question carries 2 marks 5X2=10 Marks

21. What is a flowchart? What are the Symbols used in it?

Ans: A flow chart is a step by step diagrammatic representation of the logic paths to solve a given
problem. Or a flow chart is visual or graphical representation of an algorithm. It is methods to be used
to solve a given problem and help a great deal to analyze the problem and plan its solution in a
systematic and orderly manner. A flowchart when translated in to a proper computer language, results
in a complete program.

27
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


22. Write the structure of C program.

Ans : The structure of C program is

The basic components of a C program are:


 main()
 pair of braces { }
 declarations and statements
 user defined functions

23. Write about IF... Else statement with an example


Ans:

The if-else statements is used to execute the either of the two statements depending upon
the value of the exp (expression) or condition. The general form is
if(<exp>)
{
Statement-1;
Statement -2;
………….. “ SET-I”
……………
Statement- n;
}
else
{
Statement1;
Statement 2;
………….. “ SET-II”
……………
Statement n;
}
SET - I Statements will be executed if the exp is true.
SET – II Statements will be executed if the exp is false.
Example:
if ( a> b )
printf (“a is greater than b”);
else
printf (“a is not greater than b”);
28
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


24. Write about SWICH statement.
Ans:

Switch case is used to select a single statement from set of several alternatives statements. It is
made with an option of choices from set of statements. The variable in the switch executes only a
particular statement whenever it satisfies the case constant.

Syntax:
switch(exp)
{
case constant-1: statement 1;
break;
case constant-2: statement 2;
break;
case constant-n:
statement n;
break;
default
: statement;

}
Break: The break statement are used inside each case of the switch, causes an intermediate exit
from the switch statement.
Default: When any case is not matched then this statement will be executed.

25. Write short notes on Function.

Ans: A function can be defined as a subprogram which is meant for doing a specific task. In a C
program, Or function is a self contained block of statement that specifies one or more actions to be
performed for the large program, a function definition will have name and parentheses pair

Ex: main ().

The general syntax of function is

Type name of the function ( parameter list)


{
Variable declaration; /* within the function */
Statements ;
Statements ; /* body of the function */
Statements;
return(value computed)
}
29
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


26. What is an Array ? what are the types in it.

Ans: Array is a collection of similar data elements. Which can be referring by a single name. The
array variables values will be stored in sequence (sequential memory allocation). An array is also
known as a subscripted variable. Basically arrays can divide in to
1. One Dimensional Array
2. Two Dimensional Array
27. Write the differences between Iteration and Recursion.

Ans: Recursion can be defines as the process of a function by which it can call itself. The function
which calls itself again and again either directly or indirectly based on condition is known as recursive
function.

The iteration means repetition of process until the condition becomes false. For example – when we
use loop (for, while etc.) in our programs. It involves four steps, initialization , condition, execution
and updation. The iteration is applied to the set of instructions which we want to get repeatedly
executed.

28. What are the File operations in C .

Ans: There are 4 basic operations that can be performed on any files in C programming language.
They are,

1. Opening/Creating a file  fopen() this function creates a new file or opens


an existing file.
2. Closing a file  fclose() this function is used to closes an opened file.
3. Reading a file  fgets() - reads string from a file, one line at a time. , fscanf ()
function reads formatted data from a file. fgetc() this function is used to reads a
character from file.,getche(),getchar()
4. Writing in a file  fprintf() this function writes formatted data to a file ,fputs() this
function is used to writes a string to a file,

29. What is structure (‘struct’)? Write its syntax with example.

Ans: A group of one or more variables of different data types organized together under a single
name is called Structure. a structure can be viewed as a heterogeneous (dissimilar) user-defined
data type. When a structure is defines the entire group s referenced through the structure name.
The individual components present in the structure are called structure members and those can be
accessed and processed separately.

Syntax:
struct <struct name>
{
data type variable name;
data type variable name;
} one or more structure variables;
Example: struct employee
30

{
Page

int eno;

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD


char name [80];
float sal;
} emp1, emp2;

30. What is Union? Write its syntax with an example?

Ans: A Union is a collection of heterogeneous elements. It is a group of elements; each element is of


different type. It is a similar to structure. The unions are used to save memory. Unions are chosen
for applications involving multiple members, where values need to be assigned to all of the
members at any one time.

Syntax:

union name {
data type member-1;
data type member-2;
data type member-3;
………………….
…………………
data type member-n;
};

Example :
union value
{
int no;
float sal;
char sex;
};

31
Page

P VENKATA RAMANA – GOVT. JR COLLEGE BOYS , NIZAMABAD

You might also like