CP Experiment No 9
CP Experiment No 9
EXPERIMENT NO: 9
AIM: Study of String & Pointer in C Programming.
PROGRAM 9.1: WAP to Check whether the given string is palindrome or not.
ALGORITHM:
1. Start.
2. Initialize the string str (in this case, "naman").
3. Calculate the length of the string using strlen().
4. Loop through the string:
o Compare the character at index i with the character at index len - i - 1.
o If they do not match, set flag = 1 and exit the loop.
5. Check if the string is a palindrome:
o If flag == 1, print that the string is not a palindrome.
o If flag == 0, print that the string is a palindrome.
6. End.
CODE:
int main()
{
char str[10] = "naman";
int i, len, flag = 0;
for (i = 0; i < len; i++)
{
}
}
if (flag)
printf("%s is not palindrome", str);
else
printf("%s is palindrome", str);
return 0;
}
OUTPUT:
PROGRAM 9.2: C Program Using Pointers for Arithmetic Operations (Addition, Subtraction,
Multiplication, and Division).
ALORITHM:
1. Start.
2. Declare variables:
o num1, num2 (to store the numbers entered by the user).
o ptr1, ptr2 (pointers to hold the memory addresses of num1 and num2).
3. Input the first number:
o Display "Enter first number".
o Read the number using scanf and store it in the location pointed to by ptr1.
4. Input the second number:
o Display "Enter second number".
o Read the number using scanf and store it in the location pointed to by ptr2.
VIDYA VIKAS EDUCATION TRUST'S
UNIVERSAL COLLEGE OF ENGINEERING, KAMAN-401208
C - Programming Laboratory Manual
CODE:
#include <stdio.h>
int main() {
int num1, num2;
int *ptr1, *ptr2;
// Addition
printf("Addition: %d + %d = %d\n", *ptr1, *ptr2, (*ptr1) + (*ptr2));
// Subtraction
printf("Subtraction: %d - %d = %d\n", *ptr1, *ptr2, (*ptr1) - (*ptr2));
// Multiplication
printf("Multiplication: %d * %d = %d\n", *ptr1, *ptr2, (*ptr1) * (*ptr2));
if (*ptr2 != 0)
{
printf("Division: %d / %d = %.2f\n", *ptr1, *ptr2, (float)(*ptr1) / (*ptr2));
}
else
{
printf("Division: Cannot divide by zero.\n");
}
return 0;
}
VIDYA VIKAS EDUCATION TRUST'S
UNIVERSAL COLLEGE OF ENGINEERING, KAMAN-401208
C - Programming Laboratory Manual
OUTPUT:
Result: Thus the Program to Study of Array and Pointer was implemented, written,
complied, executed and output verified.