0% found this document useful (0 votes)
116 views19 pages

Clab Internal1 PDF

The document provides source code for several C programming problems: 1) A program to find the maximum and minimum of three numbers by comparing them. 2) Programs to calculate simple and compound interest given principal, rate, and time. 3) A program that determines class (e.g. first class) awarded based on a percentage grade range. 4) A multiplication table program that prints the multiplication of a given number up to a specified range. 5) A program to convert a decimal number between 0-255 to its binary equivalent.

Uploaded by

aaaaaaaasd
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)
116 views19 pages

Clab Internal1 PDF

The document provides source code for several C programming problems: 1) A program to find the maximum and minimum of three numbers by comparing them. 2) Programs to calculate simple and compound interest given principal, rate, and time. 3) A program that determines class (e.g. first class) awarded based on a percentage grade range. 4) A multiplication table program that prints the multiplication of a given number up to a specified range. 5) A program to convert a decimal number between 0-255 to its binary equivalent.

Uploaded by

aaaaaaaasd
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
You are on page 1/ 19

Simple numeric problems:

a. Write a program for fiend the max and min from the three numbers.

Algorithm:

Step 1: Start

Step 2: Read the three numbers to be compared, as A, B and C.

Step 3: Check if A is greater than B.

3.1 If true, then check if A is greater than C.

3.1.1 If true, print 'A' as the greatest number.

3.1.2 If false, print 'C' as the greatest number.

3.2 If false, then check if B is greater than C.

3.1.1 If true, print 'B' as the greatest number.

3.1.2 If false, print 'C' as the greatest number.

Step 4: End

Flowchart:

Source Code:

#include<stdio.h>
void main(){
int a,b,c;
printf("Enter 3 numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Mximum number is a = %d\n",a);
else if(b>a && b>c)
printf("Mximum number is b = %d\n",b);
else
printf("Mximum number is c = %d\n",c);
if(a<b && a<c)
printf("Minimum number is a = %d\n",a);
else if(b<a && b<c)
printf("Minimum number is b = %d\n",b);
else
printf("Minimum number is c = %d\n",c);
}
Output:
Enter 3 numbers
45
98
-12
Mximum number is b = 98
Minimum number is c = -12
b. Write the program for the simple, compound interest.

Source Code:
#include<stdio.h>
#include<math.h>
int main(){
int p,t;
float r,si,amount,ci;
printf("Please enter principal,time and rate of interest\n");
scanf("%d%d%f",&p,&t,&r);
si=p*t*r/100;
//Simple Interest formula is p*t*r
printf("Simple interest = %.3f\n",si);
//Compound Interest formula is below
amount=p*pow((1 +r/100),t);
ci=amount-p;
printf("Compound interest = %.3f",ci);
}
Output:
Please enter principal,time and rate of interest
10000
12
5
Simple interest = 6000.000
Compound interest = 7958.553
c. Write program that declares Class awarded for a given percentage of marks, where mark <40%=
Failed, 40% to <60% = Second class, 60% to <70%=First class, >= 70% = Distinction. Read percentage
from standard input.
Algorithm:
Step 1: start
Step 2: read marks
Step 3: if marks >= 70% then display Distinction
Step 4: if marks >= 60% and marks <70% then display First Class
Step 5: if marks >=40% and marks <60% then display Second Class
Step 6: if marks <40% go to step 7
Step 7: display failed
Step 8 : stop.
Flowchart:
Source Code:
#include <stdio.h>
int main(){
int M1, M2, M3;
float per;
printf("Enter three subjects marks\n ");
scanf("%d%d%d", &M1, &M2, &M3);
per = (M1 + M2 + M3) / 3.0;
printf("Percentage = %.2f\n", per);
if(per >= 70){
printf("Distinction");
}
else if((per>=60) && (per<= 70)){
printf("First class");
}
else if((per>=40) && (per<= 60)){
printf("Second class");
}
else if (per <= 40){
printf("Failed");
}
return 0;
}

Output 1:
Enter three subjects marks
89
57
78
Percentage = 74.67
Distinction
Output 2:
Enter three subjects marks
65
60
62
Percentage = 62.33
First class
Output 3:
Enter three subjects marks
45
51
60
Percentage = 52.00
Output 4:
Second class
Enter three subjects marks
41
24
39
Percentage = 34.67
Failed
d. Write a program that prints a multiplication table for a given number and the number of rows in
the table. For example, for a number 5 and rows = 3, the output should be:
5x1=5
5 x 2 = 10
5 x 3 = 15
Algorithm:
Flowchart:

Source Code:
#include <stdio.h>
int main(){
int num,range,i;
printf("Enter a number\n");
scanf("%d",&num);
printf("Enter the range\n");
scanf("%d",&range);
i=1;
while(i<=range){
printf("%d * %d = %d \n", num, i, num*i);
++i;
}
return 0;
}
Output:
Enter a number
7
Enter the range
3
7*1=7
7 * 2 = 14
7 * 3 = 21
e. Write a program that shows the binary equivalent of a given positive number between 0 to 255.
Algorithm:
Step 1: Start
Step 2: Read an integer value
Step 3: Divide the input decimal number by 2 and store the remainder.
Step 4: Store the quotient back to the input number variable.
Step 5: Repeat this process till quotient becomes zero.
Step 6: Equivalent binary number will be the remainders in Step 5 in reverse order.
Step 7: Print the equivalent binary digit
Step 8: Stop
Flowchart:

Source Code:
#include <stdio.h>
int main() {
long int n;
int remainder;
long binary = 0, i = 1;
printf("Enter a decimal number\n");
scanf("%ld", &n);
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
printf("Binary number of %ld is %ld:", n, binary);
return 0;
}
Output:
Enter a decimal number
125
Binary number of 0 is: 111110

Expression Evaluation:
a. A building has 10 floors with a floor height of 3 meters each. A ball is dropped from the top of
the building. Find the time taken by the ball to reach each floor. (Use the formula s =
ut+(1/2)at^2 where u and a are the initial velocity in m/sec (= 0) and acceleration in m/sec^2 (=
9.8 m/s^2)).
Source Code:
#include<stdio.h>
#include<math.h>
int main(){
float s=30,u=0,a=9.8,t,temp; // s = 10*3 = 30
/* s = ut+(1/2)at^2
s = 0*t + (0.5)*a*t^2 */
temp=(0.5)*a;
/* s = 0 *t+ temp*t ^2
s/temp = t ^2
sqrt(s/temp) = t */
t = sqrt(s/temp);
printf("Time taken by the ball to reach each floor is =%f ",t);
return 0;
}
Output:
Time taken by the ball to reach each floor is = 2.474358
b. Write a C program, which takes two integer operands and one operator from the user, performs
the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch
statement)
Source Code:
#include <stdio.h>
int main(){
int a, b, c;
char ch;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch){
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
return 0;
}
Output 1:
Enter your operator(+, -, /, *, )
+
Enter the values of a and b
6
2
addition of two numbers is 8
Output 2:
Enter your operator(+, -, /, *, )
-
Enter the values of a and b
15
9
substraction of two numbers is 6
Output 3:
Enter your operator(+, -, /, *, )
*
Enter the values of a and b
5
6
multiplication of two numbers is 30
Output 4:
Enter your operator(+, -, /, *, )
/
Enter the values of a and b
8
4
remainder of two numbers is 2
c.Write a program that finds if a given number is a prime number
Source Code:
#include <stdio.h>
int main(){
int n, i, c = 0;
printf("Enter any number\n");
scanf("%d", &n);
for (i = 1; i <= n; i++){
if (n % i == 0){
c++;
}
}
if (c == 2){
printf("The given number is a Prime number");
}
else{
printf("The given number is not a Prime number");
}
return 0;
}
Output 1:
Enter any number
1
The given number is not a Prime number
Output 2:
Enter any number
11
The given number is a Prime number
d.Write a C program to find the sum of individual digits of a positive integer and test given
number is palindrome.
Source Code:
#include<stdio.h>
void main(){
int n,x,r,sum=0;
printf("Enter The Number\n");
scanf("%d",&n);
x=n;
while(n>0){
r=n%10;
sum=sum*10+r;
n=n/10;
}
if(x==sum){
printf("%d is a Palindrome Number\n",x);
}
else{
printf("%d is not a Palindrome Number\n",x);
}
return 0;
}
Output 1:
Enter The Number
121
121 is a Palindrome Number
Output 2:
Enter The Number
521
521 is not a Palindrome Number
e. A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0
and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.
Source Code:
#include <stdio.h>
int main(){
int i,n,a=0,b=1,c;
printf("Enter n value\n");
scanf("%d",&n);
printf("\n%d\t%d\t",a,b);
for(i=3;i<=n;i++){
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
return 0;
}
Output:
Enter n value
5

0 1 1 2 3
f. Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
Source Code:
#include<stdio.h>
void main(){
int n, i, j, count;
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i <= n; i++){
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0){
count++;
}
if(count == 2){
printf("%d\t", i);
}
}
}
Output:
Enter any number
10
The prime numbers between 1 to 10
2 3 5 7
g. Write a C program to find the roots of a Quadratic equation.
Source Code:
#include<stdio.h>
#include<math.h>
int main(){
int a,b,c,d;
float r1,r2;
printf("enter values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0){
printf("roots are equal\n");
r1=-b/(2*a);
r2=-b/(2*a);
printf("the value of root1 is =%f\n",r1);
printf("the value of root2 is =%f\n",r2);
}
else if(d>0){
printf("roots are distinct\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("the value of root1 is =%f\n",r1);
printf("the value of root2 is =%f\n",r2);
}
else if(d<0){
printf("roots are imaginary\n");
}
return 0;
}
/*
Output 1:
enter values of a,b,c
2
1
2
roots are imaginary
Output 2:
enter values of a,b,c
1
6
9
roots are equal
the value of root1 is =-3.000000
the value of root2 is =-3.000000
Output 3:
enter values of a,b,c
0
2
3
roots are distinct
the value of root1 is =-1.#IND00
the value of root2 is =-1.#INF00
h. Write a C program to calculate the following, where x is a fractional value.
1-x/2 +x^2/4-x^3/6
Source Code:
#include<stdio.h>
#include<math.h>
int main(){
float sum=1;
int x,k=1,i;
printf("Enter x value:");
scanf("%d",&x);
for(i=1;i<=3;i++){
k=-k;
sum=sum+(k*pow(x,i))/(i*2);
}
printf("sum of the series=%f",sum);

return 0;
}
Output:
Enter x value:4
sum of the series=-7.666667
j.Write a C program to read in two numbers, x and n, and then compute the sum of this geometric
progression: 1+x+x^2+x^3+………….+x^n. For example: if n is 3 and x is 5, then the program
computes 1+5+25+125.
Source Code:
#include <stdio.h>
#include <math.h>
void main(){
int n, x, i, sum = 0;
printf("Enter the limit\n");
scanf("%d", &n);
printf("Enter the value of x\n");
scanf("%d", &x);
if(x < 0 || n < 0){
printf("illegal value");
}
else{
for(i = 0; i <= n; i++)
sum=sum + pow(x, i);
}
printf("sum=%d", sum);
}
Output:
Enter the limit
5
Enter the value of x
2
sum=63

Arrays
a.Write a C program to find the minimum, maximum and average in an array of integers.
Source Code:
#include <stdio.h>
int main(){
int a[50];
int i, max, min, n;
printf("Enter size of the array\n");
scanf("%d", &n);
printf("Enter elements in the array\n");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
max = a[0];
min = a[0];
for(i=1; i<n; i++){
if(a[i] > max){
max = a[i];
}
if(a[i] < min){
min = a[i];
}
}
printf("Maximum element = %d\n", max);
printf("Minimum element = %d\n", min);

return 0;
}
Output:
Enter size of the array
3
Enter elements in the array
12
23
32
Maximum element = 32
Minimum element = 12

b. Write a functions to compute mean, variance, Standard Deviation, sorting of n elements in


single dimension array.
Source Code:
#include<stdio.h>
#include<math.h>
void main(){
float x[10], mean, variance,dev;
float sum1=0, sum2=0;
int n,i;
printf("Enter the number of integers\n");
scanf("%d", &n);
printf("Enter the integers\n");
for(i=0;i<=n-1;i++){
scanf("%f", &x[i]);
}

//to find mean


for(i=0;i<=n-1;i++){
sum1 = sum1 + x[i];
}
mean = sum1 /n;
printf("mean = %f\n", mean);

//to find variance


for(i=0;i<=n-1;i++){
sum2 = sum2 + (x[i] - mean) * (x[i] - mean);
}
variance = sum2/n;
printf("variance = %f\n", variance);

//to find deviation


dev = sqrt(variance);
printf("deviation = %f\n", dev);
}
Output:
Enter the number of integers
2
Enter the integers
2
3
mean = 2.500000
variance = 0.250000
deviation = 0.500000

c. Write a C program that uses functions to perform the following:


i. Addition of Two Matrices
Source Code:
#include <stdio.h>
void array_addition(int arr1[][30],int arr2[][30],int r,int c){
int i,j;
int sum[30][30];
for (i = 0; i < r; i++) {
for (j = 0 ; j < c; j++) {
sum[i][j] = arr1[i][j] + arr2[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}

}
int main(){
int r,c,i,j,first[30][30], second[30][30];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &r, &c);
printf("Enter the elements of first matrix\n");

for (i = 0; i < r; i++){


for (j = 0; j < c; j++){
scanf("%d", &first[i][j]);
}
}

printf("Enter the elements of second matrix\n");

for (i = 0; i < r; i++){


for (j = 0; j < c; j++){
scanf("%d", &second[i][j]);
}
}
printf("Sum of entered matrices:-\n");
array_addition(first,second,r,c);
}
Output:
Enter the number of rows and columns of matrix
23
Enter the elements of first matrix
563
212
Enter the elements of second matrix
589
641
Sum of entered matrices:-
10 14 12
8 5 3
ii. Multiplication of Two Matrices
Source Code:
#include<stdio.h>
int read(int x[10][10],int m,int n);
int mul(int a[10][10],int b[10][10], int c[10][10],int r1,int c2,int r2);
int write(int x[10][10],int m,int n);

int main()
{
int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2;
printf("Enter rows and columns of matrix1\n");
scanf("%d%d",&r1,&c1);
printf("Enter rows and columns of matrix2:\n");
scanf("%d%d",&r2,&c2);
if(c1==r2){
printf("Enter elements of matrix1\n");
read(a,r1,c1);
printf("Enter elements of matrix2\n");
read(b,r2,c2);
printf("After multiplying a&b matrices\n");
mul(a,b,c,r1,c2,r2);
write(c,r1,c2);
}
else{
printf("Multiplication is not possible\n");
}
}
int read(int x[10][10],int m,int n){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&x[i][j]);
}
}
}
int mul(int a[10][10],int b[10][10], int c[10][10],int r1,int c2,int r2){
int i,j,k;
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
c[i][j]=0;
for(k=0;k<r2;k++){
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
int write(int c[10][10],int m,int n){
int i,j;
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",c[i][j]);
}
}
}
Output 1:
Enter rows and columns of matrix1
32
Enter rows and columns of matrix2:
32
Multiplication is not possible
Output 2:
Enter rows and columns of matrix1
22
Enter rows and columns of matrix2:
22
Enter elements of matrix1
12
34
Enter elements of matrix2
65
42
After multiplying a&b matrices
14 9
34 23

iii. Transpose of a matrix with memory dynamically allocated for the new matrix as row and
column counts may not be same.
Source Code:
void transpose_matrix(int[][10],int,int);
int main()
{
int a[10][10],r, c, i, j;
printf("Enter rows and column of matrix\n");
scanf("%d%d", &r, &c);

/* Storing element of matrix entered by user in array a[][]. */


printf("Enter elements of matrix\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
scanf("%d",&a[i][j]);
}
/* Displaying the matrix a[][] */
printf("Entered Matrix\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ",a[i][j]);
if(j==c-1)
printf("\n\n");
}
transpose_matrix(a,r,c);
return 0;
}
void transpose_matrix(int a[][10], int r, int c){
int trans[10][10],i,j;
/* Finding transpose of matrix a[][] and storing it in array trans[][]. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}
/* Displaying the transpose,i.e, Displaying array trans[][]. */
printf("Transpose of Matrix\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",trans[i][j]);
if(j==r-1)
printf("\n");
}
}
Output:
Enter rows and column of matrix
33
Enter elements of matrix
123
456
789
Entered Matrix
1 2 3
4 5 6
7 8 9
Transpose of Matrix
1 4 7
2 5 8
3 6 9

You might also like