Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
51 views6 pages

C Practical LAB Exercises List-With Solution

The document provides a list of practical exercises for C programming, including tasks such as calculating the sum and product of digits, printing numbers in words, and implementing a calculator using switch statements. It also covers string operations, matrix manipulations, and various algorithms like checking for prime numbers and palindromes. Additionally, it includes examples of C code for some of the exercises.

Uploaded by

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

C Practical LAB Exercises List-With Solution

The document provides a list of practical exercises for C programming, including tasks such as calculating the sum and product of digits, printing numbers in words, and implementing a calculator using switch statements. It also covers string operations, matrix manipulations, and various algorithms like checking for prime numbers and palindromes. Additionally, it includes examples of C code for some of the exercises.

Uploaded by

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

C Practical Exercises List.

Write a C Program to find/evaluate

1. Sum of Digits and Product of digits, Multiplication Table

2. Print the given number in words.

/**
* C program to print number in words
*/

#include <stdio.h>

int main()
{
int n, num = 0;

/* Input number from user */


printf("Enter any number to print in words: ");
scanf("%d", &n);

/* Store reverse of n in num */


while(n != 0)
{
num = (num * 10) + (n % 10);
n /= 10;
}

/*
* Extract last digit of number and print corresponding digit in words
* till num becomes 0
*/
while(num != 0)
{
switch(num % 10)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}

num = num / 10;


}

return 0;
}
Another Solution:

/* C program to print a given number in words. The program


handles numbers from 0 to 9999 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* A function that prints given number in words */


void convert_to_words(char* num)
{
int len = strlen(
num); // Get number of digits in given number

/* Base cases */
if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr,
"Length more than 4 is not supported\n");
return;
}

/* The first string is not used, it is to make


array indexing simple */
char* single_digits[]
= { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };

/* The first string is not used, it is to make


array indexing simple */
char* two_digits[]
= { "", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen" };

/* The first two string are not used, they are to make
array indexing simple*/
char* tens_multiple[] = { "", "", "twenty",
"thirty", "forty", "fifty",
"sixty", "seventy", "eighty",
"ninety" };

char* tens_power[] = { "hundred", "thousand" };

/* Used for debugging purpose only */


printf("\n%s: ", num);

/* For single digit number */


if (len == 1) {
printf("%s\n", single_digits[*num - '0']);
return;
}

/* Iterate while num is not '\0' */


while (*num != '\0') {

/* Code path for first 2 digits */


if (len >= 3) {
if (*num - '0' != 0) {
printf("%s ", single_digits[*num - '0']);
printf("%s ",
tens_power[len - 3]); // here len can
// be 3 or 4
}
--len;
}

/* Code path for last 2 digits */


else {
/* Need to explicitly handle 10-19. Sum of the
two digits is used as index of "two_digits"
array of strings */
if (*num == '1') {
int sum = *num - '0' + *(num + 1) - '0';
printf("%s\n", two_digits[sum]);
return;
}

/* Need to explicitly handle 20 */


else if (*num == '2' && *(num + 1) == '0') {
printf("twenty\n");
return;
}
/* Rest of the two digit numbers i.e., 21 to 99
*/
else {
int i = *num - '0';
printf("%s ", i ? tens_multiple[i] : "");
++num;
if (*num != '0')
printf("%s ",
single_digits[*num - '0']);
}
}
++num;
}
}

/* Driver program to test above function */


int main(void)
{
convert_to_words("9923");
convert_to_words("523");
convert_to_words("89");
convert_to_words("8");

return 0;
}

3. Print the day of week (Month of a year) ( 1- january/1-Monday)

4. Calculator using switch.

5. Convert Binary to Decimal/ Decimal to Binary.

6. All string operations using built-in functions (strlen, strlwr, strupr, strcat, strcmp, strrev)

7. All string operations without using built-in functions.

8. Get a character or string count/ check for vowels, consonants, special characters

10. Palindrome checking (using built-in/without using built-in)

11. Prime number using function (call by value)

12. Print first and last digit of any given number.

13. Greatest of two numbers using conditional operators ( Three numbers), Negative or positive

14. Using recursion Factorial, Fibonacci series, Reverse of a number

15. Change the value of constant integers using pointers

16. Swap two numbers using pointers


17. Enter student details (book details, Employee details) using structure

18. Copy all elements of an array to another array

19. Find the duplicate elements in an array and create a unique array

20. Addition, Subtraction and Transpose of Matrices.

21. Multiplication of Two matrices.

22. Using files, read write and append the content to a file and display the same.

23. Check for leap year, Armstrong number, odd or even,

24. Area of triangle, square, rectangle using switch case.

25. Print the given pattern

1)

1. #include <stdio.h>
2.
3. int main()
4. {
5. int n,m=1;
6. printf("Enter the number of rows");
7. scanf("%d",&n);
8. for(int i=n;i>=1;i--)
9. {
for(int j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
2)

#include <stdio.h>
int main()
{
int n,m;
printf("Enter the number of rows");
scanf("%d",&n);
m=n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m-1;j++)
{
printf(" ");
}
for(int k=1;k<=2*i-1;k++)
{
printf("*");
}
m--;

printf("\n");
}
return 0;
}

3)

#include <stdio.h>
int main(void) {
int n;
printf("Enter the number of rows\n");
scanf("%d",&n);
int spaces=n-1;
int stars=1;
for(int i=1;i<=n;i++)
{ for(int j=1;j<=spaces;j++)
{
printf(" ");
}
for(int k=1;k<=stars;k++)
{
printf("*");
}
if(spaces>i)
{
spaces=spaces-1;
stars=stars+2;
}
if(spaces<i)
{
spaces=spaces+1;
stars=stars-2;
}
printf("\n");
}
return 0;
}

You might also like