1.Write a C program to Swap the value of two Variables.
(Using a 3rd Variable & without Using a 3rd Variable).
Sample Solution C Code:
a) Using a 3rd Variable :
#include < stdio.h >
int main()
{
int var1, var2, temp;
printf("Enter two integersn");
scanf("%d%d", &var1, &var2);
printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
return 0;
}
b)without Using a 3rd Variable:
#include< stdio.h >
int main()
{
int x, y;
printf("Input value for x & y: \n");
scanf("%d%d",&x,&y);
printf("Before swapping the value of x & y: %d %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\nAfter swapping the value of x & y: %d %d",x,y);
return 0;
}
Sample Output:
Input value for x & y:
Before swapping the value of x & y: 5 7
After swapping the value of x & y: 7 5
2.Write a C program to find out the Area of a Circle .
Sample Solution C Code:
#include
int main(void) {
float pie = 3.14;
int radius = 6;
printf("The radius of the circle is %d \n" , radius);
float area = (float)(pie* radius * radius);
printf("The area of the given circle is %f", area);
return 0;
}
Output
The radius of the circle is 6
The area of the given circle is 113.040001 More >
3.Write a C program to convert temperature from Fahrenheit to Celsius.
Sample Solution C Code:
#include
int main()
{
float celsius, fahrenheit;
/* Input temperature in fahrenheit */
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
/* Fahrenheit to celsius conversion formula */
celsius = (fahrenheit - 32) * 5 / 9;
/* Print the value of celsius */
printf("%.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);
return 0;
} Enter temperature in Fahrenheit: 205
205.00 Fahrenheit = 96.11 Celsius
More >
If else :
4. Write a Program in C to find whether a number is EVEN or ODD.
Sample Solution C Code:
#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: 7
7 is odd.
5.Write a Program in C to find the Largest of Three Numbers.
Sample Solution C Code:
#include
int main()
{
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2) {
if (n2 >= n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
else {
if (n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
}
return 0;
}
Output:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
6.Write a Program in C to enter marks of five subjects. Find the Sum and the Average of them and then assign grade to the students.
Sample Solution C Code:
// C Program to Find Grade of a Student Using If else Ladder
#include < stdio.h >
int main() {
float subject_1, subject_2, subject_3, subject_4, subject_5;
float total, average, percentage;
char grade;
printf("Enter the marks of five subjects::\n");
scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3, &subject_4, &subject_5);
// It will calculate the Total, Average and Percentage
total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// It will calculate the Grade
if (average >= 90)
grade = 'O';
else if (average >= 80 && average < 90)
grade = 'E';
else if (average >= 70 && average < 80)
grade = 'A';
else if (average >= 60 && average < 70)
grade = 'B';
else if (average >= 50 && average < 60)
grade = 'c';
else if (average >= 400 && average < 50)
grade = 'd';
else
grade = 'F';
// It will produce the final output
printf("\nThe Total marks is: \t%.2f / 500.00\n", total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Grade is: \t'%c'\n", grade);
return 0;
}
Output
Enter the marks of five subjects::
95
85
74
64
53
The Total marks is: 371.00 / 500.00
The Average marks is: 74.20
The Percentage is: 74.20%
The Grade is: 'A' More >
7.Write a Program in C to check whether a Year is Leap year or not.
Sample Solution C Code:
#include < stdio.h >
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output 1
Enter a year: 1900
1900 is not a leap year.
Output 2
Enter a year: 2012
2012 is a leap year. More >
8.Write a Program in C to compute the Factorial of a Number.
Sample Solution C Code:
#include
int main()
{
int n,i,f;
f=i=1;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i++;
}
printf("The Factorial of %d is : %d",n,f);
return 0;
}
OUTPUT:
Enter a Number to Find Factorial: 5
The Factorial of 5 is : 120 More >
9.Write a c program to find out the sum of series 1 + 2 + …. + n .
Sample Solution C Code:
#include< stdio.h >
int main()
{
int i,sum=0,num;//variables
printf("Enter the last element of the series:\n");
scanf("%d",&num);//input::Last element of the series
for(i=1;i<=num;i++)
{
if(i!=num)
printf("%d + ",i);
else
printf("%d ",i);
sum=sum+i;
}
printf("= %d",sum);
return 0;
}
OUTPUT:
Enter the last element of the series:
15
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 = 120 More >
10.Write a C Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
.
Sample Solution C Code:
#include
void main()
{
double number, sum = 0, i;
printf("\n enter the number ");
scanf("%lf", &number);
for (i = 1; i <= number; i++)
{
sum = sum + (1 / i);
if (i == 1)
printf("\n 1 +");
else if (i == number)
printf(" (1 / %lf)", i);
else
printf(" (1 / %lf) + ", i);
}
printf("\n The sum of the given series is %.2lf", sum);
}
Output:
enter the number 4
1 + (1/2.000000) + (1/3.000000) + (1/4.000000)
The sum of the given series is 2.08
More >
11.Write a Program in C to find the sum of the series
S= 1! + 2! + 3! + 4! + . . . . . + N! (Input N).
.
Sample Solution C Code:
#include< stdio.h >
int main()
{
int num,i,j,fact,sum=0;//variables
printf("Enter the last number of series:\n");
scanf("%d",&num);//last number of series
for(i=1;i<=num;i++)//loop for finding factorial and sum
{
fact=1;
if(i!=num)
printf("%d!+ ",i);
else
printf("%d!= ",i);
for(j=1;j<=i;j++)
fact=fact*j;
sum=sum+fact;
}
printf("%d",sum);
return 0;
}
OUTPUT:
Enter the last number of series:
10
1!+ 2!+ 3!+ 4!+ 5!+ 6!+ 7!+ 8!+ 9!+ 10!= 4037913 More >
12.Write a Program in C to Generate the Fibonacci Series up to Nth Term.
1 + 1 + 2 + 3 + 5 + 8 + 13 + . . . . + Nth Term
.
Sample Solution C Code:
#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, More >
13.Write a Program in C to generate the PASCAL’s triangle up to specified number of rows.
Sample Solution C Code:
#include < stdio.h >
int main()
{
int rows, coef = 1, space, i, j;
printf(“\nEnter the number of rows : “);
scanf(“%d”,&rows);
printf(“\n”);
for(i=0; i
{
for(space=1; space <= rows-i; space++)
printf(” “);
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf(“%4d”, coef);
}
printf(“\n\n”);
}
return 0;
}
OUTPUT
Enter the number of rows : 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 More >
14.
Write a Program in C to create a menu driven calculator using switch case
Sample Solution C Code:
#include < stdio.h >
int main()
{
char ch;
int a, b, result;
// Asking for Input
printf("Enter an Operator (+, *, *, /): ");
scanf("%c", &ch);
printf("Enter two operands: \n");
scanf("%d %d", &a, &b);
switch(ch){
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
printf("Result = %d", result);
return 0;
}
Output
Enter an Operator (+, *, *, /): *
Enter two operands:
5
7
Result = 35 More >
15.
Write a Program in C to calculate the area of a Triangle, Rectangle or a Circle using
a menu driven programming
Sample Solution C Code:
#include < stdio.h >
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
}
printf("The area is : %f\n",area);
}
Sample Output:
Input 1 for area of circle
Input 2 for area of rectangle
Input 3 for area of triangle
Input your choice : 1
Input radious of the circle : 5
The area is : 78.500000 More >
16.
Write a Program in C to swap 2 numbers using pointers .
Sample Solution C Code:
#include < stdio.h >
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Sample Output:
Enter the value of x and y
10 20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10 More >
17.
Write a simple Program in C pointers .
Sample Solution C Code:
#include < stdio.h >
int main () {
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Sample Output:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
More >
18.
Write a Program in C addition of two numbers using pointers. .
Sample Solution C Code:
#include < stdio.h >
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
} More >
19.
C Program to calculate multiplication of two numbers using pointers
.
Sample Solution C Code:
#include< stdio.h >
void main()
{
int a, b, *p, *q, mul;
// Reads two user inputs integer values for variable a and b.
printf("\nEnter integer a:");
scanf("%d", &a);
printf("\nEnter integer b:");
scanf("%d", &b);
// assign address of variable a and b to integer pointers p and q.
p = &a;
q = &b;
// performs multiplication using pointers p and q referring the address of a and b values.
mul = *p * *q;
printf("\nMultiplication of the numbers: %d", mul);
}
Output:
Enter integer a:2
Enter integer b:4
Multiplication of the numbers: 8 More >
20.
C program to print the elements of an array in reverse order
.
Sample Solution C Code:
#include < stdio.h >
int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
//Loop through the array in reverse order
for (int i = length-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Original array:
1 2 3 4 5
Array in reverse order:
5 4 3 2 1 More >
21.
C Program to Print Even and Odd Numbers in an Array
.
Sample Solution C Code:
#include< stdio.h >
int main()
{
int n, a[20];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter array elements: \n");
for(int i=0; i
{
scanf("%d",&a[i]);
}
printf("Even numbers in the array are: \n");
for(int i=0; i
{
if(a[i]%2==0)
printf("%d ", a[i]);
}
printf("\nOdd numbers in the array are: \n");
for(int i=0; i
{
if(a[i]%2!=0)
printf("%d ", a[i]);
}
return 0;
}
Enter the size of the array: 5
Enter array elements:
10
15
20
54
26
Even numbers in the array are:
10 20 54 26
Odd numbers in the array are:
15 More >
22.
C program to find maximum and minimum element in array
.
Sample Solution C Code:
#include
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int i, max, min, size;
/* Input size of the array */
printf("Enter size of the array: ");
scanf("%d", &size);
/* Input array elements */
printf("Enter elements in the array: ");
for(i=0; i
{
scanf("%d", &arr[i]);
}
/* Assume first element as maximum and minimum */
max = arr[0];
min = arr[0];
/*
* Find maximum and minimum in all array elements.
*/
for(i=1; i
{
/* If current element is greater than max */
if(arr[i] > max)
{
max = arr[i];
}
/* If current element is smaller than min */
if(arr[i] < min)
{
min = arr[i];
}
}
/* Print maximum and minimum element */
printf("Maximum element = %d\n", max);
printf("Minimum element = %d", min);
return 0;
}
Enter size of the array: 10
Enter elements in the array: -10 10 0 20 -2 50 100 20 -1 10
Maximum element = 100
Minimum element = -10 More >
23.
C program to print 2D array element
.
Sample Solution C Code:
#include < stdio.h >
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8 More >
24.
Write a Program in C to show the elements of a Matrix.
Sample Solution C Code:
#include
int main()
{
// variables
int row;
int column;
// take row and column size
printf("Ener row size: ");
scanf("%d", &row);
printf("Ener column size: ");
scanf("%d", &column);
// declare array
int arr[row][column];
// take matrix elements as input
printf("Enter elements for %dx%d matrix:\n",
row, column);
for(int i=0;i
{
for(int j=0;j
{
printf("arr[%d][%d]: ",i,j);
scanf("%d", &arr[i][j]);
}
printf("\n");
}
// display matrix using for loop
printf("The %dx%d matrix elements are:\n",
row, column);
for(int i=0; i
for(int j=0; j
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Ener row size: 3
Ener column size: 3
Enter elements for 3×3 matrix:
arr[0][0]: 1
arr[0][1]: 2
arr[0][2]: 3
arr[1][0]: 4
arr[1][1]: 5
arr[1][2]: 6
arr[2][0]: 7
arr[2][1]: 8
arr[2][2]: 9
The 3×3 matrix elements are:
1 2 3
4 5 6
7 8 9 More >
25.
Write a Program in C to find the Sum of two Matrixes..
Sample Solution C Code:
#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 rows (between 1 and 100): 2
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 More > More >
26.
Write a Program in C to find the Difference of two Matrixes.
Sample Solution C Code:
#include < stdio.h >
int main()
{
int i, j, rows, columns, a[10][10], b[10][10];
int Subtraction[10][10];
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the First Matrix Elements\n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
printf("\n Please Enter the Second Matrix Elements\n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &b[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
Subtraction[rows][columns] = a[rows][columns] - b[rows][columns];
}
}
printf("\n After Subtracting Matrix a from Matrix b = a - b \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
printf("%d \t ", Subtraction[rows][columns]);
}
printf("\n");
}
return 0;
} More >
27.
Write a Program in C to find the Transpose of a Matrix.
Sample Solution C Code:
#include < stdio.h >
int main(){
int m, n, i, j, matrix[10][10], transpose[10][10];
printf("Enter rows and columns :\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (i= 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0;i < m;i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i< n; i++) {
for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
return 0;
}
Output
Enter rows and columns :
2 3
Enter elements of the matrix
1 2 3
2 4 5
Transpose of the matrix:
1 2
2 4
3 5 More >
28.
Write a Program in C to find the Multiplication of two Matrixes.
Sample Solution C Code:
#include < stdio.h >
int main(){
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows and columns of first matrix 3 3
Enter the elements of first matrix
1 2 0
0 1 1
2 0 1
Enter the number of rows and columns of second matrix 3 3
Enter the elements of second matrix
1 1 2
2 1 1
1 2 1
Product of entered matrices:-
5 3 4
3 3 2
3 4 5 More >
29.
C Program to Find Factorial of a Number using Functions
Sample Solution C Code:
#include
#include
int main()
{
printf("Enter a Number to Find Factorial: ");
printf("\nFactorial of a Given Number is: %d ",fact());
return 0;
}
int fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
return fact;
}
OUTPUT:
Enter a Number to Find Factorial: 5
Factorial of a Given Number is: 120
More >
30.
Write a Program in C to calculate the area of a triangle,
rectangle or a circle using function depending on the choice entered by the user.
Triangle
Rectangle
Circle
Exit
Sample Solution C Code:
#include < stdio.h >
void triangleArea(){
float a,b,c;
printf("Enter the first side of the triangle: ");
scanf("%f",&a);
printf("Enter the second side of the triangle: ");
scanf("%f",&b);
printf("Enter the third side of the triangle: ");
scanf("%f",&c);
float s,area;
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the triangle is %.2f square units.",area);
}
void rectangleArea(){
float a,b;
printf("Enter the length of the rectangle: ");
scanf("%f",&a);
printf("Enter the breadth of the rectangle: ");
scanf("%f",&b);
printf("The area of the rectangle is: %.2f",a*b);
}
void circleArea(){
float r,a;
printf("Enter the radius of the circle: ");
scanf("%f",&r);
a=3.14159*r*r;
printf("The area of the circle is: %.2f",a);
}
int main() {
int c;
printf("1. Area of a triangle.\n");
printf("2. Area of a rectangle.\n");
printf("3. Area of a circle.\n");
printf("4. Exit.\n\n");
printf("Enter your choice: ");
scanf("%d",&c);
switch(c){
case 1:
triangleArea();
break;
case 2:
rectangleArea();
break;
case 3:
circleArea();
break;
case 4:
printf("Program Terminated.");
break;
default: printf("Invalid choice.");
}
return 0;
}
Output:
1. Area of a triangle.
2. Area of a rectangle.
3. Area of a circle.
4. Exit.
Enter your choice: 1
Enter the first side of the triangle: 5
Enter the second side of the triangle: 4
Enter the third side of the triangle: 3
Area of the triangle is 6.00 square units. More >
31.
example of the structure in C language to store many employees information.
Sample Solution C Code:
#include < stdio.h >
void#include< stdio.h >
#include < string.h >
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.salary=56000;
//store second employee information
e2.id=102;
strcpy(e2.name, "James Bond");
e2.salary=126000;
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
printf( "employee 1 salary : %f\n", e1.salary);
//printing second employee information
printf( "employee 2 id : %d\n", e2.id);
printf( "employee 2 name : %s\n", e2.name);
printf( "employee 2 salary : %f\n", e2.salary);
return 0;
}
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000 More >
32.
example of the structure in C language to store many Books information.
Sample Solution C Code:
#include
#include
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
When the above code is compiled and executed, it produces the following result −
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700 More >
33.
example of the Union in C language to store records.
Sample Solution C Code:
#include < stdio.h >
#include < string.h >
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
// assigning values to record1 union variable
strcpy(record1.name, "Raju");
strcpy(record1.subject, "Maths");
record1.percentage = 86.50;
printf("Union record1 values example\n");
printf(" Name : %s \n", record1.name);
printf(" Subject : %s \n", record1.subject);
printf(" Percentage : %f \n\n", record1.percentage);
// assigning values to record2 union variable
printf("Union record2 values example\n");
strcpy(record2.name, "Mani");
printf(" Name : %s \n", record2.name);
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
return 0;
}
OUTPUT:
Union record1 values example
Name :
Subject :
Percentage : 86.500000;
Union record2 values example
Name : Mani
Subject : Physics
Percentage : 99.500000 More > ;
34.
Half Pyramid of *
Sample Solution C Code:
#include < stdio. h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ")
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
OUTPUT:
*
* *
* * *
* * * *
* * * * *