1. Write the advantage of pointer. Write a C program to enter the radius of a football and find the area of football by using user defined function.
Answer:Followings are the advantages of pointer:
a. It allows us to allocate memory dynamically.
b. It makes efficient manipulation of array.
Program:
#include<stdio.h>
#include<conio.h>
void area()
{
float r,a;
printf("Enter the radius of football\n");
scanf("%f",&r);
a=4*3.24*r*r;
printf("The area of football is %f\n",a);
}
void main()
{
clrscr();
area();
getch();
}
2. Define the structure. Write C program using structure to input staff id, name and the salary of 50 staffs. Display staff id, name and salary of those staff whose salary ranges from 25000 to 40000.
Answer: Structure is an user defined variable that allows us to store multiple values within a variable but of different data type.
Program:
#include<stdio.h>
#include<conio.h>
struct staff
{
char id[20],name[20];
long int sal;
}x[50];
void main()
{
int a;
printf("enter the id name and salary of 50 staffs\n");
for(a=0;a<50;a++)
{
scanf("%s%s%ld",&x[a].id,&x[a].name,&x[a].sal);
}
printf("details required\n");
for(a=0;a<50;a++)
{
printf("%s\t%s\t%ld",x[a].id,x[a].name,x[a].sal);
}
getch();
}
3. Write a C program to enter name, grade, age and address of 10 students in a structure and display the information.
Answer:
#include<stdio.h>
#include<conio.h>
struct student{
char name[30],grade[2],add[30];
int age;
}x[10];
void main()
{
clrscr();
int a;
printf("Enter name, grade,address and age of 10 students\n");
for(a=0;a<10;a++)
{
scanf("%s%s%s%d",&x[a].name,&x[a].grade,&x[a].add,&x[a].age);
}
printf("Name\tGrade\tAddress\tAge\n");
for(a=0;a<10;a++)
{
printf("%s\t%s\t%s\t%d\n",x[a].name,x[a].grade,x[a].add,x[a].age);
}
getch();
}
4. Describe the file handling concept in C. Write a C program to enter names and address of a student and store them in a datafile "student.dat".
Answer:File handling in C involves creating, opening, reading, writing, and closing operations on a file. The C language offers various functions like fopen(), fwrite(), fread(), fseek(), fprintf(), and others to execute input, output, and various file operations within our program.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fptr;
char na[50],add[50];
fptr=fopen("student.dat","w");
printf("Enter name and address of a student\n");
scanf("%s%s",&na,&add);
fprintf(fptr,"Name\tAddress\n%s\t%s",na,add);
fclose(fptr);
getch();
}
5. Define function. Write a program to generate factorial of a number using recursive function in c program.
Answer: A function is a block of code that gets executed when it is called. There are two types of function. They are user defined function and library function.
Program to find the factorial of a number using recursive function:
#include<stdio.h>
#include<conio.h>
int fact(int a)
{
if(a<=1)
return 1;
else
return a*fact(a-1);
}
void main()
{
clrscr();
int a;
printf("Enter any number\n");
scanf("%d",&a);
printf("The factorial is %d\n",fact(a));
getch();
}
6. Explain the three modes of file opening in C. Write a program to read name and roll from a file named "exam.dat" and display them.
Answer: In C, when opening a file, you specify a mode that defines the type of operations allowed on the file. There are three main modes for file opening:
1. Read Mode ("r"):
• Allows reading from the file.
• If the file does not exist, fopen() returns NULL.
2. Write Mode ("w"):
• Allows writing to the file.
• If the file does not exist, it creates a new file; if it exists, it truncates the file to zero length.
3. Append Mode ("a"):
• Allows writing to the file, appending data at the end.
• If the file does not exist, it creates a new file.
Program:
#include<stdio.h>
#incllude<conio.h>
void main()
{
clrscr();
char na[50];
int roll;
FILE *fptr;
fptr=fopen("exam.dat","r");
while(fscanf(fptr,"%s%d",&na,&roll)!=EOF)
{
printf("Name=%s\tRoll=%d\n",na,roll);
}
fclose(fptr);
getch();
}
7. Write a program to input roll, name and age of 5 students and display them properly using structure.
Answer:
#include<stdio.h>
#include<conio.h>
struct students{
int roll,sal;
char name[30];
}x[5];
void main(){
int i;
printf(“Enter 5 different student’s roll , name and age: \t”);
for( i=1;i<=5; i++){
scanf(“%d”,&x[i].roll);
gets(x[i].name); //gets variable for string input
scanf(“%d”,&x[i].age);
}
//Creating heading
printf(“Roll\t Name\t Age\n”);
for( i=1; i<=5; i++)
{
printf(“%d\t%s\t%d\n”,x[i].roll,x[i].name,x[i].age);
}
getch();
}
8. Write a C program to enter ID, employee_name, and post of the employee and store them in a data file named "emp.txt". Display each record on the screen in an appropriate format.
Answer:
#include<stdio.h>
#include<conio.h>
void main(){
int id,;
char emp_nam[30],post[10];
FILE *fptr;
fptr = fopen(“emp.txt”,”w”);
printf(“Enter a employee id, name & post: “);
scanf(“%d”,id);
gets(emp_name);
gets(post);
fprintf(fptr, “%d\t%s\t%s\n”,id,emp_name,post);
fclose(fptr);
FILE * fp;
fp = fopen(“emp.txt”, “r”);
while(fscanf(fp,”%d%s%s”,&id,emp_name,post)!=EOF)
{
printf(“%d%s%s, id,emp_name, post);
}
getch();
}
9. Write a program to print the Fibonacci series
6 12 18 30… upto 10 terms
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=6,c=0,d;
for(d=1;d<=10;d++)
{
a=b+c;
if(d==1)
{
b=c;
c=a;
continue;
}
else
{
printf("%d\t",a);
b=c;
c=a;
}
}
getch();
}
10. What is pointer?
Answer: In C, a pointer is a variable that holds the memory address of another variable. Pointers are powerful features of the C language, allowing you to directly manipulate memory and manage data efficiently.
11. What is string function? Write a program to cancatenate two strings.
Answer: String functions in C refer to a set of functions that are specifically designed for manipulating strings. In C, strings are represented as arrays of characters, and these functions provide various operations for working with these character arrays. Some commonly used string functions in C are part of the string.h header file.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s1[20],s2[20];
printf("Enter 2 strings\n");
scanf("%s%s",&s1,&s2);
printf("String after concatenation is %s\n",strcat(s1,s2));
getch();
}
12. Write a program to input emp_no, name and salary of 100 employees using structure. Display the name and salary of employee using structure.
Answer:
#include<stdio.h>
#include<conio.h>
struct employee{
int emp_id,sal;
char nam[30];
}x[100];
void main()
{
int i;
printf(“Enter 100 different employee’s id., name & salary: “);
for( i=0; i<100; i++)
{
printf(“Employee’s no:%d\n”, i+1);
printf(“Employee’s id: “);
scanf(“%d”,&x[i].emp_id);
printf(“Employee’s name: “);
scanf(“%s”,x[i].nam);
printf(“Employee’s salary: “);
scanf(“%d”,&x[i].sal);
}
printf(“Employee’s detail: \n”);
printf(“Employee’s no.\t Name\t Salary\n”);
for( i=0; i<100; i++)
{
printf(“Employee’s no: “);
printf(“Employee’s id:%d\n”,x[i].emp_id);
printf(“Employee’s name:%s\n”,x[i].nam);
printf(“Employee’s salary: %d”,x[i].sal);
}
getch();
}
13. How do you define function in C? Write a program to find out given number is positive or negative using function.
Ans: Function in C is defined as the block of code that gets executed when it is surfed. It divides the program into multiple blocks that extends the features of reusability.
Answer: Program to check whether the integer is +ve or -ve using function is presented below:
#include<stdio.h>
#include<conio.h>
void check(int num)
{
if( num > 0 )
printf(“It is positive.”);
else if ( num < 0 )
printf (“ It is negative”);
else
printf( “ It is zero”);
}
void main(){
int num;
printf(“Enter number: “);
scanf(“%d”,&num);
check(num);
getch();
14. What is file handling in C? Write a program to enter the name and salary of employee and write it in a file "employee.txt".
Ans: File handling in C refers to the process of creating, reading, updating and deleting files and able to store files in the secondary storage so-called harddisk.
> #include<stdio.h>
#include<conio.h>
void main(){
int sal;
char nam[30];
FILE * fptr;
fptr = fopen(“employee.txt”,”w”);
printf(“Enter employee’s name and salary: “);
scanf(“%s%d”,nam,&sal);
fprintf(fptr, “%s\t%d\n”, nam,&sal);
fclose(fptr);
getch();
}
15. Write a program to enter marks of any 5 students and count how many students passed or failed. [PM=35]
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num[5];
int pass = 0, fail = 0;
printf(“Enter the marks of 5 students: \n”);
for( i=0; i<5; i++)
{
printf(“Enter the marks of student %d: “, i+1);
scanf(“%d”,&num[i]);
if(num[i] >= 35)
pass++;
else
fail++;
}
// Now displaying no. of passed or failed students
printf(“Passed students:%d\n”,pass);
printf(“Failed students:%d\n”,fail);
getch();
}
16. What is user defined function? Write a program to input length and breadth of a rectangular garden and find its area by using user defined function.
Ans: User-defined function is a typo of function defined by the users , i.e. according to the choice of user. It can be classified into four types:
i. Function without argument & no return type ,
ii. Function with argument but with no return type,
iii. Function with no argument & but with return type,
iv. Function with both argument & return type.
Answer: Program to input length and breadth and displaying its area can be done using (i) function:
#include<stdio.h>
#include<conio.h>
void area(){
int length, breadth, area;
printf(“Enter length : “);
scanf(“%d”,&length);
printf(“Enter breadth: “);
scanf(“%d”,&breadth);
area = length * breadth ;
printf(“The area of rectangular garden is:%d”,area);
}
void main(){
clrscr();
area();
getch();
}
17. Write a program to input employee id, name, address and post of 20 employees and display them properly.
Program:
#include<stdio.h>
#include<conio.h>
void main(){
int emp_id[20],i;
char emp_nam[30][20],emp_add[20][20], emp_post[20][20];
printf(“Enter 20 different employee’s id, name, address & post: “);
for( i=0; i<20; i++)
{
printf(“Employee no. :%d\n”, i+1);
printf(“Employee’s id: “);
scanf(“%d”,&emp_id[i]);
printf(“Employee’s name: “);
gets(emp_nam[i]);
printf(“Employee’s address: “);
gets(emp_add[i]);
printf(“Employee’s post: “);
gets(emp_post[i]);
}
printf(“Employee’s detail: \n”);
for(i=0 ; i<20 ; i++)
{
printf(“Employee no: “);
printf(“Employee’s id:%d\n”,emp_id[i]);
printf(“Employee’s name:%s\n”,emp_nam[i]);
printf(“Employee’s address:%s\n”, emp_add[i]);
printf(“Employee’s post:%s”,emp_post[i]);
}
getch();
}
18. What is the importance of file pointer in file handling? Explain with an example.
Answer: File pointers act like bookmarks, helping us navigate and keep track of our position in a file during reading or writing. They allow movement through the file in order or direct jumps to specific spots, providing flexibility in data access. File pointers guide where the next reading or writing action should happen, ensuring accurate data handling. Additionally, they play a crucial role in catching errors related to file operations, signaling whether our actions were successful. Overall, file pointers are essential tools for efficiently managing and traveling through data in files.
19. What is function? Write the advantages of function. Differentiate between user defined and library function with examples.
Answer:
Function is a block of code that gets executed when it is called to perfom specific tasks.
Advantages:
a. Easier to break a program in simpler blocks.
b. Allows program reuesability.
c. Breakdown the scope of a program.
d. Allows easy dubugging.
A user defined function is a type of function defined by the user to perform specific tasks, which are higly custumizable whenever needed.
A library function is a type of fucntion provided by the pre-existing libraries and are not easily custumised.
20. Write a program to input 10 integer numbers into an array and display the sum of the numbers.
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],s=0,i;
printf("Enter 10 numbers\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
s=s+a[i];
}
printf("the sum is %d\n",s);
getch();
}
21. Write a program to input 10 integer numbers into an array and display the smallest.
Answer:
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int num[10], i, j, min;
//Input
printf(“Enter 10 different integers: “);
for(i=1; i<=10; i++)
{
scanf(“%d”,&num[i]);
}
min = num[1];
//Identifying min number
for(i=2; i<=10; i++)
{
if(num[i] < min)
{
min = num[i];
}
}
//Now displaying the smallest integer
printf(“Smallest integer:%d”,min);
getch();
}
22. Explain any four string functions with an example.
Answer:
a. strlen (String Length):
=>Returns the length of a string, excluding the null terminator.
b. strcpy (String Copy):
=>Copies the contents of one string to another.
c. strcmp (String Compare):
=>Compares two strings lexicographically (based on ASCII values).
d. strcat (String Concatenate):
=>Concatenates (appends) one string to the end of another.
23. Write a program to find the sum of two numbers using pointer.
Answer:
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int num1, num2, sum;
int *ptr1, *ptr2;
printf(“Enter first number: “);
scanf(“%d”,&num1);
printf(“Enter second number: “);
scanf(“%d”,&num2);
ptr1 = &num1;
ptr2 = &num2;
sum = *ptr1 + *ptr2;
printf(“Sum of %d and %d is %d\n”, *ptr1, *ptr2, sum);
getch();
}
24. Write a program to find the simple interest using function.
Answer:
#include<stdio.h>
#include<conio.h>
void interest(){
float p , t , r , si; //[ S.I : pxtxr/100]
printf(“Enter principle, rate and time: “);
scanf(“%f%f%f”,&p,&t,&r);
si = p*t*r/100;
printf(“The simple interest is %f” , si);
}
void main(){
clrcsr();
interest();
getch();
}
25. Write a program to check odd or even using function.
Answer: #include<stdio.h>
#include<conio.h>
void check(){
int num;
printf(“Enter number: “);
scanf(“%d”,&num);
if(num%2==0)
printf(“It is even”);
esle
printf(“It is odd.”):
}
void main(){
clrscr();
check();
getch();
}
26. Write a program to display reverse of a text.
Answer:
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
char txt[30];
printf(“Enter string: “);
gets(txt);
//Now reversing the inputted string and displaying too
printf(“Reverse form is %s”,strrev(txt));
getch();
}
27. Write a program to enter the 5 subject marks and calculate sum and print using structure.
Answer:
#include<stdio.h>
#include<conio.h>
struct marks{
int sub1, sub2, sub3, sub4, sub5;
}x[5];
void main(){
int i;
int sum[i];
printf(“Enter the marks obtained in english, math, computer, account & science: \n”);
for(i=1; i<=5; i++)
{
scanf(“%d%d%d%d%d”,&x[i].sub1,&x[i].sub2,&x[i].sub3,&x[i].sub4,&x[i].sub5);
sum[i] = x[i].sub1 + x[i].sub2 + x[i].sub3 + x[i].sub4 + x[i].sub5;
}
//Now displaying the detail
printf(“Marks of english\t Math\t Computer\t Account\t Science\t & it’s sum is \n”);
for(i=1; i<=5; i++)
{
printf(“%d\t%d\t%d\t%d\t%d\n”, x[i].sub1,x[i].sub2, x[i].sub3, x[i].sub4, x[i].sub5,sum[i]);
}
getch();
}
28. What is switch statement? Write a program to input to numbers and display sum, product and difference using switch.
Ans: In C programming , Switch statement is control flow statement that allow the program to evaluate an expression against multiple possible constant values.
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
//Displaying menu
int choice, num1,num,2, sum, product , difference;
printf(“\n 1: Sum of two numbers\n”);
printf(“\n 2: product of two numbers\n”);
printf(“n 3: Difference of two numbers\n”);
printf(“\n CHOOSE THE NUMBER BETWEEEN(1-3) FIRST !!! \n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:{
printf(“Enter two number: “);
scanf(“%d%d”,&num1,&num2);
sum = num1 + num2;
printf(“The sum of two number is %d”,sum);
break;
}
case 2:{
printf(“Enter two numbers: “);
scanf(“%d%d”,&num1,&num2);
product = num1 * num2;
printf(“The product is %d”,product);
break;
}
case 3:{
printf(“Enter two numbers: “);
scanf(“%d%d”,&num1,&num2);
difference = num1 - num2;
printf(“The difference is %d”,difference);
break;
}
default:
printf(“Invalid input ! Please TRY AGAIN !! “);
getch();
}
29. Write a program to enter name, address and salary of an employee and store it in a file "staff.dat".
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int sal;
char nam[30],add[20];
FILE * fptr;
fptr = fopen(“staff.doc”, “w”);
printf(“Enter employee’s name, address & salary: \n“);
scanf(“%s%s%d”, nam, add, &sal);
fprintf(fptr, “%s\t%s\t%d\”, nam, add, &sal);
fclose(ptr);
getch();
}
30. Write a program to find the sum of n numbers using recursive function.
Answer:
#include<stdio.h>
#include<conio.h>
int sum(int n)
{
if(n==0)
return 0;
else
return n+sum(n-1);
}
void main()
{
clrscr();
int n;
printf("Enter n number\n");
scanf("%d",&n);
printf("The sum is %d\n",sum(n));
getch();
}
31. Differentiate between array and structure.
Answer:
Arrays are used for organizing a collection of elements of the same data type in a sequential manner, while structures are used for grouping variables of different data types under a single name. Arrays provide homogeneity, whereas structures provide heterogeneity. Elements in arrays are accessed by index, and members in structures are accessed by name.
32. Write a program to copy the roll, name and address of 20 students stored in "std.dat" into a file "records.dat".
33. Describe fscanf and fprintf.
Answer:
fscanf is a function in C under stdio.h that allows us to read the data from a file.
fprintf is a function in C under stdio.h that allows us to write the date to a file.
(Example: Program to read and write data into a file)
34. Differentiate between structure and union.
Answer:
Structure:
a. Allocates separate memory for each member.
b. Each member has its own storage space.
c. Size is the sum of individual member sizes.
d. Members are accessed independently.
e. Suitable for storing and accessing multiple pieces of related information .
Union:
a. Shares a common memory space for all members.
b. All members share the same memory space.
c. Size is determined by the largest member.
d. Only one member can be accessed at a time.
e. Useful for saving memory by sharing space among different types of data.
35. Write a program to add 5 more name and age of students in a file "stdrecord.dat".
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE * fptr;
fptr = fopen(“stdrecord.dat”,”a”);
int i, age;
char nam[30];
for(i=0 ;i<5; i++)
{
printf(“Enter the name of the student %d: ”, i+1);
scanf(“%s”, nam[i]);
printf(“Enter the age of student %d: “,i+1);
scanf(“%d”, &age[i]);
fprintf(fptr, “%s\t%d\n”, nam, &age);
fclose(fptr);
printf(“Students detail added to ‘stdrecord.dat’ successfully \n“);
getch();
}
36. WAP to store "n" records of students in a new file "student.txt" under the roll,name,class, & marks of 3 subjects & also display the total records of all students along with total & percentage after reading from a file !
Answer:
#include<stdio.h>
#include<conio.h>
struct student{
int roll,class;
char nam[20];
int sub1,sub2,sub3;
}n[9999];
void main(){
clrscr();
int i,l;
FILE * fptr;
printf("How many students ?");
scanf("%d",&l);
fptr = fopen("student.txt","w");
printf("Enter %d student's roll,name,class & marks obtained in english,math and science:\n",l);
for(i=0;i<l;i++)
{
printf("Student no:%d\n",i+1);
printf("Student roll: ");
scanf("%d",&n[i].roll);
printf("Student's name: ");
scanf("%s",n[i].nam);
printf("Student's class: ");
scanf("%d",&n[i].class);
printf("Marks obtained in english, math and science: ");
scanf("%d%d%d",&n[i].sub1,&n[i].sub2,&n[i].sub3);
fprintf(fptr,"%d\t%s\t%d\t%d\t%d\t%d\n",n[i].roll,n[i].nam,n[i].class,n[i].sub1,n[i].sub2,n[i].sub3 );
}
fclose(fptr);
int tot[i];
float per[i];
FILE *fp;
fp = fopen("student.txt","r");
printf("Roll\tName\tClass\tEnglish\tMath\tScience\tTotal\tpercntage\n");
while(fscanf(fp,"%d%s%d%d%d%d",&n[i].roll,n[i].nam,&n[i].class,&n[i].sub1,&n[i].sub2,&n[i].sub3)!= EOF){
tot[i] = n[i].sub1 + n[i].sub2 + n[i].sub3;
per[i] = tot[i]/3;
printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\t%f\n",n[i].roll,n[i].nam,n[i].class,n[i].sub1,n[i].sub2,n[i].sub3,tot[i],per[i]);
}
fclose(fp);
getch();
}
Contributor Details
Post Views: 1,104