C - PREV - QUES - ANS
C - PREV - QUES - ANS
Algorithm:
Step 1: start
Step 2: set i=0, sum=0
Step 3: read mark of a student, mark
Step 4: perform sum=sum + mark
Step 5: increment the value of i i.e i=i+1
Step 6: check if i<50 then goto step 3 otherwise go to step 6
Step 7: perform average=sum/50
Step 8: print he values of sum and average (Using this algorithm draw flowchart)
7. Which are the basic data types in C language? How much memory is allocated for
each?
8. Write a program to swap two numbers without third variable
9. Find the reverse of a number using loop statement.
10. Write a program to print first 10 numbers of Fibonacci series.
11. Explain any five kinds of operators in C.
12. Draw a flowchart to find the sum of digits of an integer
13. If more than one kind of operator is present in an expression, explain the order of
precedence.
14. Write a C program to print the prime numbers between 101 and 500. Those numbers
whose sum of digits is 5 need not be printed. Use ‘while’ loop in the program
Program:
#include<stdio.h>
int main()
{
int i,j,found,n,num,b,sum;
for(i=101;i<=500;i++)
{
found = 0;
sum=0;
n=i;
for(j=2;j<n;j++)
{
if(n%j==0)
{
found = 1;
break;
}
}
if(found == 0)
{
num=n;
while(n!=0)
{
b=n%10;
sum=sum+b;
n=n/10;
}
if(sum!=5)
{
printf("%d\n",num);
}
}
}
return 0;
}
15. Explain ‘switch’ and ‘go to’ statements in C with the help of examples
16. Write a C program to print the following pattern using ‘for’ loop
**
***
****
*****
Program:
#include<stdio.h>
Void printprime(int,int);
int main()
{
int i,n1,n2,j,found,n;
printf("Enter a lower limit and upper limit");
scanf("%d %d",&n1,&n2);
printprime(n1,n2);
return 0;
}
Void printprime(int lower,int upper)
{
Printf(“Prime numbers between %d and %d are\n”,lower,upper);
for(i=lower;i<=upper;i++)
{
found = 0;
n=i;
for(j=2;j<n;j++)
{
if(n%j==0)
{
found = 1;
break;
}
}
if(found == 0)
printf("%d\n",n);
}
12. Write a C program to store the name and roll numbers of 10 students using structure.
The name must be then printed in the ascending order of roll numbers.
#include<stdio.h>
struct student
{
int rollno,marks;
char name[25];
}stud[50],t;
void main()
{
int i,j,n;
printf("Enter the no of students\n");
scanf("%d",&n);
printf("enter student info as rollno , name , marks\n");
for(i=0;i<n;i++)
{
scanf("%d %s %d",&stud[i].rollno,stud[i].name,&stud[i].marks);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(stud[j].rollno<stud[j+1].rollno)
{
t=stud[j];
stud[j]=stud[j+1];
stud[j+1]=t;
}
}
}
#include<stdio.h>
#include<file.h>
int main()
{
FILE *f1,*f2;
char c;
printf(“data input\n\n”)
f1 = fopen(“INPUT”, “w”);
while((c = getchar())!=EOF)
putc(c,f1);
fclose(f1);
printf(“\ndata output\n\n”)
f1 = fopen(“INPUT”, “r”);
while((c = getchar())!=EOF)
putc(“%c”,c);
fclose(f1);
return 0;
}
Program
include<stdio.h>
void findFactorial(int,int *);
int main()
{
int i,factorial,num;
printf("Enter a number: ");
scanf("%d",&num);
findFactorial(num,&factorial);
printf("Factorial of %d is: %d",num,*factorial);
return 0;
}
void findFactorial(int num,int *factorial)
{
int i;
*factorial =1;
for(i=1;i<=num;i++)
*factorial=*factorial*i;
}
4. What are the advantages of using pointers in C
5. How is append mode different from write mode regarding files in C?
6. Which arithmetic operations are possible with pointers? Illustrate with suitable
example.
7. How is a file pointer declared? What are the different modes of opening a file?Explain
with one simple example.
8. Write a C program using pointers to sort an array of integers by calling a function.
9. Distinguish between address stored in the pointer and value in that address. How does
one pointer point to another pointer? Explain with examples.
10. Write a C program to add two variables using pointers.
11. Explain how a new file is opened. What are the 3 modes while opening an existing
file
12. Write a C program to store Roll No, Name and Marks for five subjects of class of 50
students to a file named “Student_data”. Based on the data read from this file, prepare
a rank list and write into another file “Rank_list”.
13. What is a pointer. How can you access a variable using chain of pointers?
14. Write a C program to sort an array using pointers.
Program
#include <stdio.h>
Void sort(int,int *);
int main()
{
int n = 5;
int arr[] = { 0, 23, 14, 12, 9 };
sort(n, arr);
return 0;
}
// Function to sort the numbers using pointers
void sort(int n, int* ptr)
{
int i, j, t;
t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}
Program
#include<stdio.h>
#include<file.h>
main()
{
FILE *f1,*f2;
char c;
printf(“data input\n\n”)
f1 = fopen(“INPUT”, “w”);
while((c = getchar())!=EOF)
putc(c,f1);
fclose(f1);
printf(“\ndata output\n\n”)
f1 = fopen(“INPUT”, “r”);
while((c = getchar())!=EOF)
putc(“%c”,c);
fclose(f1);
//copying f f1 data into f2
f1=fopen(file1,”r”)
if(f1=NULL)
{
printf(“no data”);
exit(0):
}
f2=fopen(file2,”w”);
if(f2=null)
{
printf(“cannot able to open”);
exit(0);
}
while((ch==getc(f1)!=EOF)
putc(ch,f2);
printf(“completed”);
fclose(f1);
fclose(f2);
}