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

CP Experiment No 9

Uploaded by

max287068
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)
10 views5 pages

CP Experiment No 9

Uploaded by

max287068
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/ 5

VIDYA VIKAS EDUCATION TRUST'S

UNIVERSAL COLLEGE OF ENGINEERING, KAMAN-401208


C - Programming Laboratory Manual

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 (str[i] != str[len - i - 1]) {


flag = 1;
break;
VIDYA VIKAS EDUCATION TRUST'S
UNIVERSAL COLLEGE OF ENGINEERING, KAMAN-401208
C - Programming Laboratory Manual

}
}
if (flag)
printf("%s is not palindrome", str);
else
printf("%s is palindrome", str);
return 0;
}

OUTPUT:

Naman is not palindrome

=== Code Execution Successful ===

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

5. Perform arithmetic operations:


o Addition:
 Access the values pointed to by ptr1 and ptr2, add them, and display the result.
o Subtraction:
 Access the values pointed to by ptr1 and ptr2, subtract them, and display the
result.
o Multiplication:
 Access the values pointed to by ptr1 and ptr2, multiply them, and display the
result.
o Division:
 Check if the second number (num2, pointed to by ptr2) is not zero.
 If it's not zero, divide the first number (num1, pointed to by ptr1) by the second
number and display the result.
 If the second number is zero, print "Cannot divide by zero."
6. End.

CODE:

#include <stdio.h>

int main() {
int num1, num2;
int *ptr1, *ptr2;

// Pointers to hold the addresses of num1 and num2


ptr1 = &num1;
ptr2 = &num2;
VIDYA VIKAS EDUCATION TRUST'S
UNIVERSAL COLLEGE OF ENGINEERING, KAMAN-401208
C - Programming Laboratory Manual

// Taking input from the user


printf("Enter first number: ");
scanf("%d", ptr1);

printf("Enter second number: ");


scanf("%d", ptr2);

// Performing arithmetic operations using pointers


printf("\nUsing pointers:\n");

// 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));

// Division (check for division by zero)

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:

Enter first number: 6


Enter second number: 6
Using pointers:
Addition: 6 + 6 = 12
Subtraction: 6 - 6 = 0
Multiplication: 6 * 6 = 36
Division: 6 / 6 = 1.00

=== Code Execution Successful ===

Result: Thus the Program to Study of Array and Pointer was implemented, written,
complied, executed and output verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy