Professional Documents
Culture Documents
C Language Programming-Practise Probs by Er - Anchal Kamra PDF
C Language Programming-Practise Probs by Er - Anchal Kamra PDF
List of experiments
Pract Sub
Aim
ical # part
Write a program that reads two nos. from key board and gives their addition, subtraction,
1
multiplication, division and modulo.
1
The distance between two cities (In KM) is input through key board. Write a program to
2
convert and print this distance in meters, feet, inches & centimeters.
3 WAP which implements the working of all Bit-wise operators.
4 WAP to find largest out of three numbers by using ternary operator.
2
5 WAP to check whether entered year is leap or not.
Note: For century year, check number by 400 instead 4.
6 WAP to Find out the Roots of a Quadratic Equation.
7 WAP to print grade of a student based on marks of 5 subjects entered by user.
Write a menu driven program that allow the user to perform any one of the following
3 operations based on the input given by user
a. check number is even or odd
8
b. check number is positive or negative
c. printing square of the number
d. printing square root of the number
9 WAP to find sum of all integers greater than 100 & less than 200 and are divisible by 5.
𝑥 𝑥2 𝑥3
4 10 Write a C program to evaluate 𝑒 𝑥 = 1 + 1! + + + ⋯,
2! 3!
Write a program to print series of arm-strong numbers from m to n. m, n will be input by
11
user. Armstrong are those numbers where number= sum of cubes of digits.
12 Write a program to search an element from an array.
5 Write a program to perform various matrix operations Addition, Subtraction, Multiplication,
13
Transpose using switch-case statement.
Write a program to illustrate various string inbuilt functions (strrev, strcmp, strlen, strcpy,
14
6 strcat…)
15 Write user defined functions for all the inbuilt functions of the above Program.
Illustrate the concept of call by value vs. call by reference by taking example of swapping of
16
two numbers.
7
Write a recursive function for computing factorial of a number. Write main to test its
17
functioning.
WAP to read an array of elements and print the same in the reverse order along with their
18
8 addresses
19 Write a function code that is returning pointer to the larger value out of two passed values.
Define a structure type, personal, that would contain person name, date of joining and salary.
9 20 Using this structure, write a program to read this information for one person from the key
board and print the same on the screen.
Page | 1
Practice Problems C Language Programming
Page | 2
Practice Problems C Language Programming
Program 1: Write a program that reads two nos. from key board and gives their
addition, subtraction, multiplication, division and modulo.
#include <stdio.h>
int main()
{
int first, second, add, sub, mult, mod, div;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
sub = first - second;
mult = first * second;
div = first / second;
mod = first % second;
printf("Sum = %d\n", add);
printf("Difference = %d\n", sub);
printf("Multiplication = %d\n", mult);
printf("Division = %d\n", div);
printf("Modulo = %d\n", mod);
return 0;
}
Page | 3
Practice Problems C Language Programming
Program 2: The distance between two cities (In KM) is input through key board. Write
a program to convert and print this distance in meters, feet, inches & centimeters.
#include<stdio.h>
#include<conio.h>
void main()
{
float km,m,feet,inch,cm;
printf("Enter the distance between two cities(in km) - ");
scanf("%f",&km);
m = km*1000;
feet= km*3280.84;
inch=km*39370.1;
cm=km*100000;
printf("\nDistance in kilometres = %f ",km);
printf("\nDistance in metres = %f ",m);
printf("\nDistance in feet = %f ",feet);
printf("\nDistance in inches = %f ",inch);
printf("\nDistance in centimetres = %f ",cm);
getch();
}
Page | 4
Practice Problems C Language Programming
Program 3: Write a program which implements the working of all Bit-wise operators.
#include<stdio.h>
int main()
{
unsigned char a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b);
printf("a|b = %d\n", a|b);
printf("a^b = %d\n", a^b);
printf("~a = %d\n", a = ~a);
printf("b<<1 = %d\n", b<<1);
printf("b>>1 = %d\n", b>>1);
return 0;
}
Page | 5
Practice Problems C Language Programming
Program 4: Write a program to find largest out of three numbers by using ternary
operator.
# include <stdio.h>
void main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
}
Page | 6
Practice Problems C Language Programming
Page | 7
Practice Problems C Language Programming
Page | 8
Practice Problems C Language Programming
/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0;
return 0;
}
Page | 9
Practice Problems C Language Programming
Program 8: Write a menu driven program that allows the user to perform any one of
the following operation based on the input given by user;
i. check number is even or odd
ii. check number is positive or negative
iii. printing square of the number
iv. printing square root of the number
#include<stdio.h>
#include<conio.h>
void main()
{
int choice, num, I, Square;
double number, root;
clrscr();
while(1)
{
printf("1. Odd/Even \n");
printf("2.Positive/Negative\n");
printf("3. Square of number\n");
printf("4. Square root of numberExit\n");
printf("5. Exit\n");
printf("\nYour choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter number:");
scanf("%d",&num);
if(num %2==0)
printf("\n Even number.\n");
else
printf("\nOdd number.\n");
break;
case 2:
printf("Enter a number: ");
scanf("%lf", &number);
if (number <= 0.0)
{
if (number == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("You entered a positive number.");
break;
case 3:
printf(" \n Please Enter any integer Value : ");
scanf("%d", &num);
Square = num * num;
printf("\n Square of a given number %d is = %d", num, Square);
break;
case 4:
printf("Enter any number to find square root: ");
scanf("%lf", &number);
root = sqrt(number);
printf("Square root of %.2lf = %.2lf", number, root);
break;
case5:
exit();
Page | 10
Practice Problems C Language Programming
}
}
}
getch();
}
Program 9: Write a program to find sum of all integers greater than 100 & less than
200 and are divisible by 5.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum=0;
clrscr();
printf("All nos. between 100 - 200 which is divisible by 7\n");
for(i=101;i<200;i++)
{
if(i%7==0)
{
printf("%5d",i);
sum+=i;
}
}
printf("\n\nsum = %d",sum);
getch();
}
Page | 11
Practice Problems C Language Programming
𝑥 𝑥2 𝑥3
Program 10: Write a C program to evaluate 𝑒 𝑥 = 1 + + + + ⋯,
1! 2! 3!
#include <stdio.h>
void main()
{
float x,sum,no_row;
int i,n;
printf("Input the value of x :");
scanf("%f",&x);
printf("Input number of terms : ");
scanf("%d",&n);
sum =1; no_row = 1;
for (i=1;i<n;i++)
{
no_row = no_row*x/(float)i;
sum =sum+ no_row;
}
printf("\nThe sum is : %f\n",sum);
}
Page | 12
Practice Problems C Language Programming
Page | 13
Practice Problems C Language Programming
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
/*
* If element is not found in array
*/
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}
return 0;
}
Page | 14
Practice Problems C Language Programming
Page | 15
Practice Problems C Language Programming
#include<stdio.h>
#include<conio.h>
int main()
{
int m,n,a[20][20],b[20][20],i,j,sum[20][20],sub[20][20],opt,tr[20][20],opt1,ch,e,f;
printf("Note : For Addition or Subtraction , no. of rows and columns should be same and for
transpose of matrices , your first matrices entered should be the desired matrices .\n");
printf("Enter the no. of rows: ");
scanf("%d",&m);
printf("Enter the no. of columns: ");
scanf("%d",&n);
printf("Enter the Data Elements of first matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
} printf("Enter the no. of rows for second matrices: ");
scanf("%d",&e);
printf("Enter the no. of columns: ");
scanf("%d",&f);
printf("Enter the Data Elements of second matrices\n");
for(i=0;i<e;i++)
{
for(j=0;j<f;j++)
{
scanf("%d",&b[i][j]);
}
}
do
{
if(m==e&&n==f)
{
printf("Enter 1 for addtion or subtraction of matrices\n");
if(n==e){printf("Enter 2 for multiplication of matrices\n");}
printf("Enter 3 for transpose of first matrices\n");
}
else if(m!=n&&n==e)
{
Page | 16
Practice Problems C Language Programming
Page | 17
Practice Problems C Language Programming
case 2 :
printf("The resultant matrices is : \n");
int k;
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
{ sum[i][j]=0;
for(k=0;k<m;k++)
{
sum[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",sum[i][j]);
}
printf("\n");
}
break;
case 3 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
tr[j][i]=a[i][j];
}
}
printf("The resultant matrices is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%3d",tr[i][j]);
}
printf("\n");
}
break; }}
while(ch>0);
getch();
}
Page | 18
Practice Problems C Language Programming
Page | 19
Practice Problems C Language Programming
char title[5],fName[30],lName[30];
char name[100]={0}; //assign null
strcat(name,fName);
strcat(name," ");
strcat(name,lName);
strcat(name," ");
printf("Hi.... %s\n",name);
char str3[30]={0};
char str4[30]={0};
strncpy(str4,str3,3);
char str5[30];
char str6[30];
//using strcmp
printf("Using strcmp:\n");
if(strcmp(str5,str6)==0)
printf("strings are same.\n");
else
printf("strings are not same.\n");
Page | 20
Practice Problems C Language Programming
return 0;
}
Page | 21
Practice Problems C Language Programming
Program 15: Write user defined functions for all the inbuilt functions of the above
Program.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
stringCpy(str2,str1);
char str3[100],str4[100];
if(!stringCmp(str3,str4))
printf("\n stringCmp :String are same.");
else
printf("\n stringCmp :String are not same.");
printf("\n");
char str5[100],str6[100];
stringCat(str5,str6);
printf("\nAfter concatenate strings are :\n");
printf("String 1: %s \nString 2: %s",str5,str6);
printf("\n");
char str[100],revStr[100];
int i,j;
Page | 22
Practice Problems C Language Programming
/*copy characters from last index of str and store it from starting in revStr*/
j=0;
for(i=(strlen(str)-1); i>=0;i--)
revStr[j++]=str[i];
return 0;
}
len=strlen(s1);
for(i=0;i< strlen(s2); i++)
{
s1[len+i]=s2[i];
}
s1[len+i]='\0'; /* terminates by NULL*/
Page | 23
Practice Problems C Language Programming
Page | 24
Practice Problems C Language Programming
temp = num1;
num1 = num2;
num2 = temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
int main()
{
int n1, n2;
printf("By value:\n");
printf("By ref:\n");
int n3, n4;
/*
* &n1 - & evaluate memory address of n1
* &n2 - & evaluate memory address of n2
*/
swap(&n3, &n4);
Page | 25
Practice Problems C Language Programming
return 0;
}
Page | 26
Practice Problems C Language Programming
#include <stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Page | 27
Practice Problems C Language Programming
#include<stdio.h>
void main() {
int size, i, arr[30];
int *ptr;
ptr = &arr[0];
Page | 28
Practice Problems C Language Programming
#include <stdio.h>
#include <stdlib.h>
int main()
{
double myarr[2];
double * max_pos;
int i;
printf("Please insert 2 numbers to the array\n");
for (i = 0; i < 2; i++)
{
scanf("%lf", &myarr[i]);
}
// use return value here
max_pos = BigEl(myarr, 2);
return 0;
}
Page | 29
Practice Problems C Language Programming
struct personal {char name[20]; int day; char month[10]; int year; float salary; };
int main()
{struct personal person;
printf("Input Values\n");
scanf("%s %d %s %d %f", person.name, &person.day, person.month, &person.year, &person.salary);
printf("%s %d %s %d %f\n",person.name, person.day, person.month, person.year, person.salary); }
Page | 30
Practice Problems C Language Programming
int main()
{
int num, i, *ptr, sum = 0;
Page | 31