0% found this document useful (0 votes)
10 views24 pages

C Program 4c To 6c

The document contains multiple C programming examples demonstrating various concepts such as multi-dimensional arrays, tree traversal, linear and binary search, string operations, and Armstrong number checks. Each example includes an aim, algorithm, program code, and output results, showcasing the successful execution of the programs. The document serves as a practical guide for implementing fundamental programming techniques in C.

Uploaded by

sowmisoms0
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)
10 views24 pages

C Program 4c To 6c

The document contains multiple C programming examples demonstrating various concepts such as multi-dimensional arrays, tree traversal, linear and binary search, string operations, and Armstrong number checks. Each example includes an aim, algorithm, program code, and output results, showcasing the successful execution of the programs. The document serves as a practical guide for implementing fundamental programming techniques in C.

Uploaded by

sowmisoms0
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/ 24

EX. No: 4.

c PROGRAM USING MULTI-DIMENSIONAL ARRAY


DATE:

AIM:
To implement a C Program to perform Array using multi-Dimensional array.
ALGORITHM:
Step1: Start the program
Step2: Assign the variables of test.
Step3: Print the value and store the input using multi- dimensional array.
Step4: Perform the for-loop asper the condition and display the values.
Step5: Stop the program.
PROGRAM:
I|C Program to store and print 12 values entered by the user
#include <stdio.h>
void main ()

int test [2][3J[2];


printf("Enter 12 values: In"):
for (int i= 0; i<2; +ti)
for (int j= 0;j< 3; ++j)
for (int k =0; k<2; +tk)
scanf("%d", &test[i]GJ[K);

I/Printing values with proper index.


printf("nDisplaying values: n");
for (int i=0; i<2; tti)

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


for (int k=0; k< 2; ++k)

printf("test[od] [%d] [%d] = %d n", i, j, k, test[i][][k):


getch();

OUTPUT
Enter 12 values:

2
3
4
5
6
7

9
10
11
12

Displaying Values:
test [0][0J[0] = 1
test [O][0j[1] = 2
test [0][1][0]=3
test [0][1][1] = 4
test [0][2][0] = 5
test [0][2][1]= 6
test [1][0][0]= 7
test [1][0][1] = 8
test [1][1][O] =9
test [1][1][1]= 10
test [1][2][0] = 11
test [1][2][1] = 12

RESULT:

Thus Program to using multi-Dimensional arrayexecuted successfully.


EX. No: 4.c PROGRAM OF TREE TRAVERSAL USING ARRAY
DATE:

AIM:
To implement a C Program to perform Traversal Array using multi-Dimensional
array.

ALGORITHM:
Stepl: Start the program
Step2: Assign the variables list and ivariable for loop.
Step3: Get the value of array elements and perform the loop asper the loop
condition.
Step4: Display the values(i.e., list) of traversal array.
Step5: Stop the program.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void main ()
int list [5], i;
/* Getting Array Elements from User */
printf("Enter array elements: \n");
for (i-0; i<5; itt)
scanf("%d", &list[i]);
printf("n");
j* Traversing Array and Displaying them */
for (i-0; i<s; itt)
printf("n List [id] %d", i, list[il):

getch();
OUTPUT:

RESULT:

Thus C Program to perform Traversal Array using multi-Dimensional array was


executed successfully.
EX. No: 4.e PROGRAM OF LINEAR SEARCH
DATE:

AIM:
To implement a C Program of linear search using one-Dimensional array.

ALGORITHM:
Step 1: set pos =-1
Step 2: set i= 1
Step 3: repeat step 4 while i <=n
Step 4: if a•i] == val
set pos =i
print pos
go to step 6
[end of if]
set ii =i+1
[end of loop]
Step 5: ifpos = -1
print "value is not present in the array"
fend of if]
Step 6: exit.

PROGRAM:

#include <stdio.h>
int linearSearch(int a[], int n, int val) {
/Going through array sequentially
for (int i=0; i<n; itt)
if (a[i] val)
return i;

return -1;

void main () {
int a (] = {70, 40, 30, 1|, S7, 41, 25, 14, 52); // given array
int val = 41;// value to be searched
int n = sizeof(a) / sizeof(a [0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The clements of the array are: "):
for (int i 0; i<n; itt)
printf("%d", afil);
printf("nElement to be scarched is :%d", val):
if (res m -|)
printf("nElement is not present in the array");
clse
printl("nElement is present at %dposition of array", res);
getch();

OUTPUT:
The clemnents of thcarray are: 70 40 30 1157 4| 25 14 52
Element to be searched is: 41
Element is present at 6 positionof array

RESULT:

Thus C Program of linear search using one-Dimensional array was executed


successfully.
EX. No: 4.f PROGRAM OF BINARY SEARCH
DATE:

AIM:
To implement a C Program of binary search using iterative call.
ALGORITHM:
Step 1: Find the middle elernent of array.
using, middle = initial value tend value /2;
Step 2: If middle =clement, return 'element found' and index.
Step 3: if middle>element, callthe function with end_value =middle - 1.
Step 4: if middle < element, call the function with start value = middle + 1.
Step 5: exit.
PROGRAM:
#include <stdío.h>
int iterativeBinarySearch(int array[], int start index, int end index, int element)
while (start index end index)

int middle = start index + (end index- start index)/2;


if (array[middle] =element)
return middle;
if (array[middle] < element)
start index = middle + 1;
else
end index = midd!le - 1;

return -l;

int main(void)

intarray ||= 1,4, 7,9, 16, 56, 70};


int n 7;
int element = 16;
int found index iterativeBinarySearch(array, 0, n-1, element);
if (found index -l)
printf("Eement not found in the array ");
else {
printf("Element found at index :%d",found index);
return 0;

OUTPUT:
Element found at index: 4

RESULT:
Thus C Program of binary search using iterative call was executed successfully.
EX. No: 4.g PROGRAM TO CREATE AN ARRAY OF STUDENT NAMES
DATE:

AIM:
To implement a C Program to create an array of student names.
ALGORITHMS:
Stepl: Start the program.
Step2: Declare the variables using char and initialize i value for looping statement.
Step3: To get the namne list in array by using for loop.
Step4: Tostore the list of student names using for loop.
Step5: Stop the program.
PROGRAMS:
I/CProgram to create an array of student names
#include<stdio.h>
void main ()

char name [5][20];


I/This can store 5 names: maximum 20characters in each name
int i;
/Logic to Read name of 5 students
for (i-0; i<5; itt)
{
printf("Enter name %d:", i+1);
gets(name[il);
printf("List of student names stored: \n");
//Logic to print name of 5 students
for (i=0; i<s; itt)

printf("%s In", name[i]);

getch();

Output:
Enter name 1: Amit
Enter name 2: Sunit
Enter name 3: Kamlesh
Enter name 4: Bhargav
Enter name 5: Alpesh
List of student names stored:
Amit
Sunit
Kamlesh
Bhargav
Alpesh

RESULT:
successfully.
Thus C Program to create an array of student names was executed
EX. No: 5
DATE: PROGRAM USING STRINGOPERATIONS
AIM:
To implement a C program using strings and its
operations.
ALGORITHMS:
Stepl: Start the program.
Step2: Declare the values using string.
Step3: To perform the string operations using
given values.
Step4: To Get the output of the string using string
Step5: Stop the program. operations.
PROGRAM 1:
//Program to count characters in given string using strlen function.
#include<stdio.h>
#include<string. h>
void main ()
char str [20] ="AHMEDABAD";
int i, cnt-0, len=0;
len=strlen(str);
printf(Length of the string is %d",len)
for (i-0; i<len; it+)
if(str[i] -='A')
cntt+;

printf("Total Ain given string:%d",cnt);


getch();

OUTPUT:
Length of the string is 9
Total A in given string: 3
PROGRAM 2:
/String Processing - Concatenation using strcat function.
#include<stdio.h>
#include<string.h>
int main()

char strl[10]="12345", str2[10]="abcd";


strcat(str1,str2);
puts(strl);
return 0;

OUTPUT:

12345abcd

PROGRAM 3:
//Count occurrences of alphabets in the given paragraph.
/This program willcount only A to Z alphabets.
#include<stdio.h>
#include<string.h>
void main()
int i, counter[26]-{}, ch;
char str[100];
puts("Enter your paragraph:");
gets(str);
for(i-0; i<strlen(str); it+)

ch=str[i];
counter[ch-65]++;

printf("Character and its frequency are as under:in");


for(i-0; i<26; itt)
if(counter[i]!=0)
printf("%c =%d \n", it65,counter[il);

getch();

OUTPUT:
Enter your paragraph:
HELLO STUDENT HOWARE YOU
Character
A =1
and its frequency arc as under:
D=1
E=3
H=2
L=2
N=1
O=3
R=1
S=1
T=2
U=2
W=1
Y=1

PROGRAM 4:
#include <stdio.h>
#include <string.h>
void main ()
char strl[10]= "12345";
char str2[10]= "World";
strcpy(strl, str2, 4);
strl [4]=\0';
printf("strcpy( strl, str2, 3) =%s\n", strl );
getch);

OUTPUT:
strncpy (strl, str2, 3)= Worl

PROGRAM 5:
//Cprogram to printpassword as star ( *****
#include<stdio.h>
#include<string.h>
void main()
int i:
char chl, password[ 10]:
printf("Enter your password ").
for(i 0, i<10, i+)
chl"getch().
password[il-chl;

iflch| !=13) /13 is ASCIlof Enter key


printf("*");
if(chl = 13)
break;

password[ij=\0:;
printf(" n%s", password);
getch);

PROGRAM 6:
I/Use of built-in Cstring library function.
#include<stdio.h>
#include<string.h>
void main()

int len;
char strl[10], str2[10];
puts("Enter String 1:");
gets(strl );
puts("Enter String 2:");
gets(str2);
I/Function to find length of given string 1.
len = strlen(str l);
printf("nLength of String 1is %d.n", len);
/Function to compare two strings
if(stremp(strl,str2)==0)
printf("nString Iand String 2 are same. n");
else
printf("nString I and String 2 are not same.\n");
I/Function to copy string I into string 2
strcpy(str2,strl ); /this willcopy strl into str2
printf(" nString 2 after execution of strcpy =%s", str2);
etch);
OUTPUT:
Enter String 1:
abc
Enter String 2:
Xyz
Length of String 1 is 3.
String I and String 2 are not same.
String 2 after execution of strcpy = abc
PROGRAM 7:
/CProgram to check given string is Palindrome or not.
#include <stdio.h>
#include <string.h>
void main()

char a[10], b[10];


printf("Enter your string:n");
gets(a);
strepy(b,a);
strrev(b);
if (strcmp(a,b) -=0)
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome. n");
getch);

OUTPUT:
Enter your string:
ABA
Entered string is a palindrome.

RESULT:
obtained
The C program to perform various string operation was executed and
output.
Ex .No:6.a CPROGRAM TO FIND THE GIVEN NUMBER AS AN ARMSTRONG
NUMBEROR NOT USING USER DEFINED FUNCTION
Date:

AIM:

To find whether a given number in Armstrong or not

ALGORITHM:

Step 1: start
Step 2:read n
Step 3:assign sum ¬0,Ifmfn,count =0
Step 4:if m>0 repeat
Step 4.1:mm/10
Step 4.2:count++
Step 4.3:until the condition fail
fail
Step5: ifb0repeat step 4 untilcondition
Step 5.1:rem-I%10
Step 5.2:sumf-sumtpow(rem,count)
Step 5.3:I<V10
print not armstrong
Step 6:ifn=sum print Armstrong otherwise
Step 7:stop
PROGRAM:
#include<stdio.h>
int isArmstrong(int number)

/ declare variables
int lastDigit = 0;
int power = 0;
int sum = 0:

/ temporary variable to store number


int n = number;

while(n!=0) {
I/ find last digit
lastDigit =n %10;
I/find power of digit (order = 3)
power = 1astDigit*lastDigit*lastDigit;
I/add power value into sum
sum t= power;

Il remove last digit


n = 10;

if(sum number) return 0;


else return 1;

int main()
int number;

printf("nEnter number: ");


scanf("%d",&number);
if(isArmstrong(number) ==0)
printf("n%d is an Armstrong number.n", number);
else
printf("%d is not an Armstrong number.", number);
return O;
}

OUTPUT:
Enter number: 153
153 is an Armstrong number

RESULT:

Thus, a CProgram tofind whether a given number in Armstrong or not was executed and the
outputwas obtained.
Ex .No:6.b CPROGRAM TO FIND MAXIMUM OF GIVEN THREE NUMBERS
USING FUNCTION WITH PARAMETER
Date:

AIM:

To find maximum of three number using sub function with three parameters

ALGORITHM:

main program
Step 1: start
Step 2: call subprogram as numl=input();num2=input();num3=input)
Step 3: callsub program as largest-large(num1,num2,num3)
Step 4: call subprogram as display(largest)
Step 5: stop
Sub program: large(float a, float b, float c)
Step 1: if a> b and a>c;return a
Step 2: ifb>a and b>c;return b;
Step 3: other wise return c to main program
Sub program: input)
Step 1: read n
Step 2: return n
Sub program: display(float max)
Step 1: print max
PROGRAM:

#include stdio.h
/tunctions declaration
tloat input();
tloat large(tloat a, float b, float c);
voiddisplay( float max);
l/ nmain function
int main()

float numl, nun2, num3,largest;


numl input();
num2 input();
num3 input();
largest = large(num1, num2, num3);
display(largest);
return 0;

I/function for taking input from user


float input)
float n;
printf("nEnter number: "):
scanf("%f", &n);
return n;

I/function to find sum of two numbers


float large(float a, float b, float c)
if(a>-b && a>-c) return a;
else if(b>=a&& b=c)return b;
else returnc:

Hfunction for displaying the result


void display((loat max)
printf("nLargest number %.21",max ):
OUTPUT:
Enter number: 10
Enter number:20
Enter number:4
Largest number -20

RESULT: Thus, a C Program to find Maximum of three numbers using function was executed
and the output was obtaincd.
Ex .No:6.c C PROGRAM TOSWAP TWONUMBER USING PASSBY VALUE AND
REFERENCES
Date:

AIM:

To swap to values using pass by value and pass by reference with functions

ALGORITHM:

a)
main program
Step 1: start
Step 2: read a, b as integer value
Step 3: callsub program as swap(a,b)
Step 4: stop
Sub program: swap(int a, int b)
Step 1: assign temp to a
Step 2: assign a to b
Step 3: assign b to temp
Step 4: display a and b

b)
main program
Step 1: start
Step 2: read a, b as integer value
Step 3: call sub program as swap(&a,&b)
Step 4: stop
Sub program: swap(int *a, int *b)
Step 1: assign temp to *a
Step 2: assign *a to *b
Step 3: assign *b to temp
Step 4: display a and b
PROGRAM:
a) Pass by value
#include <stdio.h>
void swap(int , int); /prototype of the function
int main()

int a = 10;
int b= 20;
printf("Before swapping the values in main a = %d, b=%dn",a,b);
swap(a,b);
printf("nAfter swapping values in nmain a =od, b=%dn",a,b);}
void swap (int a, int b)
int temp;
temp = a;
a=b;
b-temp;
printf("nAfter swapping values in function a =%d, b =%dn",a,b);

b) Pass by reference
#include <stdio.h>
void swap(int *, int *);/prototype of the function
int main()
int a = 10;
int b= 20;
printf("Before swapping the values in main a =%d, b=odn",ab);
swap(&a, &b);
printf("nAfter swapping values in main a=%d, b=%dn",a,b);
void swap (int *a, int *b)

int temp;
temp = *a;
*a=*b;
*b=temp;
printf("nAfter swapping values in function a=od, b=%dn",*a,*b);
OUTPUT:
a)
Before swapping the values in main a =10, b=20
After swapping values in function a = 20, b = 10
After swapping values in maina= 10, b = 20
b)

Before swapping the values in main a = 10, b = 20


After swapping values in function a=20, b= 10
After swapping values in main a =20, b=10

RESULT:
by value and pass by reference was
Thus, a C Program toperform swap operation using pass
executed and the output was obtained.

You might also like