0% found this document useful (0 votes)
32 views18 pages

C Practical Q&A

C practice questions
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)
32 views18 pages

C Practical Q&A

C practice questions
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/ 18

Ex1

//program to add digits of a number

#include <stdio.h>

int main()

int num, k=1, sum=0;

printf("Enter the number whose digits are to be added:\n");

scanf("%d",&num);

while(num!=0)

k=num%10;

sum=sum+k;

k=num/10;

num=k;

printf("Sum of the digits:%d",sum);

return 0;

output: working

Ex2:

Write a C program, which takes two integer operands and one operator form the user

performs the operation and then prints the result.(Consider the operators +,-,*, /, % and

use Switch Statement).

#include<stdio.h>

#include<stdlib.h>

int main()
{

int a,b,res,ch;

printf("\t *********************");

printf("\n\tMENU\n");

printf("\t********************");

printf("\n\t(1)ADDITION");

printf("\n\t(2)SUBTRACTION");

printf("\n\t(3)MULTIPLICATION");

printf("\n\t(4)DIVISION");

printf("\n\t(5)REMAINDER");

printf("\n\t(0)EXIT");

printf("\n\t********************");

printf("\n\n\tEnter your choice:");

scanf("%d",&ch);

if(ch<=5 & ch>0)

printf("Enter two numbers:\n"); scanf("%d%d",&a,&b);

switch(ch)

case 1:

res=a+b; printf("\n Addition:%d",res); break;

case 2:

res=a-b; printf("\n Subtraction:%d",res); break;

case 3:

res=a*b; printf("\n Multiplication:%d",res); break;

case 4:

res=a/b; printf("\n Division:%d",res); break;

case 5:
res=a%b; printf("\n Remainder:%d",res); break;

case 0: printf("\n Choice Terminated"); exit(0); break;

default:

printf("\n Invalid Choice");

return 0;

output: working

Ex 3:

Write a C program - using gets() and puts() do display the input character.

#include<stdio.h>

#include<string.h>

int main()

char c[100]; //Character array with size 100

scanf ("%s",c);

puts(c);

return 0;

Output: running with scanf, gets function depricated

Ex 4:

//program using if-else statements to find largest of 2 integer numbers

#include<stdio.h>
int main()

int x, y;

printf("Enter value for x:\n");

scanf("%d",&x);

printf("Enter value for y:\n");

scanf("%d",&y);

if(x>y)

printf("X is larger number : %d\n",x);

else

printf("Y is larger number : %d\n",y);

return 0;

output: working

Ex 5:

//program to calculate factorial of an integer number using non-recursive function

#include<stdio.h>

int main()

int n,i,f;

printf("Enter the number:\n");


scanf("%d",&n);

f=1;

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

f=f*i;

printf("Factorial of %d=%d",n,f);

return 0;

output:working

//program to calculate factorial of an integer number using recursive function

#include<stdio.h>

long int fact(int n);

int main()

long int n;

printf("\n Enter the number:\n");

scanf("%ld",&n);

printf("Factorial of %ld=%ld",n,fact(n));

return 0;

long int fact(int n)

if(n==0)

return 1;

else

return n*fact(n-1);
}

ouput:working

Ex 6

//program to extract a string from a character string

#include<stdio.h>

#include<string.h>

int main()

char str1[50],str2[50];

int i,j,n,m,l;

printf("Enter the string\n");

scanf ("%s",str1);

printf("Enter the position of the required character:\n ");

scanf("%d",&n);

printf("Enter the required number of characters to be extracted:\n ");

scanf("%d",&m);

l=strlen(str1);

if(m+n-1<l)

for(i=n-1,j=0;i<m+n-1;i++,j++)

str2[j]=str1[i];

str2[j]='\0';

printf ("The extracted string is: %s",str2);

else

printf ("string extraction is not possible");

return 0;
}

output:working

Ex 7

a. Write a C program to find both the largest and smallest number in a list of integers using an array

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int i, j, m, min, max, size, a[20];

printf("Enter the size of array:");

scanf("%d",&i);

printf("Enter the array elements:\n");

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

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

max=a[0];

min=a[0];

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

if(a[j]>max)

max=a[j];

if(a[j]<min)

min=a[j];
}

printf("The maximum number in given array is: %d\n", max);

printf("The minimum number in given array is: %d\n", min);

getch();

output: working

b. Write a C program to add two matrices using arrays

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int a[2][2], b[2][2];

int i, j;

printf("Enter the elements of matrix A:\n");

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

for(j=0;j<2;j++)

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

printf("Enter the elements of matrix B:\n");

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

for(j=0;j<2;j++)
scanf("%d",&b[i][j]);

printf("\n====Matrix Addition====\n");

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

for(j=0;j<2;j++)

printf("%5d", a[i][j]+b[i][j]);

printf("\n");

getch();

output:working

c. Write a program to multiply 2 matrices using arrays.

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int a[2][2], b[2][2], c[10][10];

int i, j, k;

//clrscr();

printf("Enter the elements of matrix A:\n");

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

for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

printf("Enter the elements of matrix B:\n");

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

for(j=0;j<2;j++)

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

printf("\n====Matrix Multiplication====\n");

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

for(j=0;j<2;j++)

c[i][j]=0;

for(k=0;k<2;k++)

c[i][j]+=a[i][k]*b[k][j];

//printing result

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

for(j=0;j<2;j++)

printf("%d\t",c[i][j]);

getch();
}

output: working

Ex 8

Write a C program using structures for reading the employee details like employee name, date of joining
and salary and also to compute total salary outgo for a month

#include<stdio.h>

#include<string.h>

struct Employee

char name[20];

int day;

char month[10];

int year;

float salary;

};

int main()

struct Employee e[20];

int total_salary=0;

int n, i, t;

char month[10];

printf("Enter number of employees\n");

scanf("%d",&n);

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

printf("Input Values\n");
scanf("%s%d%s%d%f",e[i].name,&e[i].day,e[i].month,&e[i].year,&e[i].salary);

printf("%s%d%s%d%f\n",e[i].name,e[i].day,e[i].month,e[i].year,e[i].salary);

printf("enter the month for total salary\n ");

scanf("%s", month);

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

t=strcmp(e[i].month, month);

if(t==0)

total_salary=total_salary+e[i].salary;

printf("Total salary for given month=%d\n", total_salary);

return 0;

output: partly working

#include<stdio.h>

#include<string.h>

struct personal

char name[20];

int day;

char month[10];

int year;

float salary;

};

int main()
{

struct personal person[10];

char month[20];

int i, n;

float m, total_salary=0;

printf("Enter number of entries\n");

scanf("%d",&n);

printf("Input Values\n");

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

scanf("%s %d %s %d %f",person[i].name,&person[i].day,person[i].month,&person[i].year,&m);

person[i].salary=m;

printf("\nOutput Values\n");

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

printf("%s %d %s %d
%f\n",person[i].name,person[i].day,person[i].month,person[i].year,person[i].salary);

printf("enter the month for total salary\n ");

scanf("%s", month);

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

if(strcmp(person[i].month, month)==0)

total_salary=total_salary+person[i].salary;

}
printf("Total salary =%f", total_salary);

return 0;

output: not working properly

Ex 9: Write a C program to use structure within union, display the structure and length of union
elements.

#include <stdio.h>

#include <conio.h>

struct hai

char c;

long l;

char *p;

};

union bye

char c;

long l;

char *p;

};

int main()

struct hai myhai;

union bye mybye;

myhai.c=1;

myhai.l=2L;
myhai.p="This is my myhai";

mybye.c=1;

mybye.l=2L;

mybye.p="This is mybye";

printf("myhai: %d %ld %s\n", myhai.c, myhai.l, myhai.p);

printf("mybye: %d %ld %s\n", mybye.c, mybye.l, mybye.p);

return 0;

output:working

#include <stdio.h>

#include <conio.h>

union bye

int c;

float l;

char *p;

mydata;

int main()

mydata.c=5;

printf("This is an integer: %d\n", mydata.c);

mydata.l=12.35;

printf("This is float:%f\n", mydata.l);


mydata.p="This is my myhai";

printf("This is string: %s\n",mydata.p);

getch();

return 0;

output: working

Ex 10

Write a program using pointers to swap values of two variables

#include <stdio.h>

int main()

int x, y, *a, *b, temp;

printf("Enter values of x and y: \n");

scanf("%d%d", &x, &y);

printf("The values before swapping\nx=%d\ny=%d\n", x,y);

a=&x;

b=&y;

temp=*b;

*b=*a;

*a=temp;

printf("The values after swapping\nx=%d\ny=%d\n", x,y);

// getch();

return 0;

output: working fine


Ex 11

//Write a program to read data from the keyboard, write it to a file called INPUT, again read the same
data from the INPUT file, and copy to that another file and also //display it to screen. check if working on
LMS lab, not able to run on online compiler.

#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=getc(f1))!=EOF)

printf("%c",c);

fclose(f1);

//copying f1 data into f2

//f1=fopen(file1,"r");

f1=fopen("INPUT","r");

if(f1==NULL)

printf("no data");

exit(0);

}
f2=fopen("file2", "w");

if(f2==NULL)

printf("cannot open");

exit(0);

while((c=getc(f1))!=EOF)

putc(c, f2);

printf("completed");

fclose(f1);

fclose(f2);

getch();

return 0;

You might also like