Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Practice Problems C Language Programming

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

Program 5: Write a program to check whether entered year is leap or not.


#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d isn't a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else //
printf("%d isn't a leap year.\n", year);
return 0;
}

Page | 7
Practice Problems C Language Programming

Program 6: WAP to Find out the Roots of a Quadratic Equation.


#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, discr, root1, root2, realPart, imgPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
discr = b*b-4*a*c;
// condition for real and different roots
if (discr > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(discr))/(2*a);
root2 = (-b-sqrt(discr))/(2*a);
printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
}
//condition for real and equal roots
else if (discr == 0)
{
root1 = root2 = -b/(2*a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else
{
realPart = -b/(2*a);
imgPart = sqrt(-discr)/(2*a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imgPart,
realPart, imgPart);
}
return 0;
}

Page | 8
Practice Problems C Language Programming

Program 7: Write a program to print grade of a student based on marks of 5 subjects


entered by user.
#include <stdio.h>
int main()
{
int phy, chem, bio, math, comp;
float per;

/* Input marks of five subjects from user */


printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);

/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0;

printf("Percentage = %.2f\n", per);

/* Find grade according to the percentage */


if(per >= 90)
{
printf("Grade A");
}
else if(per >= 80)
{
printf("Grade B");
}
else if(per >= 70)
{
printf("Grade C");
}
else if(per >= 60)
{
printf("Grade D");
}
else if(per >= 40)
{
printf("Grade E");
}
else
{
printf("Grade F");
}

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

Program 11: Write a program to print series of arm-strong numbers from m to n. m, n


will be inputted by user.
#include <stdio.h>
int main()
{
int i, j, cur, lastDigit, m, n;
long fact, sum;
printf("Enter lower and upper limit: ");
scanf("%d%d", &m,&n);
printf("Armstrong numbers between %d to %d are:\n", m,n);
for(i=m; i<=n; i++)
{
cur = i;
sum = 0;
while(cur > 0)
{
fact = 1ll;
lastDigit = cur % 10;
for( j=1; j<=lastDigit; j++)
{
fact = fact * j;
}
sum += fact;
cur /= 10;
}
if(sum == i)
{
printf("%d, ", i);
}
}
return 0;
}

Page | 13
Practice Problems C Language Programming

Program 12: Write a program to search an element from an array.


#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements of array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);

/* Assume that element does not exists in array */


found = 0;

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


{
/*
* If element is found in array then raise found flag
* and terminate from loop.
*/
if(arr[i] == toSearch)
{
found = 1;
break;
}
}

/*
* 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

Program 13:Write a program to perform various matrix operations


Addition, Subtraction, Multiplication, Transpose using switch-case
statement

#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

printf("Enter 2 for multiplication of matrices\n");


printf("Enter 3 for transpose of first matrices\n");
}
else
{
printf("Enter 3 for transpose of first matrices\n");
}
scanf("%d",&ch);
switch(ch)
{
case 1 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];
sub[i][j]=a[i][j]-b[i][j];
}
}
printf("Enter 1 for Addition or 2 for Subtraction: ");
scanf("%d",&opt);
switch(opt)
{
case 1 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sum[i][j]);
}
printf("\n");
}
break;
case 2 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sub[i][j]);
}
printf("\n");
}
}
break;

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

Program 14:Write a program to illustrate various strings inbuilt


functions (strrev, strcmp, strlen,strcpy, strcat…)
#include<stdio.h>
#include<string.h>
int main()
{
char str1[30];
char str2[30];

printf("Enter string 1: ");


gets(str1);

//copy str1 into str2


strcpy(str2,str1);

printf("str1: %s \nstr2: %s \n",str1,str2);

char title[5],fName[30],lName[30];
char name[100]={0}; //assign null

printf("Enter title (Mr./Mrs.): "); gets(title);


printf("Enter first name: "); gets(fName);
printf("Enter last name: "); gets(lName);

//create complete name using string concatenate


strcat(name,title);
strcat(name," ");

strcat(name,fName);
strcat(name," ");

strcat(name,lName);
strcat(name," ");

printf("Hi.... %s\n",name);

char str3[30]={0};
char str4[30]={0};

printf("Enter string1: "); gets(str3);

//copy first 3 characters of str1


//before copy insert null in target string

strncpy(str4,str3,3);

printf("After copying str2 is: %s\n",str4);

char str5[30];
char str6[30];

printf("Enter string1: "); gets(str5);


printf("Enter string2: "); gets(str6);

//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 stringCmp (char *s1,char *s2);


void stringCpy(char* s1,char* s2);
void stringCat (char *s1,char *s2);

int main()
{
char str1[100],str2[100];

printf("Enter string 1: ");


scanf("%[^\n]s",str1);//read string with spaces

stringCpy(str2,str1);

printf("String 1: %s \nString 2: %s\n",str1,str2);

char str3[100],str4[100];

printf("Enter string 1 : ");


scanf("%[^\n]s",str3);//read string with spaces
printf("\n");

getchar(); //to read enter after first string

printf("Enter string 2 : ");


scanf("%[^\n]s",str4);//read string with spaces

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

printf("Enter string 1 : ");


scanf("%[^\n]s",str5);//read string with spaces

getchar();//read enter after entering first string

printf("Enter string 2 : ");


scanf("%[^\n]s",str6);//read string with spaces

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;

printf("Enter a string: ");


scanf("%[^\n]s",str);//read string with spaces

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

//assign NULL in the revStr


revStr[j]='\0';

printf("\nOriginal String is: %s",str);


printf("\nReversed String is: %s",revStr);

return 0;
}

void stringCpy(char* s1,char* s2)


{
int i=0;
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
}
int stringCmp (char *s1,char *s2)
{
int i=0;
for(i=0; s1[i]!='\0'; i++)
{
if(s1[i]!=s2[i])
return 1;
}
return 0;
}
void stringCat (char *s1,char *s2)
{
int len,i;
len=strlen(s1)+strlen(s2);
if(len>100)
{
printf("\nCan not Concatenate !!!");
return;
}

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

Program 16:Illustrate the concept of call by value vs. call by


reference by taking example of swapping of two numbers.
#include <stdio.h>

/* Swap function definition */


void swap1(int num1, int num2)
{
int temp;

printf("In Function values before swapping: %d %d\n", num1, num2);

temp = num1;
num1 = num2;
num2 = temp;

printf("In Function values after swapping: %d %d\n\n", num1, num2);


}
void swap(int * num1, int * num2)
{
int temp;

printf("In Function values before swapping: %d %d\n", *num1, *num2);

temp = *num1;
*num1 = *num2;
*num2 = temp;

printf("In Function values after swapping: %d %d\n\n", *num1, *num2);


}

int main()
{
int n1, n2;
printf("By value:\n");

/* Input two integers from user */


printf("Enter two numbers: ");
scanf("%d%d", &n1, &n2);

/* Print value of n1 and n2 in before swapping */


printf("In Main values before swapping: %d %d\n\n", n1, n2);

/* Function call to swap n1 and n2 */


swap1(n1, n2);
printf("In Main values after swapping: %d %d", n1, n2);
printf("\n");

printf("By ref:\n");
int n3, n4;

printf("Enter two numbers: ");


scanf("%d%d", &n3, &n4);

printf("In Main values before swapping: %d %d\n\n", n3, n4);

/*
* &n1 - & evaluate memory address of n1
* &n2 - & evaluate memory address of n2
*/
swap(&n3, &n4);

Page | 25
Practice Problems C Language Programming

printf("In Main values after swapping: %d %d", n3, n4);

return 0;
}

Page | 26
Practice Problems C Language Programming

Program 17:Write a recursive function for computing factorial of a


number. Write main to test its functioning.

#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

Program 18:Write a program to read an array of elements and print


the same in the reverse order along with their addresses

#include<stdio.h>

void main() {
int size, i, arr[30];
int *ptr;

ptr = &arr[0];

printf("\nEnter the size of array : ");


scanf("%d", &size);

printf("\nEnter %d integers into array: ", size);


for (i = 0; i < size; i++) {
scanf("%d", ptr);
ptr++;
}

ptr = &arr[size - 1];

printf("\nElements of array in reverse order are :");

for (i = size - 1; i >= 0; i--) {


printf("\nElement%d is %d : ", i, *ptr);
ptr--;
}

Page | 28
Practice Problems C Language Programming

Program 19:Write a function code that is returning pointer to the


larger value out of two passes values.

#include <stdio.h>
#include <stdlib.h>

// return pointer to location from function


double * BigEl(double* arr, double arrSize)
{
int i;
// initialise both variables
double maximum = arr[0], *max_pos = arr;

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


{
if (arr[i]>maximum)
{
maximum = arr[i];
// assign address here
max_pos = &arr[i];
}
}
// return address
return max_pos;
}

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

Program 20: Define a structure type, personal, that would contain


person name, date of joining and salary. Using this structure,
write a program to read this information for one person from the
key board and print the same on the screen.

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

Program 21:Write a program to store a character string in block of


memory space created by malloc and then modify the same to store a
large string.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, i, *ptr, sum = 0;

printf("Enter number of elements: ");


scanf("%d", &num);

ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements of array: ");


for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);


free(ptr);
return 0;
}

Page | 31

You might also like