0% found this document useful (0 votes)
5 views13 pages

C Programming Lab Questions Solutions

The document is a lab tutorial for C programming that includes 25 different programming exercises. Each exercise demonstrates various concepts such as displaying messages, performing arithmetic operations, using conditional statements, working with loops, and implementing functions, structures, and unions. The tutorial serves as a practical guide for learning C programming through hands-on coding examples.

Uploaded by

navhne
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)
5 views13 pages

C Programming Lab Questions Solutions

The document is a lab tutorial for C programming that includes 25 different programming exercises. Each exercise demonstrates various concepts such as displaying messages, performing arithmetic operations, using conditional statements, working with loops, and implementing functions, structures, and unions. The tutorial serves as a practical guide for learning C programming through hands-on coding examples.

Uploaded by

navhne
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

Lab tutorial for C Programming.

1. WAP to display “Welcome to C programming class”.


#include <stdio.h>
int main() {
printf("Welcome to C programming class");
return 0;
}
2. WAP to add two numbers.
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d", sum);
return 0;
}
3. WAP to find simple Interest.
#include <stdio.h>
int main() {
float p, t, r, si;
printf("Enter Principal, Time, and Rate: ");
scanf("%f %f %f", &p, &t, &r);
si = (p * t * r) / 100;
printf("Simple Interest: %.2f", si);
return 0;
}
4. WAP to calculate the area of triangle whose sides area taken from the user.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter three sides of the triangle: ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area: %.2f", area);
return 0;
}
5. WAP to check whether the number entered by user is exactly divisible by 5 but not by 11.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 5 == 0 && num % 11 != 0)
printf("%d is divisible by 5 but not 11.", num);
else
printf("Condition not met.");
return 0;
}
6. WAP to find the greatest among three number given by the user.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
{
printf("%d is greatest", a);
}
else if (b >= a && b >= c)
{
printf("%d is greatest", b);
}
else
{
printf("%d is greatest", b);
}
return 0;
}
7. WAP to find factorial of any number given by the user.
#include <stdio.h>
int main() {
int n, i, fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
fact *= i;
}
printf("Factorial: %d ", fact);
return 0;
}
8. WAP to read 3 digits number and test whether it is Armstrong or not.
#include <stdio.h>
int main() {
int num, original, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
original = num;
while (original != 0) {
remainder = original % 10;
result += remainder * remainder * remainder;
original /= 10;
}
if (result == num) {
printf("%d is an Armstrong number.", num);
}
else {
printf("%d is not.", num);
}
return 0;
}
9. WAP to read a number from the user and check whether it is a palindrome or not.
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed){
printf("Palindrome.");
}
else{
printf("Not a palindrome.");
}
return 0;
}
10. WAP to generate Fibonacci series up to nth term. (n is given by user)
#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
11. WAP to generate Floyd’s triangle.
1
2 3
4 5 6
7 8 9 10
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}
12. WAP to make a menu for addition, subtraction, and multiplication of two numbers.
#include <stdio.h>
int main() {
int choice;
float a, b;
printf("1. Add\n2. Subtract\n3. Multiply\nEnter choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
switch(choice) {
case 1:
printf("Result: %.2f", a + b);
break;
case 2:
printf("Result: %.2f", a - b);
break;
case 3:
printf("Result: %.2f", a * b);
break;
default:
printf("Invalid choice");
}
return 0;
}
13. WAP to display the addition of two matrices.
#include <stdio.h>
int main() {
int a[2][2], b[2][2], sum[2][2];
printf("Enter elements of 1st matrix:\n");
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
scanf("%d", &b[i][j]);
}
}

printf("The sum of two matrix is:\n");


for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
14. WAP to add the diagonal elements of the matrix supplied by the user.
#include <stdio.h>
int main() {
int a[3][3], i, j, sum1 = 0, sum2 = 0;
printf("Enter 3x3 matrix elements:\n");
//To put matrix elements
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &a[i][j]);
}
}

//To print matrix elements


printf("The matrix is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{

printf("%d\t",a[i][j]);
}
printf("\n");
}

//To add diagonal elements from left to right


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(i == j)
{
sum1 += a[i][j];
}
}
}

//To add diagonal elements from left to right


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if (i + j == 2 )
{
sum2 += a[i][j];
}
}
}
printf("Sum of left to rigt diagonal: %d\n", sum1);
printf("Sum of right to left diagonal: %d", sum2);
return 0;
}
15. WAP to show the concept of pointer expressions.

16. WAP to read a string from the user and check whether it is palindromic or not.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
printf("Enter the string\n");
gets(str1);
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0)
{
printf("String is palindrome");
}
else
{
printf("String is not palindrome");
}
return 0;
}
17. WAP to illustrate the function with arguments and no return value.
#include<stdio.h>
void sum(int,int);
int main()
{
int a,b;
printf("Enter the two numbers\n");
scanf("%d%d",&a,&b);
sum(a,b);
return 0;
}
void sum(int x,int y)
{
int r;
r=x+y;
printf("The sum of numbers=%d",r);
}
18. WAP to illustrate the function with no arguments and no return value.
#include<stdio.h>
void sum();
int main()
{
sum();
return 0;
}
void sum()
{
int x,y,r;
printf("Enter the two numbers\n");
scanf("%d%d",&x,&y);
r=x+y;
printf("The sum of numbers=%d",r);
}
19. WAP to illustrate the function with arguments and return value.
#include<stdio.h>
int sum(int,int);
int main()
{
int a,b,result;
printf("Enter the two numbers\n");
scanf("%d%d",&a,&b);
result=sum(a,b);
printf("The sum of numbers=%d",result);
return 0;
}
int sum(int x,int y)
{
int r;
r=x+y;
return r;
}
20. WAP to illustrate the function with no arguments and return value.
#include<stdio.h>
int sum();
int main()
{
int result;
result=sum();
printf("The sum of numbers=%d",result);
return 0;
}
int sum()
{
int x,y,r;
printf("Enter the two numbers\n");
scanf("%d%d",&x,&y);
r=x+y;
return r;
}
21. WAP to swap the values of two variables using call by value.
#include<stdio.h>
void swap(int x,int y);
int main()
{
int a,b;
a=10;
b=20;
printf("value before swapping a=%d and b=%d",a,b);
swap(a,b);
printf("\nvalue after swapping a=%d and b=%d",a,b);
return 0;
}

void swap(int x,int y)


{
int temp;
temp=x;
x=y;
y=temp;
}
22. WAP to swap the values of two variables using call by reference.
#include<stdio.h>
void swap(int *x,int *y);
int main()
{
int a,b;
a=10;
b=20;
printf("The value before swapping are a=%d and b=%d",a,b);
swap(&a,&b);
printf("\nThe values after swapping are a=%d and b=%d",a,b);
return 0;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
23. WAP to calculate the factorial of a supplied number using a recursive function.
#include<stdio.h>
long int fact(int n);
int main()
{
int num;
long int f;
printf("Enter the number\n");
scanf("%d",&num);
f=fact(num);
printf("The factorial of a given number is %ld",f);
return 0;
}
long int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
24. WAP to create a STRUCTURE student containing name, [Link], marks as members and
that STRUCTURE to read and display the data entered by the user.
#include <stdio.h>

// Defining the structure


struct student {
char name[50];
int roll_no;
float marks;
};

int main() {
struct student s;

// Reading data from the user


printf("--- Enter Student Details ---\n");
printf("Enter Name: ");
gets([Link]);
printf("Enter Roll Number: ");
scanf("%d", &s.roll_no);
printf("Enter Marks: ");
scanf("%f", &[Link]);

// Displaying data
printf("\n--- Student Information ---\n");
printf("Name: %s\n", [Link]);
printf("Roll No: %d\n", s.roll_no);
printf("Marks: %.2f\n", [Link]);

return 0;
}
25. WAP to create a UNION student containing name, [Link], marks as members and that
UNION to read and display the data entered by the user.
#include <stdio.h>
#include <string.h>

// Defining the union


union student {
char name[50];
int roll_no;
float marks;
};

int main() {
union student u;

printf("--- Union Data Handling ---\n");

// Handling Name
printf("Enter Name: ");
gets([Link]);
printf("Displaying Name: %s\n\n", [Link]);

// Handling Roll Number (This overwrites the Name memory)


printf("Enter Roll Number: ");
scanf("%d", &u.roll_no);
printf("Displaying Roll No: %d\n\n", u.roll_no);
// Handling Marks (This overwrites the Roll No memory)
printf("Enter Marks: ");
scanf("%f", &[Link]);
printf("Displaying Marks: %.2f\n", [Link]);

printf("\nNote: In a Union, only the last member entered is stored correctly.");

return 0;
}

You might also like