You are on page 1of 46

1)Write a C Program to convert Celsius to Fahrenheit and vice versa.

Ans:
Flowchart :

ALGORITHM:
Step 1: Start.
Step 2: Read C.
Step 3: F=(9*C)/5+32.
Step 4: Print F.
Step 2: Read C.
Step 3: C=(5(F-32))/9.
Step 4: Print C.
Step 5: Stop.
PROGRAM:
#include<stdio.h>
int main()
{
float c,f;
printf("Enter The Temparature in celcius:\n");
scanf("%f",&c);
f=(9*c/5)+32;
printf("Temparature is Farenhite is %f\n",f);
printf("Enter Temparature in Farenhite:\n");
scanf("%f",&f);
c=(f-32)/9*5;
printf("Temparature is Celcius is %f\n",c);
return 0;
}

Test caes :
Input :
f=45 c=45
output :
temperature in Celsius = 7.222 temperature in farenheit=113.000
2) Write a C Program to find largest of three numbers using ternary operator.
Ans:
FLOWCHART:

ALGORITHM:
Step 1: Start
Step 2: Read A,B & C Values.
Step 3:Max =(A>B ? A>C ? A : C: B>C ? B: C);
Step 4: Print Max.
Step 5: Stop.
Program:
#include<stdio.h>
main()
{
int a,b,c;
printf("enter any three integers:\n");
scanf("%d%d%d",&a,&b,&c);
int big=a>b ?(a>c ? a : c ) : (b>c ? b : c);
printf("biggest number =%d\n",big);
}

Test case :
Input :a=1 , b=2 , c=3
Output :
biggest number = 3
3.Write a C Program to Calculate area of a Triangle using Heron's formula.
Ans:
FLOWCHART:

Algorithm:
Step 1: Start.
Step 2: Read sides of a Triangle say a,b and c.
Step 3: Calculate perimeter say p=(a+b+c)/2.
Step 4: Calculate Area= sqrt(p(p-a)(p-b)(p-c)).
Step 5: Print Area.
Step 6: Stop.
Program:
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,area,p;
printf("\nPlease Enter three sides of triangle\n");
scanf("%f%f%f",&a,&b,&c);
p = (a+b+c)/2;
area = sqrt(p*(p-a)*(p-b)*(p-c));
printf("\n Area of triangle = %.2f\n",area);
return 0;
}

Test case :
Input :a=25 , b=25, c=25
Output :
area of triangle = 270.63
4) Write a C Program to Find Whether the Given Number is Armstrong
Number or not.
Ans:
FLOWCHART:
ALGORITHM:
Step 1:start
Step 2: Accept the number say num.
Step 3: set sum=0 and temp =num.
Step 4: r=num%10.
Step 5: sum=sum+(r*r*r)
Step 6: num=num/10.
Step 7: repeat steps 4 to 6 until num>0.
Step 8: if sum=temp print “number is Armstrong” otherwise print
“number is not Armstrong”.
Step 9: stop.
PROGRAM : #include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&num);
temp=num;
while(num>0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Test case :
Input :num = 123 num=153

Output :
not a Armstrong number Armstrong umber
5) Write a C program to print Floyd Triangle.
Ans:
FLOWCHART:
ALGORITHM:
Step 1:start.
Step 2: Read Number of Lines as N.
Step 3: let Line = N, X = 1.
Step 4: Let a =1
Step 5: y =1
Step 6: print a
Step 7: a=a+1.
Step 8: if y <= line Then Goto Step 6.
Step 9: line = line-1.
Step 10: X=X+1.
Step 11: if X<=N Then Goto Step 5.
Step 12: Stop.
PROGRAME:
#include <stdio.h>
main()
{
int n, x, y, num = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
for (x = 1; x <= n; x++)
{
for (y = 1; y <= i; y++)
{
printf("%d ",num);
num++;
}
printf("\n");
} }
Test case :Input :num= 5 output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
6) Write a C program to check whether given number is palindrome or not.
Ans:FLOWCHART:
ALGORITHM:

Step 1:start.
Step 2: Accept the number say n.
Step 3: set reversedInteger = 0 and remainder, originalInteger=n.
Step 4: remainder=n%10.
Step 5: reversedInteger = reversedInteger*10 + remainder;
Step 6: n=n/10.
Step 7: repeat steps 4 to 6 until (n!=0).
Step 8: if originalInteger == reversedInteger print “n is Palindrome”
otherwise print “n is not a palindrome”.
Step 9: stop.
PROGRAME :
#include <stdio.h>
main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
}
Test case :
Input : n=123 n=121
Output :123 is not a palindrome 121 is a palindrome
7)Write a C program to print all natural numbers from 1 to n. - using while
loop
Ans:
Flowchart :
Algorithm:
Step 1: Start
Step 2: Read n.
Step 3: Set x=1.
Step 4: if(x<=n) then Print x and x =x+1 repeat until condition is
satisfied otherwise exit.
Step 5:Stop.
Program:
#include<stdio.h>
main()
{
int n,x=1;
printf("Enter The Value Of n\n");
scanf("%d",&n);
while(x<=n)
{
printf("%d\n",x);
x++;
}
}
Test case: Input : n=5 Output: 1
2
3
4
5
8) Write a C program to find the roots of a Quadratic Equation.
ALGORITHM: Step 1:start
Step 2:read a,b,c values
Step 3:calculate D=b2-4ac;
Step 4:if(D>0) then continue ,else goto step 8
Step 5:calculate
root1= ( -b+sqrt(D))/2a;
root2= root1= ( -b-sqrt(D))/2a;
Step 6:Display roo1 and root2 values
Step 7:display the roots are real and different goto step 15
Step 8:if(D==0) then goto step 10,else goto step 12
Step 9:calculate root1= -b/2a; root2= -b/2a;
Step 10:display root1 and root2 values
Step 11:display roots are real and equal goto step 15
Step 12:calculate
real= -b/2a;;
imag= sqrt(-D)/2a
Step 13:calculate
root1=real+imag;
root2=real-imag;
Step 14: display root1 and root2 gotoste 15
Step : stop

Program :
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,D;
float root1,root2,real,imag;
printf("enter a,b,c values like(ax2+bx+c)\n");
scanf("%f%f%f",&a,&b,&c);
D=(b*b)-4*a*c;
if(D>0)
{
root1=(-b+sqrt(D))/(2*a);
root2=(-b-sqrt(D))/(2*a);
printf("ROOT1 = %.f\n",root1);
printf("ROOT2 = %.f\n",root2);
}
else if(D==0)
{
root1=-b/(2*a);
root2=-b/(2*a);
printf("ROOT1 = %.f\n",root1);
printf("ROOT2 = %.f\n",root2);
}
else
{
real=-b/(2*a);
imag=sqrt(-D)/(2*a);
root1=real;
root2=imag;
printf("ROOT1 = %.f + %.fi\n",root1,root2);
printf("ROOT2 = %.f - %.fi\n",root1,root2);

}
}

Test case :
Input :
a=1 ,b=3 c=4
output :
root1 = 4
root2 = -1

9) Write a C program to perform matrix addition.


Ans:
ALGORITHM:
Step 1: Start
Step 2: Declare variables and initialize necessary variables
Step 3: Read number of row and colums of the A matrix(r1,c1)
Step 4: Read number of row and columns of the B matrix(r2,c2)
Step 4: If r1==c2 goto step 6,else goto step 10.
Step 6: Read matrix A goto step 7
Step 7: Read matrix B goto step 8
Step 8: Multiply A,Bss matrices using nested loops.
Step 9: Display the matrix multiplication, then goto step 11
Step 10: Multiplication Matrix is not possible
Step 11: Stop

Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,r1,c1,r2,c2;
printf("enter A matrix order:\n");
scanf("%d%d",&r1,&c1);
printf("enter B matrix order:\n");
scanf("%d%d",&r2,&c2);
if(r1==c2)
{
printf("enter A matrix...\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("enter B matrix...\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("addition matrix of Aan B...\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",a[i][j]+b[i][j]);
}
printf("\n");
}
}
else
printf(“invalid order of matrix\n”);
}

Test case :
Input :
r1 = 2 , c1=2 , r2 = 2 , c2 = 2

A=1 1 B=1 1
1 1 1 1

Output :
Addition matrix c= 2 2
2 2
10) Write a C program to perform matrix multiplication.
Ans:ALGORITHM:
STEP 1:Start
STEP 2:Declare variables and initialize necessary variables
STEP3:Enter the element of matrices by row wise using loops
STEP4:Check the number of rows and column of first and second
matrices
STEP5:If number of rows of first matrix is equal to the number of
columns of second matrix, go to step 6. Otherwise, print matrix
multiplication is not possible and go to step 8.
STEP6:Multiply the matrices using nested loops.
STEP7:Print the product in matrix form as console output.
STEP8:Stop
Flowchrt:
Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,r1,c1,r2,c2,k;
printf("enter A matrix order:\n");
scanf("%d%d",&r1,&c1);
printf("enter B matrix order:\n");
scanf("%d%d",&r2,&c2);
if(r1==c2)
{
printf("enter A matrix...\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("enter B matrix...\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("mulitiplication matrix of Aan B...\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]+=a[i][k]*b[k][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf(“invalid matrix order:\n”);
}

Test case :

Input : r1 = 2 , c1=2 , r2 = 2 , c2 = 2

A=2 2 B=22
2 2 22
Output :
multiplication matrix c= 8 8
8 8
11)Write a C program to find maximum and minimum element in an array. -
using recursion.
Ans:
Program:
#include <stdio.h>
int maximum(int array[], int index, int len);
int minimum(int array[], int index, int len);
int main()
{
int n;
printf("enter array size:\n");
scanf("%d",&n);
int array[n], max, min;
int i;
printf("Enter %d elements in array: ", n);
for(i=0; i<n; i++)
{
scanf("%d", &array[i]);
}

max = maximum(array, 0, n);


min = minimum(array, 0, n);
printf("Minimum element in array = %d\n", min);
printf("Maximum element in array = %d\n", max);
return 0;
}
int maximum(int array[], int index, int len)
{
int max;
if(index >= len-2)
{
if(array[index] > array[index + 1])
return array[index];
else
return array[index + 1];
}
max = maximum(array, index + 1, len);
if(array[index] > max)
return array[index];
else
return max;
}
int minimum(int array[], int index, int len)
{
int min;

if(index >= len-2)


{
if(array[index] < array[index + 1])
return array[index];
else
return array[index + 1];
}

min = minimum(array, index + 1, len);

if(array[index] < min)


return array[index];
else
return min;
}
Test case :
Input :
n=5
12345
Output :
Minimum element in array = 1
Maximum element in array = 5
12) Write a C Program for swapping of two integer values using call-by-value.
Ans:
ALGORITHM:
Step 1: Start
Step 2: read any two numbers (a,b).
Step 3: Call the function swap(a,b);
Step 4: Start function.
Step 5: calculation
x=x+y;
Y=x-y;
X=x-y;
Step 6: display swapped number in function x,y
Step 7: display swapped number in main function
Step 8: Stop.

Program:

#include<stdio.h>
void swap(int x,int y);
main()
{
int n1,n2;
printf("enter any two integers:\n");
scanf("%d%d",&n1,&n2);
swap(n1,n2);
printf("n1 value in main=%d\n",n1);
printf("n2 value in main=%d\n",n2);
}
void swap(int x,int y)
{
x=x+y;
y=x-y;
x=x-y;
printf("x value in function =%d\n",x);
printf("y value in function =%d\n",y);

Test case :
Input : n1= 2 , n2= 3
Output :
n1 value in main=2
n2 value in main=3
x value in function = 3
y value in function = 2
13) Write a C Program for swapping of two integer values using call-by-
reference.
Ans:
ALGORITHM:
Step 1: Start
Step 2: read any two numbers (a,b).
Step 3: Call the function swap(&a,&b);
Step 4:Start function.
Step5:calculation
*x=*x+*y;
*y=*x-*y;
* x=*x-*y;
Step 6: display swapped number in function *x,*y
Step 7: display swapped number in main function
Step 8: Stop.
Program:
#include<stdio.h>
void swap(int *x,int *y);
main()
{
int n1,n2;
printf("enter any two integers:\n");
scanf("%d%d",&n1,&n2);
swap(&n1,&n2);
printf("n1 value in main=%d\n",n1);
printf("n2 value in main=%d\n",n2);
}
void swap(int *x,int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf("x value in function =%d\n",*x);
printf("y value in function =%d\n",*y);
}
Test case Input : n1= 2 , n2= 3
Output :
n1 value in main=3
n2 value in main=2
x value in function = 3
y value in function = 2
14) Write a C Program to find Fibonacci, Factorial of a number with
Recursion
Ans:
//FIBINACCISERIES
Flowchart :
ALGORITHM:

Step 1: Start
Step 2: read limit of the series(n).
Step 3: Initialize variables a=0, b=1;
Step 4: Display a,b.
Step 5: for(i=0;i<n;i++)Repeat the steps until limit n else goto step 8.
Step 6: C=a+b,goto step 7.
Step 6: Display c ,then goto step 7.
Step 7: calculation
a=b;
b=c; then goto step 5.
Step 8: Stop
Program:
//FIBINACO SERIES
#include<stdio.h>
main()
{
int a=0,b=1,n;
int i,c;
printf("enter limit for fibonacci series:\n");
scanf("%d",&n);
printf("%d %d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
}

Test case :
Input : n= 5
Output : 0 1 1 2 3
//FACTORIAL
Flowchart :
Algorithm :
Step 1: Start .
Step 2: Initialize variables n,fact=1,i=1
Step 3: Read value of n
Step 4: for i=1 Repeat the steps until i<=n,elsegoto step 6
fact=fact*i;, then i++ ,goto step 4.
Step 5: Display factorial
Step 6: Stop
//FACTORIAL Program

#include<stdio.h>
void main()
{
int n,i,fact=1;
printf("enter number for factorial:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("factorial of %d =%d\n",n,fact);
}
Test case :
Input : n=5
Output : factorial of 5 = 10
15) Implementation of string manipulation operations without library function:
a) copy
ALGORITHM:
Step1:start
Step2:read a string
Step3:while (a[i]!=’\0’) until repeat the steps,elsegoto step 7
Step4:b[i]=a[i] then i++
Step5:b[i]=’\0’
Step 6:display copied string b
Step 7: stop
PROGRAM :
#include <stdio.h>
main() {
char s1[20],s2[20], i;
printf("Enter string s1: ");
gets(s1);
for (i = 0; s1[i] != '\0'; ++i) {
s2[i] = s1[i];
}
s2[i] = '\0';
printf("copied String s2: %s", s2);
}
Test case :
Input : s1 = hi
copied string2 s2 =hi
b) concatenate
ALGORITHM:
Step1:start
Step2:initialize i=0,j=0
Step3:read 1th string(a)
Step 4:read 2nd string(b)
Step5:while (a[i]!=’\0’) until repeat the steps
Step6: i=i+1;
Step 7: while (b[j]!=’\0’) until repeat the steps
Step8: a[i]=b[j];
i=i+1;
J=j+1; goto step 6
Step 9:a[i]=’\0’
Step 10:display string a
Step 11: stop
PROGRAME:
#include <stdio.h>
int main()
{
char s1[50] = "programming ", s2[] = "is awesome";
int i, j;
for (i = 0; s1[i] != '\0'; ++i) {
printf("i = %d\n", i);
}
for (j = 0; s2[j] != '\0'; ++j, ++i) {
s1[i] = s2[j];
}
s1[i] = '\0';
printf("After concatenation: ");
puts(s1);
return 0;
}

Test case :
Output:
After concatenation

programming is awesome
C ) length
ALGORITHM :
Step1:start
Step 2:initialize i=0
Step3:read a string
Step4:while (a[i]!=’\0’) until repeat the steps,elsegoto step 7
Step5:i=i+1;
Step 6:display length of string i;
Step 7: stop
PROGRAME:
#include<stdio.h>
#include<string.h>
main()
{
char a[20];
printf("enter 1th string:\n");
gets(a);
int n=0,i;
for(i=0;a[i]!='\0';i++)
{
n++;
}
printf("length of the given string=%d",n);
}
Test case :

Input : a = welcome

Output: length of the given string = 7

D ) compare
#include<stdio.h>
#include<string.h>
Main()
{
Char a[20],b[20];
Int x:
Printf(“ENTER THE FIRST STRING AND SECOND STRING :\n”);
Scanf(“%d%d”,&a,&b);
X=strcmp(a,b);
If(x==0)
Printf(“BOTH STRINGS ARE EQUAL”);
else
Printf(“STRING ARE NOT EQUAL”);
}
Test case :
Input :
a =hi
b= hi
output :BOTH STRINGS ARE EQUAL

You might also like