0% found this document useful (0 votes)
15 views9 pages

PF Rec 6a-7b

Uploaded by

akshaya.2207005
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)
15 views9 pages

PF Rec 6a-7b

Uploaded by

akshaya.2207005
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
You are on page 1/ 9

EX.

NO:6a WRITE PROGRAM TO PRINT THE SUM OF ELEMENTS OF THE ARRAY

DATE:

AIM: To print sum of elements of array.


ALGORITHIM:

o STEP 1: Start
o STEP 2: Initialize arr[] = {1, 2, 3, 4, 5}
o STEP 3: Set sum = 0
o STEP 4: length= sizeof(arr)/sizeof(arr[0])
o STEP 5: Set i=0. repeat STEP 6 and STEP 7 until i<length
o STEP 6: sum = sum + arr[i]
o STEP 7: i=i+1.
o STEP 8: PRINT "Sum of all the elements of an array:" by assigning sum.
o STEP 9: RETURN 0.
o STEP 10: End.

PROGRAM:
#include<stdio.h>
int main()
{
int arr[] ={1,2,3,4,5,6};
int sum =;
int length= sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < length;i++)
{
sum = sum+arr[i];
}
printf(“sum of all the elements of an array:%d”,sum);
return 0;
}
OUTPUT:
sum of all the elements of an array:21
EX.NO:6b COPY ELEMENTS OF ONE ARRAY INTO ANOTHER

DATE:

RESULT: The sum of all elements in a array is found using c language.

AIM: To copy elements of 1 array into another.

ALGORITHIM:

o STEP 1: Start
o STEP 2: Initialize arr1[] ={1, 2, 3, 4, 5}
o STEP 3: length = sizeof(arr1)/sizeof(arr1[0])
o STEP 4: Define arr2[length]
o STEP 5: set i=0. repeat STEP 6 and STEP 7 until (i<length)
o STEP 6: arr2[i] =arr1[i]
o STEP 7: i=i+1.
o STEP 8: Display elements of arr1[].
o STEP 9: set i=0. repeat step 10 and step 11 until (i<length)
o STEP 10: Print arr1[i]
o STEP 11: i=i+1.
o STEP 12: Print new line.
o STEP 13: Display elements of arr2[].
o STEP 14: Set i=0. repeat step 13 until (i<length)
o STEP 15: Print arr2[i].
o STEP 16: i=i+1.
o STEP 17: RETURN 0.
o STEP 18: End

PROGRAM:

#include <stdio.h>
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int length = sizeof(arr1)/sizeof(arr1[0]);
int arr2[length];
for (int i = 0; i < length; i++)
{
arr2[i] = arr1[i];
}
printf("Elements of original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
printf("Elements of new array: \n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
OUTPUT:
Elements of original array
12345
Elements of new array
12345
RESULT: The elements of the array is copied

EX.NO: 6c REVERSE AND PRINT ELEMENTS OF THE ARRAY

DATE:

AIM: To reverse the elements of an array.


ALGORITHIM:

o STEP 1: Start
o STEP 2: Initialize arr[] = {1, 2, 3, 4, 5}
o STEP 3: length= sizeof(arr)/sizeof(arr[0])
o STEP 4: Print "Original Array:"
o STEP 5: Repeat STEP 6 and STEP 7 UNTIL i<length
o STEP 6: Print arr[i]
o STEP 7: i=i+1
o STEP 8: Print new line.
o STEP 9: Print "Array in reverse order"
o STEP 10: Set i=length-1. Repeat STEP 11 and STEP 12 UNTIL i>=0
o STEP 11: Print a[i]
o STEP 12: i=i-1
o STEP 13: RETURN 0.
o STEP 14: End

PROGRAM:

#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
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");
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
RESULT: The given array is reversed.
EX.NO:7a WRITE PROGRAM TO CREATE TWO STRINGS. USE SPECIAL STRINGS
FUNCTIONS TO FIND LENGTH, COMPARE THE TWO STRINGS AND
DATE: CONCATENATE AND COPY THE STRING INTO A NEW VARIABLES.
AIM: To create 2 strings. Use special string function to find length, compare the 2 strings
and concatenate and copy the string into a new variable.
ALGORITHIM:
1)The main() calls the stringconcatenate() function to combine the two strings.
2)The function gets the string s1 length using strlen(s1).
3) Append the character of string s2[i] at s1[i+j].Repeat this step by increasing i value until
no character available in s2. Here, we append the characters of string s2 to s1 from the
end of s1.
4) After all iterations of for loop, we will get concatenated string s1.
5) The main() prints the string s1.
PROGRAM:
#include <stdio.h>
int main() {
char s1[100] = "Good", s2[] = "Day";
int length, j;
length = 0;
while (s1[length] != '\0') {
++length;
}
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];

s1[length] = '\0';
printf("After concatenation: ");
puts(s1);
return 0;
}
OUTPUT:
After concatenation: GoodDay
EX.NO: 7b CHECK IF THE GIVEN NUMBER IS A PALINDROME OR NOT

DATE:

RESULT: The string is formed and concatenation is done.

AIM: To check if the given string is palindrome or not.

ALGORITHM:

INPUT:

#include <stdio.h>

#include <string.h>

int checkpalindrome(char*s)

int i,c=0,n;

n=strlen(s);

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

if(s[i]==s[n-i-1])

c++;

if(c==i)

return 1;

else

return 0;

int main()

char s[1000];

printf("Enter the string:");


gets(s);

if(checkpalindrome(s)){

printf("string is palindrome");

else{

printf("string is not palindrome");

OUTPUT:

Enter the string: 202

string is palindrome

RESULT: The given string is checked and got the output.

You might also like