VANI C Practical File
VANI C Practical File
Q1. Write a program to convert temperature from Celsius to Fahrenheit by taking input from the
user.
Code:
#include<stdio.h>
void main()
float celsius,fahrenheit;
clrscr();
scanf("%f",&celsius);
getch();
Output:
VANI TANWAR
DIVYANSHU BCA
2
Q2. Write a program to find the greatest no. among 3 numbers given by the user.
Code:
#include<stdio.h>
int main ()
else
Return 0;
VANI TANWAR
DIVYANSHU BCA
3
Output:
VANI TANWAR
DIVYANSHU BCA
4
Code:
#include <stdio.h>
number.", n);
else{
int n, i, flag = 0;
scanf("%d", &n);
if (n == 0 || n == 1){
flag = 1;
if (n % i == 0) {
flag = 1;
break;
if (flag == 0){
return 0;
Output:
VANI TANWAR
DIVYANSHU BCA
5
Q4. Write a program to display the following pattern upto n rows, taking the value of n from the user.
23
456
7 8 9 10
Code:
#include <stdio.h>
int main() {
clrscr();
scanf("%d", &rows);
++number;
printf("\n");
return 0;
VANI TANWAR
DIVYANSHU BCA
6
Output
VANI TANWAR
DIVYANSHU BCA
7
Q5. Write a program to input the marks of 50 students using an array and find the avg. marks of the
class.
Code:
include<stdio.h>
int main ()
double average;
sum += marks[i];
return 0;
VANI TANWAR
DIVYANSHU BCA
8
Output
VANI TANWAR
DIVYANSHU BCA
9
Q6. Write a program to search for a no. entered by the user in a given array and display the array in
the ascending order.
Code:
#include <stdio.h>
int num[20]={3,4,1,5,2,6};
int i, j, a, n , no,flag=0;
scanf("%d",&no);
a = num[i];
num[i] = num[j];
num[j] = a;
printf("%d", num[i]);
If(no==num[i]){
Flag = 1;
If(flag == 1){
printf("found it");
Else{
VANI TANWAR
DIVYANSHU BCA
10
Output:
VANIDIVYANSHU
TANWAR BCA
11
Code:
#include <string.h>
int main () {
char string1[20];
int i, length;
int flag = 0;
length = strlen(string1);
flag = 1;
break;
if (flag) {
} else {
return 0;
Output:
VANI TANWAR
DIVYANSHU BCA
12
Q8. Write a program to add, subtract, multiply and divide two no.s using pointer.
Code:
#include<stdio.h>
void main()
int num1,num2,sub,*p1,*p2,add,div,mul;
clrscr();
scanf("%d",&num1);
scanf("%d",&num2);
p1=&num1;
p2=&num2;
add=*p1+*p2;
sub=*p1-*p2;
mul=*p1 * *p2;
div=*p1/*p2;
getch();
VANI TANWAR
DIVYANSHU BCA
13
Output:
VANI TANWAR
DIVYANSHU BCA
14
Q9. Write a program to create a structure for employees containing the following data members:
Employee ID, Employee Name, Age, Address, Department and Salary. Input data for 10 employees
and display the details of the employee from the employee ID given by the user.
Code:
#include<stdio.h>
struct employee{
int employeeID;
char name[50];
int age;
char address[100];
char department[100];
float salary;
};
int main(){
int i,id;
clrscr();
for(i=1;i<=10;i++){
printf("input the details for\n
%d\nEmployeeid:\nname:\nAge:\nDepartment:\nsalary:\n",i);
scanf("%d",&emp[i].employeeID);
scanf("%s",emp[i].name);
scanf("%d",&emp[i].age);
scanf("%s",emp[i].address);
scanf("%s",emp[i].department);
scanf("%.2f",&emp[i].salary);
scanf("%d",&id);
VANI TANWAR
DIVYANSHU BCA
15
for(i=1;i<=10;i++){
if(id==emp[i].employeeID){
printf("%d employee");
printf("%d",emp[i].employeeID);
printf("%s",emp[i].name);
printf("%d",emp[i].age);
printf("%s",emp[i].address);
printf("%s",emp[i].department);
printf("%.2f",emp[i].salary);
return 0;
}
Output:
VANI TANWAR
DIVYANSHU BCA
16
Q10. : Write a menu driven program to construct a calculator for following Arithmetic Operation:
addition, subtraction, multiplication, division, average and percentage.
Code:
#include <stdio.h>
int main ()
float num1;
float num2;
char op;
float result;
switch(op)
case '-':
break;
case '+':
break;
VANI TANWAR
DIVYANSHU BCA
17
case '*':
break;
case '/':
break;
case 'a':
break;
case 'p':
break;
default:
Return 0;
Output:
VANIDIVYANSHU
TANWAR BCA
18
Q11. write a menu driven program to perform the following operations: (i) Print armstrong
numbers upto N, (ii) Display prime numbers between 1 to N, (iii) Reverse of an integer.
#include<stdio.h>
void main() {
int choice;
clrscr();
while(1) {
printf("4.Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:{
int n,sum,i,t,a;
scanf("%d",&n);
for(i = 1;i<=n;i++)
t = i;
sum = 0;
while(t != 0){
a = t%10;
sum += a*a*a;
t = t/10;
if(sum == i){
printf("%d\n", i);
VANI TANWAR
DIVYANSHU BCA
19
break;
case 2:{
int j,i,count,n;
scanf("%d",&n);
for(i=1;i<=n;i++){
count = 0;
for(j=2;j<=i/2;j++){
if(i%j==0){
count++;
break;
break;
case 3:{
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
n /= 10;
VANI TANWAR
DIVYANSHU BCA
20
break;
case 4:{
exit(0);
default:
Return 0;
VANI TANWAR
DIVYANSHU BCA
21
OUTPUT :
VANI DIVYANSHU
TANWAR BCA
22
Code:
#include <stdio.h>
#include <string.h>
switch(hex) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
VANIDIVYANSHU
TANWAR BCA
23
int main() {
char hexNum[100];
scanf("%s", hexNum);
hexToBinary(hexNum[i]);
printf("\n");
return 0;
Output :
VANI DIVYANSHU
TANWAR BCA
24
Q13. Write a program to calculate factorial of a number and display fibonacci series upto N .
Code:
#include<stdio.h>
if(n>0) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printFibonacci(n-1);
int main () {
int n;
return 0;
Output:
VANI TANWAR
DIVYANSHU BCA
25
Question 15. Write a program to perform (i) matrix addition, (ii) matrix multiplication, and (iii)
Matrix
transpose on 2D arrays
Code:
i. Matrices addtion
#include <stdio.h>
int main() {
scanf("%d", &r);
scanf("%d", &c)
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
DIVYANSHU
VANI TANWAR BCA
26
if (j == c - 1) {
printf("\n\n");
return 0;
Output :
VANI TANWAR
DIVYANSHU BCA
27
Q16. Write a menu-driven program in C to perform the following operations on string using standard
library functions.
Solution:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
char str1[20],str2[20];
int ch,i,j;
do
{printf("\tMENU");
printf("\n------------------------------\n");
printf("\n3:Concatenate Strings");
printf("\n5:Compare Strings");
printf("\n6:Exit");
printf("\n------------------------------\n");
VANIDIVYANSHU
TANWAR BCA
28
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%s",str1);
i=strlen(str1);
break;
scanf("%s",str1);
//strrev(str1);
break;
case 3:
scanf("%s",str1);
scanf("%s",str2);
strcat(str1,str2);
break;
case 4:
scanf("%s",str1);
scanf("%s",str2);
strcpy(str2,str1);
printf("-----------------------------------------------\n");
VANI TANWAR
DIVYANSHU BCA
29
printf("-----------------------------------------------\n");
break;
case 5:
scanf("%s",str1);
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
else
break;
case 6:
exit(0);
break;
default:
}while(ch!=6);
return 0;
VANI TANWAR
DIVYANSHU BCA
30
Output:
VANI DIVYANSHU
TANWAR BCA
31
Q17. Write a program to read time in string format and extract hours,minutes and seconds also
check time validity.
Code:
#include <stdio.h>
int ret=0;
if(hh>24) ret=1;
if(mm>60) ret=1;
if(ss>60) ret=1;
return ret;
// main function
int main()
char string[100]={0};
int ret=0,hour=0,min=0,sec=0;
fgets(string,100,stdin);
VANI TANWAR
DIVYANSHU BCA
32
//printf("\nHH : %d MM : %d SS : %d",hour,min,sec);
ret = ValidateTime(hour,min,sec);
if(ret)
else
return 0;
output:
VANI DIVYANSHU
TANWAR BCA
33
Code:
#include <stdio.h>
#include <string.h>
FILE *fp1 ;
FILE *fp2 ;
int cnt1 = 0;
int cnt2 = 0;
int flg = 0;
printf("Insufficient Arguments!!!\n");
return -1;
fp1 = fopen(argv[1],"r");
return -1;
fseek(fp1,0,SEEK_END);
VANI DIVYANSHU
TANWAR BCA
34
cnt1 = ftell(fp1);
fp2 = fopen(argv[2],"r");
return -1;
fseek(fp2,0,SEEK_END);
cnt2 = ftell(fp2);
fseek(fp1,0,SEEK_SET);
fseek(fp2,0,SEEK_SET);
else
while( ! feof(fp1) )
flg = 1;
break;
VANI DIVYANSHU
TANWAR BCA
35
fclose(fp1);
fclose(fp2);
return 0;
Output:
VANI TANWAR
DIVYANSHU BCA