0% found this document useful (0 votes)
9 views

C lab exercises

The document contains multiple C programs that perform various tasks, including calculating areas of shapes, finding the largest number, reversing digits, computing GCD, generating prime numbers, and more. Each program is accompanied by code snippets and sample outputs demonstrating their functionality. The programs cover a wide range of topics, from basic arithmetic to string manipulation and array handling.

Uploaded by

Roopa Sk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C lab exercises

The document contains multiple C programs that perform various tasks, including calculating areas of shapes, finding the largest number, reversing digits, computing GCD, generating prime numbers, and more. Each program is accompanied by code snippets and sample outputs demonstrating their functionality. The programs cover a wide range of topics, from basic arithmetic to string manipulation and array handling.

Uploaded by

Roopa Sk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Write a C program to find the area of a circle and area of a triangle


given three sides.
// C program to find the area of
// the circle using radius
#include <math.h>
#include <stdio.h>
#define PI 3.142
// Function to find the area of
// of the circle
double findArea(int r) { return PI * r * r; }
// Driver code
int main()
{
printf("Area is %f", findArea(5));
return 0;
}
Output
Area is 78.550000
1. To find area of a triangle given three sides.
#include <stdio.h>
#include <math.h> /* It is ecessary for using sqrt function */
void main()
{
/*Variable Declaration*/
float a, b, c, s, area;
/*Taking user input*/
printf("Enter the three sides, a, b, and c, of the triangle:");
scanf("%f%f%f", &a, &b, &c);
/*Calculate the area of the triangle*/
s = (a + b + c) / 2;
area = sqrt((s *(s - a) *(s - b) *(s - c)));
printf("\n The area of the triangle is %f.", area);
}
Output
Enter the three sides a, b, c of the triangle:
3
4
3
The area of the triangle is:4.472136

2. Write a C program to find largest of three numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int c = 1,b = 22, a =48;
// Finding largest by comparing using relational operators
if (a >= b) {
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else {
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}

return 0;
}
Output
48 is the largest number

3. Write a C program to Reverse the digits of an integer.


#include <stdio.h>
#include<conio.h>
// Iterative function to
// reverse digits of num
int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Driver code
int main()
{
int num = 1234;
printf("Reverse of is %d", reverseDigits(num));

//getchar();
return 0;
}
Output
Reverse is 4321

4. Write a C program to find GCD of two integers.


// C program to find GCD of two numbers
#include <math.h>
#include <stdio.h>

// Function to return gcd of a and b


int gcd(int a, int b)
{
// Find Minimum of a and b
int result = ((a < b) ? a : b);
while (result > 0) {
// Check if both a and b are divisible by result
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
// return gcd of a nd b
return result;
}

// Driver program to test above function


int main()
{
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}

Output
GCD of 98 and 56 is 14

5. Write a C Program to generate Prime numbers.


#include<stdio.h>
#include<conio.h>
void main(){
int i, num, n, count;
printf("Enter the range: ");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
Output
Enter the range:50
The prime numbers in between the range 1 to 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
6. Write a C program to demonstrate ccomputing nth Fibonacci
numbers.
#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}
Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

7. Write a C program to find even and odd numbers.


#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}
Output
Enter an integer: 8
8 is a even number

8. Write a C program to exchange the values of two variables.


#include <stdio.h>
int main() {
int x,y;
scanf("%d%d",&x,&y,printf("Enter two Numbers"));
printf("Before Swap x = %d and y = %d\n",x,y);
x = x + y; // 300 = 100 + 200
y = x - y; // 100 = 300 - 200
x = x - y; // 200 = 300 - 100
printf("After Swap x = %d and y = %d",x,y);
return 0;
}
Output
Enter two numbers
6
5
Before swap x=6 and y=5
After swap x=5 and y=6
9. Write a C program to Print number from 100 to 200
which are divisible by 7 and display their sum and
count using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int d,i=100,count=0,val;
clrscr();
printf("The numbers which are divisible by 7\nbetween 100 and 200: \
n");
while(i<200)
{
if(i%7==0)
{
printf("\n");
count=count+1;
val=i;
printf("%d",val);
}
i=i+1;
}
printf("\n");
printf("count=%d",count);
getch();
}
Output
The number which are divisible by 7
Between 100 to 200:
105
112
119
126
133
140
147
154
161
168
175
182
189
196
Count=14
10. Write a C program to calculate Sum of natural numbers.

#include <stdio.h>
int main()
{
int i, s = 0;
int n = 10;
i = 1;

// while loop executes


// the statements until the
// condition is false
while (i <= n) {

// adding natural numbers


// up to given number n
s += i;
i++;
}
// printing the result
printf("Sum = %d", s);
return 0;
}
Output
Sum=55

11. Write a C program to display factorial computation.


#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Output
Enter a number:5
Factorial of 5 is :120
12. Write a C program for the generation of Fibonacci sequence.
#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
if(i < n) {
printf(", ");
}
}
return 0;
}
Output
Enter the number in terms:3
Fibonacci series:0,1,1

13.Write a C program to revese of an array.


#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop;
for(loop = 9; loop >= 0; loop--)
printf("%d ", array[loop]);
return 0;
}
Output
0,9,8,7,6,5,4,3,2,1

14.Write a C program to find maximum number in a set.


#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, largest;
largest = array[0];

for(loop = 1; loop < 10; loop++) {


if( largest < array[loop] )
largest = array[loop];
}

printf("Largest element of array is %d", largest);

return 0;
}
Output
Largest element of array is 9

15. Write a C program to display the removal duplicate from an


ordered array
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array");
scanf("%d",&number);
printf("Enter Elements of the array");
for(i=0;i<number;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are:");

for(i=0;i<number;i++){
printf("%d ",a[i]);
}
for(i=0;i<number;i++){
for(j = i+1; j < number; j++){
if(a[i] == a[j]){
for(k = j; k <number; k++){
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("After deleting the duplicate element the Array is:");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
return 0;
}

Output
Enter the size of array:3
Enter the elements of the array:
2
2
3
Entered elements are 2,2,3
After deleting the duplicate element array is:
2
3

16.Write a C program to demonstrate two dimensional array.


#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}
Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6

17. Write a C program to find Kth element of an array.


#include <stdio.h>
int main()
{
int arr[100], len, i, j, temp, n;
printf("Enter the size of array");

scanf("%d", &len);
printf("\n Enter the array elements");

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


{
scanf("%d", &arr[i]);
}

printf("\n Enter Which kth Number You want");


scanf("%d", &n);
printf("\n The %d th kth number is: %d", n, arr[n - 1]);
return 0;
}
Output
Enter the size of array4

Enter the array elements


12
13
17
20
Enter Which kth Number You want 4

The 4th kth number is: 20

18. Write a C program to read N (minimum 3) students marks and find number of
students passed and fail depending on the marks.
#include <stdio.h>
#include<conio.h>
main()
{
int s1,s2,s3,s4,s5;
printf("Enter marks of sub1,sub2,sub3,sub4,sub5");
scanf("%d%d%d",&s1,&s2,&s3);
if(s1>40 && s2>40 && s3>40)
{
printf("Pass");
}
else
{
printf("Fail");
}
return 0;
}
Output
Enter marks of sub1,sub2 ,sub3
55
80
75
Pass

19. Write a C program to count the number of vowels, consonants and special
characters in a given sentence.

#include <stdio.h>
#include <string.h>

// Function to count vowels and consonants


void stringcount(char* s)
{
static int i, vowels = 0, consonants = 0;

if (!s[i]) {
printf("\nVowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
return;
}
else {
if ((s[i] >= 65 && s[i] <= 90)
|| (s[i] >= 97 && s[i] <= 122)) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
|| s[i] == 'o' || s[i] == 'u' || s[i] == 'A'
|| s[i] == 'E' || s[i] == 'I' || s[i] == 'O'
|| s[i] == 'U')
vowels++;
else
consonants++;
}
i++;
stringcount(s);
}
}

// Driver code
int main()
{
char s[1000] = "C Programming";
printf("String: %s", s);

stringcount(s);
return 0;
}

OUTPUT
Strings: C Programing
Vowels:5
Consonants:8

20.Write a C program to find the addition and subtraction of two


matrices using function.

#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]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


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
Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3

Sum of two matrices:


-2 8 7

10 8 6

You might also like