Year 2018
Group B
2.Write a program to input two integer as arguments and display sum of those numbers
//i am using arguments without return type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> #include<conio.h> void sum(int a,int b); void main() { int a,b; printf("enter two numbers:"); scanf("%d%d",&a,&b); sum(a,b); getch(); } void sum(int a,int b) { int sum=0; sum=a+b; printf("the sum of %d and %d is:%d",a,b,sum); } |
- write a program to write “hello world” to file hello.dat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> #include<conio.h> void main() { FILE *ptr; ptr=fopen("E:\\hello.dat","w");//opens file in E local disk char ch[]="HELLO WORLD"; if(ptr==NULL) { printf("unable to open"); } else { fputs(ch,ptr); fclose(ptr); } getch(); } |
- Write a program for nested structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<stdio.h> #include<conio.h> struct student { int id; char name[20]; struct dateofbirth { int day,month,year; }dob; }s; void main() { printf("enter id,name and date of birth of an student(day-month-year)"); scanf("%d%s%d%d%d",&s.id,&s.name,&s.dob.day,&s.dob.month,&s.dob.year); printf("id\tname\tdate of birth(day-month-year)"); printf("\n%d\t%s\t%d-%d-%d",s.id,s.name,s.dob.day,s.dob.month,s.dob.year); getch(); } |
- Write a program to input on integer as argument and pass the factorial number of one less number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<stdio.h> #include<conio.h> int fact(int a); void main() { int a,fac; printf("enter a number:"); scanf("%d",&a); fac=fact(a); printf("the value less than a is %d and its factorial is %d",a-1,fac); getch(); } int fact(int a) { int i,fact=1; for(i=1;i<a;i++) { fact=fact*i; } return fact; } |
- Write a program to add numbers at even position of integer array of size 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> #include<conio.h> void main() { int a[50],sum=0,i; for(i=0;i<50;i++) { printf("enter the value for a[%d]:",i); scanf("%d",&a[i]); } for(i=0;i<50;i++) { if(i%2==0) { sum=sum+a[i]; } } printf("sum=%d",sum); getch(); } |
Group C
Why files are used in program? Describe sequential and random access of file and also explain any two file handling functions.
Ans: Files are created for the permanent storage of data.
- Sequential access of file:
In sequential access, to access data in the file, all data from the start must be accessed and discarded up to that location. The main disadvantage is that all the preceding data must be read. If the desired data is at the end of the storage, all previous data from the top must be accessed. Sequential access is only means of accessing data in some devices like tape drive. In some cases, it is the access method of choice, for example if we simply want to process a sequence of data elements in order.
- Random access of file:
Random access permits direct access to a specific portion of a file randomly. For random access, we use function fseek( ) to set the file position, ftell( ) to return the current value of the file position and the function rewind( ) to reset the current value of the file position.
File handling functions:
- fopen( ) : It creates a new file or opens an existing file
- fclose( ) : fclose( ) function closes an opened file
Differentiate between switch…case and if…else statement. Write importance of conditional statements.
Importance of conditional statements are:
- It is used for decision making purposes (if statement)
- To execute a single statement or a compound statement (if-else statement)
- To check the series of conditions (multiway conditional statement)