0% found this document useful (0 votes)
11 views35 pages

C - Record - Programs

The document provides a series of C programming tasks, including algorithms and code implementations for checking Armstrong numbers, summing digits, generating Fibonacci sequences, finding largest and smallest numbers in an array, swapping integers using call by value and call by address, adding and multiplying matrices, calculating factorials using recursion, performing string operations, and searching for an element in an array. Each task includes an algorithm, a flowchart, and a sample program with input and output examples. The document serves as a comprehensive guide for problem-solving in C language.

Uploaded by

rojamani ganta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views35 pages

C - Record - Programs

The document provides a series of C programming tasks, including algorithms and code implementations for checking Armstrong numbers, summing digits, generating Fibonacci sequences, finding largest and smallest numbers in an array, swapping integers using call by value and call by address, adding and multiplying matrices, calculating factorials using recursion, performing string operations, and searching for an element in an array. Each task includes an algorithm, a flowchart, and a sample program with input and output examples. The document serves as a comprehensive guide for problem-solving in C language.

Uploaded by

rojamani ganta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROBLEM SOLVING IN C LANGUAGE

1. Write a program to check whether the given number is Armstrong


or not.

Flowchart For Armstrong Number

ALGORITHM:

Armstrong number

Step 1: start
Step 2:read n
Step 3:assign sum 0,Imn,count =0
Step 4:if m>0 repeat
Step 4.1:mm/10
Step 4.2:count++

Step 4.3:until the condition fail

Step5: if I>0 repeat step 4 until condition fail


Step 5.1:remI%10
Step 5.2:sumsum+pow(rem,count)
Step 5.3:II/10
Step 6:if n=sum print Armstrong otherwise print not Armstrong

Step 7:stop

Check Armstrong Number


PROGRAM:

#include <stdio.h>

int main()
{

int n, n1, rem, num=0;


printf("Enter a positive integer: ");

scanf("%d", &n);

n1=n;

while(n1!=0)
{
rem=n1%10;

num+=rem*rem*rem;
n1/=10;
}

if(num==n)
printf("%d is an Armstrong number.",n);
else

printf("%d is not an Armstrong number.",n);


}

INPUT:
Enter a positive integer: 371

OUTPUT:
371 is an Armstrong number.

2. Write a C program to find the sum of individual digits of a positive


integer.

FLOWCHART:
ALGORITHM:

Step 1: Start

Step 2: Read n

Step 3: Initialize sum ← 0


Step 4: while(n!=0)
Begin

Step 5: r←n%10

Step 6: sum←sum+r

Step 7: n←n/10

End
Step 8: Print “sum”
Step 9: Stop

PROGRAM :

#include<stdio.h>

#include<math.h>

void main ()

int number = 0, digit = 0, sumOfDigits = 0;

clrscr();
printf("Enter any number\n ");

scanf("%d", &number);

while (number != 0)

digit = number % 10;

sumOfDigits = sumOfDigits + digit;

number = number / 10;

} printf ("Sum of individual digits of a given number is %d",


sumOfDigits);

Input & Output:

Enter any number

1234

Sum of individual digits of a given number is 10

3. Write a program to generate the first n terms of the Fibonacci


sequence.

Write a C program to generate the first n terms of the Fibonacci


sequence.
Fibonacci Series Algorithm:
 ALGORITHM:

Step 1 : Start

Step 2 : Read n
Step 3 : Initialize f0 ← 0, f1 ← 1, f ← 0

Step 4 :i=0
Step 5 : while(i<=n) do as follows

printf("%d\t",f0);
f=f0+f1;

f0=f1;
f1=f;

i=i+1;

If not goto step 6

Step 6 : Stop
.

PROGRAM:

#include<stdio.h>

void main()

int f0,f1,f,n,i;
clrscr();
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);

f0=0;

f1=1;

printf("FIBONACCI SEQUENCE FOR THE FIRST %d


TERMS:\n",n); i=0;

while(i<n)

{
printf("%d\t",f0);
f=f0+f1;

f0=f1;

f1=f;

i=i+1;

INPUT:

ENTER THE VALUE FOR n


10
OUTPUT:
FIBONACCI SEQUENCE FOR THE FIRST 10
TERMS:
0 1 1 2 3 5 8 13 21 34
4. Write a program to find both the largest and smallest number in a
list of integer values

Algorithm to find the smallest and largest numbers in an array

1. Input the array elements.


2. Initialize small = large = arr[0]
3. Repeat from i = 2 to n
4. if(arr[i] > large)
5. large = arr[i]
6. if(arr[i] < small)
7. small = arr[i]
8. Print small and large.
// C program to find the smallest and largest element in an array

#include<stdio.h>

int main()

int a[50],i,n,large,small;

printf(“\nEnter the number of elements : “);

scanf(“%d”,&n);

printf(“\nInput the array elements : “);

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

scanf(“%d”,&a[i]);

large=small=a[0];

for(i=1;i<n;++i)

if(a[i]>large)

large=a[i];

if(a[i]<small)

small=a[i];

printf(“\nThe smallest element is %d\n”,small);

printf(“\nThe largest element is %d\n”,large);

return 0;

Output:
Enter the number of elements : 5
Input the array elements : 1 2 3 4 5
The smallest element is 1
The largest element is 5

5. Write a program to demonstrate refection of parameters in


swapping of two integer values using Call by Value.

AIM:
To write a C program to swap two numbers using call by value.
ALGORITHM:
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(a,b)
Step 3a: Start function.
Step 3b: Assign t ← x
Step 3c: Assign x ← y
Step 3d: Assign y ← t
Step 3e: Print x and y.
Step 3f: End function.
Step 4: Stop the program.
Flow chart – call by value

Program :
PROGRAM CODE:

#include<stdio.h>

void swap(int , int); // Declaration of function


void main( )
{
int a = 10, b = 20 ;
clrscr();
printf("n Before swapping");
printf ( "n a = %d b = %d", a, b ) ;
swap(a,b);// call by value : a and b are actual parameters
;
}
void swap( int x, int y ) // x and y are formal parameters
{
int t ;
t=x;
x=y;
y=t;
printf("n After swapping");
printf ( "n a = %d b = %d", x, y ) ;
}

OUTPUT:
before swap are 10 20

after swap are 20 10

6. Write a program to demonstrate refection of parameters in


swapping of two integer values using Call by Address.

Step 1: Start the program.


Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(a,b)
Step 3a: Start function.
Step 3b: Assign t ← x
Step 3c: Assign x ← y
Step 3d: Assign y ← t
Step 3e: Print x and y.
Step 3f: End function.
Step 4: Stop the program.
Program:
#include <stdio.h>

void swap(int * num1, int * num2)


{
int temp;

printf("In Function values before swapping: %d %d\n", *num1,


*num2);

temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("In Function values after swapping: %d %d\n\n", *num1,
*num2);
}

/* main() function declaration */


int main()
{
int n1, n2;

printf("Enter two numbers: ");


scanf("%d%d", &n1, &n2);

printf("In Main values before swapping: %d %d\n\n", n1, n2);

swap(&n1, &n2);

printf("In Main values after swapping: %d %d", n1, n2);

return 0;
}

Output -

Enter two numbers: 10 20


In Main values before swapping: 10 20
In Function values before swapping: 10 20
In Function values after swapping: 20 10
In Main values after swapping: 20 10

7. Write a program that to add two matrices.

ALGORITHM:
Step 1: Start

Step 2: Declare matrix A[r][c];


and matrix B[r][c];
and matrix C[r][c]; r= no. of rows, c= no. of
columns

Step 3: Read r, c, A[][] and B[][]


Step 4: Declare variable i=0, j=0

Step 5: Repeat until i < r

5.1: Repeat until j < c

C[i][j]=A[i][j] + B[i][j]

Set j=j+1

5.2: Set i=i+1

Step 6: C is the required matrix after addition

Step 7: Stop
#include <stdio.h>

#include <conio.h>

void main()

int a[2][3],b[2][3],c[2][3],i,j;

clrscr();

printf("\nENTER VALUES FOR MATRIX A:\n");

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

for(j=0;j<3;j++)

scanf("%d",&a[i][j]);

printf("\nENTER VALUES FOR MATRIX B:\n");

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

for(j=0;j<3;j++)

scanf("%d",&b[i][j]);

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

for(j=0;j<3;j++)

c[i][j]=a[i][j]+b[i][j];

printf("\nTHE VALUES OF MATRIX C ARE:\n");

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

for(j=0;j<3;j++)

printf("%5d",c[i][j]);

printf("\n");

}
[Link] a program for multiplication of twoN X N matrices

Step 1: Start

Step 2: Declare matrix A[m][n]

and matrix B[p][q]

and matrix C[m][q]

Step 3: Read m, n, p, q.

Step 4: Now check if the matrix can be multiplied or not, if n is not

equal to q matrix can't be multiplied and an error message is

generated.

Step 5: Read A[][] and B[][]

Step 4: Declare variable i=0, k=0 , j=0 and sum=0

Step 5: Repeat Step until i < m

5.1: Repeat Step until j < q

5.1.1: Repeat Step until k < p

Set sum= sum + A[i][k] * B[k][j]

Set multiply[i][j] = sum;

Set sum = 0 and k=k+1

5.1.2: Set j=j+1

5.2: Set i=i+1

Step 6: C is the required matrix.


Step 7: Stop
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;

printf("\nEnter First Matrix : n");


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

printf("\nEnter Second Matrix:n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}

printf("\nMultiplication Of Two Matrices : \n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}

return (0);
}
9. Write a program to calculate factorial of given integer value using
recursive functions
ALGORITHM :

Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return f
#include<stdio.h>

void main( )
{
clrscr( )
int factorial(int);
int n,f;
printf("Enter the number: ");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of the number is %d",f);
;
}
int factorial(int n)
{
int f;
if(n==1)
return 1;
else
f=n*factorial(n-1);
return f;
}

Output

Enter the number : 5

Factorial of the number is 120


INPUT:
ENTER A VALUE FOR n
5
OUTPUT:
THE FACTORIAL OF A GIVEN NUMBER IS..120
[Link] a program to perform various string operations.

Algorithm:
Step 1. [Initialise] Start
Step 2. Input the two strings] read s1, s2
Step 3. [perform two string compare using a function] Call the function
Str2cmp(s1,s2)
Step 4. [perform two string concatenation] Call the function
Str2cat(s1,s2)
Step 5. Find the length of two string using a function] Call the function
length(s1), length(s2) Step 6. [Finished] Stop

T
#include<stdio.h>
Void main()
{
Char string1[25],string2[25];
int l;
Clrscr();
Printf(“***** performing string length ******\n”);
Printf(“enter only one string \n”);
Scanf(“%s”,string1);
l = strlen(string1);
printf(“the string length is %d\n\n”,l);
printf(“**** performing string concatenation ****\n”);
printf(“enter two strings\n”);
scanf(“%s%s”,string1,string2);
printf(“the concatenated string is %s\n\n”,strcat(string1,string2));
printf(“***** performing string compare *****\n”);
printf(“enter two strings \n”);
scanf(“%s%s”,string1,string2);
if(strcmp(string1,string2) = = 0)
printf(“strings are equal\n”);
else
printf(“strings are not equal\n”);
printf(“*** performing string copy ****\n”);
printf(“enter the two strings\n”);
scanf(“%d%d”,string1,string2);
printf(“the first string is %s and second string is %s\n”,string1,string2);
strcpy(string1,string2);
printf(“the first string is %s and second string is %s\n”,string1,string2);
;

11. Write a program to search an element in a given list of values.


Algorithm:
Step 1: Input Key

Step 2: Index = 0

Step 3: While (Index < SizeofArray)

If (Array[Index] == Key)

Print Key Found at Location

goto Step 6

Index++

Step 4: If (Index == SizeofArray)

Step 5: Print Key Not Found

Step 6: End

FLOWCHART :
#include <stdio.h>
int main()
{
int a[10000],i,n,key;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key : ");
scanf("%d", &key);
for(i=0; i<n; i++)
{
if(a[i]==key)
{
printf("element found ");
return 0;
}
}
printf("element not found");
}
Output:
Enter size of the array: 5
Enter elements in array: 4
6
2
1
3
Enter the key: 2
element found

12. Write a program to sort a given list of integers in ascending order .

PROGRAM IN C TO SORT A LIST OF NUMBERS IN ASCENDING ORDER


ALGORITHM:

ALGORITHM:

Step 1: start

Step 2: read n

Step 3: initialize i=0

Step 4: if i<n do as follows. If not goto step 5

Read a[i]

Increment i

Goto step 4
Step 5: small=a[0], large=a[0]

Step 6: initialize i=0

Step 7: if i<n do as follows. If

not goto step 8

j=0

if j<n-1 do as follows

If a[j]< a[j+1]

t = a[j]

a[j] = a[j+1]

a[j+1] = t

Increment j
Increment i
goto Step 7
Step 8: if i<n do as follows. If not goto step 9

print a[i]

Increment i
Goto step 8

Step 9: stop
#include <stdio.h>
void main()
{
int a[20];
int i,j,n,c,flag;
clrscr();
printf("Input size of array: ");
scanf("%d", &n);
printf("Input array elements: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for (i=0; i<n-1; i++)
{
for(j=0; j<n-1-i; j++)
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
flag=0;
}
}
if(flag)
break;
else
flag=1;
}
printf("Stored elements:");
for(i=0; i<n; i++)
printf("%d,", a[i]);

input

Input size of array: 10


Input array elements: 20, 2, 10, 6, 52, 31, 0, 45, 79, 40

Output

Stored elements:0, 2, 6, 10, 20, 31, 40, 45, 52, 79

Try it now

You might also like