0% found this document useful (0 votes)
12 views35 pages

VANI C Practical File

Uploaded by

Vani Tanwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views35 pages

VANI C Practical File

Uploaded by

Vani Tanwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1

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();

printf("\n Enter the Temparature in Celsius : ");

scanf("%f",&celsius);

fahrenheit = (1.8 * celsius) + 32;

printf("\n Temperature in Fahrenheit : %f ",fahrenheit);

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 ()

int num1, num2, num3;

printf ("Enter first number:");

scanf ("%d”, &num1);

printf ("\nEnter second number:");

scanf ("%d”, &num2);

printf ("\nEnter third number:");

scanf ("%d”, &num3);

if(num1>num2 && num1>num3)

printf ("\nFirst number is greater than other numbers");

else if(num2>num1 && num2>num3)

printf ("\nSecond number is greater than other numbers");

else

printf ("\nThird number is greater than other numbers");

Return 0;

VANI TANWAR
DIVYANSHU BCA
3

Output:

VANI TANWAR
DIVYANSHU BCA
4

Q3. Write a program to check if the given number is prime or not.

Code:

#include <stdio.h>

number.", n);

else{

printf("%d is not a int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

if (n == 0 || n == 1){

flag = 1;

for (i = 2; i <= n / 2; ++i) {

if (n % i == 0) {

flag = 1;

break;

if (flag == 0){

printf("%d is a prime prime number.", n);

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() {

int rows, i, j, number = 1;

clrscr();

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; i++) {

for (j = 1; j <= i; ++j) {

printf("%d ", number);

++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 ()

int marks [50], i, n, sum =0;

double average;

printf ("Enter numbers:");

scanf ("%d”, &n);

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

printf ("Enter number %d:”, i+1);

scanf ("%d”, &marks[i]);

sum += marks[i];

average = (double) sum / n;

printf ("Average = %.2lf", average);

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>

void main (){

int num[20]={3,4,1,5,2,6};

int i, j, a, n , no,flag=0;

printf("enter the number you want to search it");

scanf("%d",&no);

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

for (j = i + 1; j < 6; ++j){

if (num[i] > num[j]){

a = num[i];

num[i] = num[j];

num[j] = a;

printf("The numbers in ascending order is:\n");

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

printf("%d", num[i]);

If(no==num[i]){

Flag = 1;

If(flag == 1){

printf("found it");

Else{

VANI TANWAR
DIVYANSHU BCA
10

printf("we don't found");}

Output:

VANIDIVYANSHU
TANWAR BCA
11

Q7. Write a program to check whether the string is palindrome or not.

Code:

#include <string.h>

int main () {

char string1[20];

int i, length;

int flag = 0;

printf ("Enter a string: ");

scanf ("%s", string1);

length = strlen(string1);

for (i = 0; i < length / 2; i++) {

if (string1[i]! = string1[length - i - 1]) {

flag = 1;

break;

if (flag) {

printf ("%s is not a palindrome\n", string1);

} else {

printf ("%s is a palindrome\n", string1);

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();

printf("Enter 1st number: ");

scanf("%d",&num1);

printf("Enter 2nd number: ");

scanf("%d",&num2);

p1=&num1;

p2=&num2;

add=*p1+*p2;

sub=*p1-*p2;

mul=*p1 * *p2;

div=*p1/*p2;

printf("\n\nDifference of %d and %d is %d",*p1,*p2,sub);

printf("\n\nAddition of %d and %d is %d",*p1,*p2,add);

printf("\n\n multiplication of %d and %d is %d",*p1,*p2,mul);

printf("\n\nDivision of %d and %d is %d",*p1,*p2,div);

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(){

struct employee emp[10];

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);

printf("enter id of employee you want to search");

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;

printf ("Enter the first number:");

scanf ("%f”, &num1);

printf ("Enter the operation:");

scanf (" %c”, &op);

printf ("Enter the second number:");

scanf ("%f”, &num2);

switch(op)

case '-':

result = num1 - num2;

printf ("%f”, result);

break;

case '+':

result = num1 + num2;

printf ("%f”, result);

break;

VANI TANWAR
DIVYANSHU BCA
17

case '*':

result = num1 * num2;

printf ("%f”, result);

break;

case '/':

result = num1 / num2;

printf ("%f”, result);

break;

case 'a':

result = (num1 + num2)/2;

printf ("%f”, result);

break;

case 'p':

result = ((num1 + num2)/200) *100;

printf ("%f”, result);

break;

default:

printf ("the operator is not valid:");

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("1.print armstrong number : \n");

printf("2. Pirnt Prime number : \n");

printf("3.Print reverse number : \n");

printf("4.Exit\n");

printf("\nYour choice :? ");

scanf("%d",&choice);

switch(choice){

case 1:{

int n,sum,i,t,a;

printf("The Armstrong numbers in between 1 to N are : \n\n\n");

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;

printf("Enter max range: ");

scanf("%d",&n);

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

count = 0;

for(j=2;j<=i/2;j++){

if(i%j==0){

count++;

break;

if(count==0 && i!= 1){

printf("prime number: %d\n",i);

break;

case 3:{

int n, reverse = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0) {

remainder = n % 10;

reverse = reverse * 10 + remainder;

n /= 10;

VANI TANWAR
DIVYANSHU BCA
20

printf("Reversed number = %d\n", reverse);

break;

case 4:{

exit(0);

default:

printf("you choose wrong choice");

Return 0;

VANI TANWAR
DIVYANSHU BCA
21

OUTPUT :

VANI DIVYANSHU
TANWAR BCA
22

Q12. Write a program to convert a hexadecimal to binary.

Code:

#include <stdio.h>

#include <string.h>

// Function to convert hexadecimal digit to binary string

void hexToBinary(char hex) {

switch(hex) {

case '0': printf("0000"); break;

case '1': printf("0001"); break;

case '2': printf("0010"); break;

case '3': printf("0011"); break;

case '4': printf("0100"); break;

case '5': printf("0101"); break;

case '6': printf("0110"); break;

case '7': printf("0111"); break;

case '8': printf("1000"); break;

case '9': printf("1001"); break;

case 'A':

case 'a': printf("1010"); break;

case 'B':

case 'b': printf("1011"); break;

case 'C':

case 'c': printf("1100"); break;

case 'D':

case 'd': printf("1101"); break;

case 'E':

case 'e': printf("1110"); break;

case 'F':

case 'f': printf("1111"); break;

VANIDIVYANSHU
TANWAR BCA
23

default: printf("Invalid hexadecimal digit\n");

int main() {

char hexNum[100];

// Input hexadecimal number as a string

printf("Enter a hexadecimal number: ");

scanf("%s", hexNum);

// Print the binary equivalent of each hexadecimal digit

printf("The binary equivalent is: ");

for (int i = 0; i < strlen(hexNum); i++) {

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 .

terms using recursive functions.

Code:

#include<stdio.h>

void printFibonacci (int n) {

static int n1=0, n2=1, n3;

if(n>0) {

n3 = n1 + n2;

n1 = n2;

n2 = n3;

printf ("%d “, n3);

printFibonacci(n-1);

int main () {

int n;

printf ("Enter the number of elements: ");

scanf ("%d”, &n);

printf ("Fibonacci Series: ");

printf ("%d %d ",0,1);

printFibonacci(n-2) ;//n-2 because 2 numbers are already printed

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() {

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

printf("Enter the number of rows (between 1 and 100): ");

scanf("%d", &r);

printf("Enter the number of columns (between 1 and 100): ");

scanf("%d", &c)

printf("\nEnter elements of 1st matrix:\n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

printf("Enter elements of 2nd matrix:\n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("Enter element b%d%d: ", i + 1, j + 1);

scanf("%d", &b[i][j]);

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

sum[i][j] = a[i][j] + b[i][j];

DIVYANSHU
VANI TANWAR BCA
26

printf("\nSum of two matrices: \n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("%d ", sum[i][j]);

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.

i) Calculate Length of String

ii) Reverse a given String

iii) Concatenation of one string to another

iv) Copy one String into another

v) Compare two Strings

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("1:Find Length of String");

printf("\n2:Find Reverse of String");

printf("\n3:Concatenate Strings");

printf("\n5:Copy String ");

printf("\n5:Compare Strings");

printf("\n6:Exit");

printf("\n------------------------------\n");

printf("\nEnter your choice: ");

VANIDIVYANSHU
TANWAR BCA
28

scanf("%d",&ch);

switch(ch)

case 1:

printf("Enter String: ");

scanf("%s",str1);

i=strlen(str1);

printf("Length of String : %d\n\n",i);

break;

case 2: printf("Enter String: ");

scanf("%s",str1);

//strrev(str1);

printf("Reverse string : %s\n\n",str1);

break;

case 3:

printf("\nEnter First String: ");

scanf("%s",str1);

printf("Enter Second string: ");

scanf("%s",str2);

strcat(str1,str2);

printf("String After Concatenation : %s\n\n",str1);

break;

case 4:

printf("Enter a String1: ");

scanf("%s",str1);

printf("Enter a String2: ");

scanf("%s",str2);

printf("\nString Before Copied:\nString1=\"%s\",String2=\"%s\"\n",str1,str2);

strcpy(str2,str1);

printf("-----------------------------------------------\n");

printf("\"We are copying string String1 to String2\" \n");

VANI TANWAR
DIVYANSHU BCA
29

printf("-----------------------------------------------\n");

printf("String After Copied:\nString1=\"%s\", String2=\"%s\"\n\n",str1,str2);

break;

case 5:

printf("Enter First String: ");

scanf("%s",str1);

printf("Enter Second String: ");

scanf("%s",str2);

j=strcmp(str1,str2);

if(j==0)

printf("Strings are Same\n\n");

else

printf("Strings are Not Same\n\n");

break;

case 6:

exit(0);

break;

default:

printf("Invalid Input. Please Enter valid Input.\n\n ");

}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>

// function to validate the time

int ValidateTime(int hh , int mm , int ss)

int ret=0;

if(hh>24) ret=1;

if(mm>60) ret=1;

if(ss>60) ret=1;

return ret;

// main function

int main()

// declare a char buffer

char string[100]={0};

// declare some local int variables

int ret=0,hour=0,min=0,sec=0;

printf("\nEnter the time in \"hh:mm:ss\" format : ");

fgets(string,100,stdin);

// fetch the hour,min and sec values from the

VANI TANWAR
DIVYANSHU BCA
32

// string and then store it in int variables

// in order to validate them

sscanf(string , "%d:%d:%d" , &hour,&min,&sec);

//printf("\nHH : %d MM : %d SS : %d",hour,min,sec);

// validate the time

ret = ValidateTime(hour,min,sec);

if(ret)

printf("\nInvalid Time. Try Again.\n");

else

printf("\nThe Time is : %d:%d:%d\n",hour,min,sec);

return 0;

output:

VANI DIVYANSHU
TANWAR BCA
33

Q18./Write a program to compare two file.

Code:

#include <stdio.h>

#include <string.h>

int main(int argc, char *argv[])

FILE *fp1 ;

FILE *fp2 ;

int cnt1 = 0;

int cnt2 = 0;

int flg = 0;

if( argc < 3 )

printf("Insufficient Arguments!!!\n");

printf("Please use \"program-name file-name1 file-name2\" format.\n");

return -1;

fp1 = fopen(argv[1],"r");

if( fp1 == NULL )

printf("\n%s File can not be opened : \n",argv[1]);

return -1;

// move file pointer to end and get total number of bytes

fseek(fp1,0,SEEK_END);

VANI DIVYANSHU
TANWAR BCA
34

cnt1 = ftell(fp1);

fp2 = fopen(argv[2],"r");

if( fp2 == NULL )

printf("\n%s File can not be opened : \n",argv[2]);

return -1;

// move file pointer to end and get total number of bytes

fseek(fp2,0,SEEK_END);

cnt2 = ftell(fp2);

fseek(fp1,0,SEEK_SET);

fseek(fp2,0,SEEK_SET);

// check for the total number of bytes

if( cnt1 != cnt2 ){

printf("\nFile contents are not same\n");

else

while( ! feof(fp1) )

if( fgetc(fp1) != fgetc(fp2) )

flg = 1;

break;

VANI DIVYANSHU
TANWAR BCA
35

if( flg ) printf("\nFile contents are not same.\n");

else printf("\nFile contents are same.\n");

fclose(fp1);

fclose(fp2);

return 0;

Output:

VANI TANWAR
DIVYANSHU BCA

You might also like