unit1234
unit1234
unit1234
C - program
_____________________________________
Program 1:
1. Write a program to calculate simple
interest. Accept Principle amount, rate
of interest and duration in years from
the user:
#include <stdio.h>
#include<conio.h>
void main() {
<Output>
Program 2:
<Output>
int a, b;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
// swapping
a = a - b;
b = a + b;
a = b - a;
printf("After swapping, a = %d\n", a);
printf("After swapping, b = %d", b);
getch();
}
<Output>
Program 3:
3. Write a program to find greatest
number from given 3 numbers from
the user using nested if and using
conditional operator.
#include<stdio.h>
#include<conio.h>
int main() {
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
// outer if statement
if (n1 >= n2) {
// inner if...else
if (n1 >= n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
<Output>
Program 4:
4. Write a program to convert
temperature from Fahrenheit to
Celsius and from Celsius to Fahrenheit.
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
clrscr();
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
/* fahrenheit conversion formula */
fahrenheit = (celsius * 9 / 5) + 32;
printf(" \n%.2f \n Celsius = %.2f
Fahrenheit", celsius, fahrenheit);
printf("\nEnter Fahrenheit: ");
scanf("%f",&fahrenheit);
/* celsius conversion formula */
celsius = (fahrenheit - 32)*5/9;
printf("\nCelsius: %f ", celsius);
getch();
}
<Output>
Program 5:
5. Demonstrate printing Table of given
number using loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int num, i; // declare a variable
printf (" Enter a number to generate the
table in C: ");
scanf (" %d", &num); // take a positive
number from the user
printf ("\n Table of %d", num);
// use for loop to iterate the number from 1
to 10
for ( i = 1; i <= 10; i++)
{
printf ("\n %d * %d = %d", num, i,
(num*i));
}
getch();
}
<Output>
Program 6:
6. Demonstrate to find factorial of
given number.
#include<stdio.h>
#include<conio.h>
void main()
{
//fact number
int i,fac = 1;
clrscr();
printf("enter the any number");
scanf("%d",&i);
while(i>=1)
{
fac = fac * i;
i--;
}
printf("factorial is = %d",fac);
getch();
}
<Output>
Program 7:
<Output>
Program 8:
8. Demonstrate to find reverse of a
given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n ,r;
clrscr();
printf("enter number : ");
scanf("%d",&n);
while(n>0)
{
r = n % 10;
printf("%d",r);
n = n/10;
}
getch();
}
<Output>
Program 9:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, i, terms;
/* Input number from user */
printf("Enter number of terms: ");
scanf("%d", &terms);
/* Fibonacci magic initialization */
a = 0;
b = 1;
printf("Fibonacci terms: \n");
/* Iterate through n terms */
for(i=1; i<=terms; i++)
{
printf("%d, ", c);
a = b;
b = c;
c = a + b;
}
getch();
}
<Output>
Program 10:
10. Demonstrate to find GCD and LCM
of given 2 numbers.
#include<stdio.h>
#include<conio.h>
int main() {
int n1, n2, i, gcd, lcm;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
// loop to find the GCD
for (i = 1; i <= n1 && i <= n2; ++i) {
// check if i is a factor of both integers
if (n1 % i == 0 && n2 % i == 0)
gcd = i;
}
lcm = (n1 * n2) / gcd;
printf("The GCD of two numbers %d and
%d is %d",n1,n2 , gcd);
printf("\nThe LCM of two numbers %d
and %d is %d.", n1, n2, lcm);
getch();
}
<Output>
Program 11:
11. Demonstrate to check whether
given is Palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,real,s=0;
clrscr();
printf("enter the number ");
scanf("%d",&n);
real = n;
while(n>0)
{
r = n % 10;
s = r+(s*10);
n = n / 10;
}
if(real == s)
{
printf("%d palindrome number",real);
}
else
{
printf("%d not palindrome number",real);
}
getch();
}
<Output>
Program 12:
12. Demonstrate to check whether the
given number is Prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,count = 0 ;
clrscr();
printf("enter the any number");
scanf("%d",&n); 5
for(i=1;i<=n;i++) 2<=5
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
printf("prime number");
}
else
{
printf("not prime number");
}
getch();
}
<Output>
Semester-1 C programming practical
Program 1:
1. Print the following pyramid. [Left
aligned, Right Aligned,
Center Aligned]
*
**
***
****
#include <stdio.h>
#include<conio.h>
void printLeftAligned(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
int rows = 4; // Number of rows in the
pyramid
printf("Left Aligned:\n");
printLeftAligned(rows);
printf("\nRight Aligned:\n");
printRightAligned(rows);
printf("\nCenter Aligned:\n");
printCenterAligned(rows);
return 0;
}
getch();
}
<Output>
Program 2:
2. To accept an integer N, if the
integer N = 4, then print the
pyramid :
1
121
12321
1234321
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Input number of rows : ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
printf("%d", j);
for(j = i - 1; j >= 1; j--)
printf("%d", j);
printf("\n");
}
getch();
}
<Output>
Program 3:
3. A program which will take 10
numbers from user and stored
it in the array. It will print all the
numbers, their sum and average of it.
#include<stdio.h>
#include<conio.h>
int main() {
int arr[10];
int sum = 0;
float avg;
int i;
clrscr();
// Input 10 numbers from the user
printf("Enter 10 numbers:\n");
for ( i = 0; i < 10; i++)
{
printf("Number %d: ", i + 1);
scanf("%d", &arr[i]);
}
for(i=0 ;i<10 ;i++)
{
sum = sum + arr[i];
}
// Calculate average
avg = sum / 10.0;
// Print the numbers, sum, and average
printf("\nThe numbers you entered
are:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n\nSum: %d", sum);
printf("\nAverage: %.2f\n", avg);
getch();
}
<Output>
Program 4:
4. Demonstrate to sort an array.
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[5] = {50, 20, 80, 70, 10};
int temp = 0;
int i,j;
printf("Elements of original array: \n");
for ( i = 0; i <5; i++) {
printf("%d ", arr[i]);
}
for ( i = 0; i < 5; i++) {
for ( j = i+1; j < 5; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\n");
printf("Elements of array sorted in
ascending order: \n");
for ( i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
getch();
}
<Output>
Program 5:
5. Demonstrate to find addition of two
matrices of 3*3.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3] = {{1,1,1},{2,2,2},{3,3,3}};
int b[3][3] = {{2,2,2},{2,2,2},{3,3,3}};
int c[3][3];
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n The Sum is :\n");
for(i=0;i<3;i++)
{
printf("\n\n");
for(j=0;j<3;j++)
{
printf("\t %d", c[i][j]);
}
}
getch();
}
<Output>
Program 6:
6. Demonstrate to find multiplication
of two matrices of 3*3.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3] = {{1,1,1},{2,2,2},{3,3,3}};
int b[3][3] = {{2,2,2},{2,2,2},{3,3,3}};
int c[3][3];
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
}
printf("\n The multiple is :\n");
for(i=0;i<3;i++)
{
printf("\n\n");
for(j=0;j<3;j++)
{
printf("\t %d", c[i][j]);
}
}
getch();
}
<Output>
Program 7:
7. Input a string from the user and
check whether the string is
palindrome or not.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()
{
char a[50],b[50];
int i,j,flag=0;
int a1,b1,c1;
clrscr();
printf("enter string : ");
gets(a);
j = 0;
for(i=strlen(a)-1;i>=0;i--)
{
b[j] = a[i];
j++;
}
b[j] = '\0';
for(i=0;i<=strlen(a);i++)
{
if(a[i]!=b[i])
{
}
flag = 1;
break;
}
}
if(flag == 0)
{
printf("\npalindrom string");
}
else
{
printf("\n not palindrome string");
}
getch();
{
<Output>
Program 8:
8. Demonstrate to print factorial of a
given number by recursive user
defined function fact(int).
#include<stdio.h>
#include<conio.h>
int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
int fact;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %d\n", number,
fact);
getch();
}
<Output>
Program 9:
9. Demonstrate to convert lowercase
string to uppercase string (without
including string.h).
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i;
clrscr();
printf("enter the string in lowercase : ");
gets(a);
for(i=0;a[i]!='\0';i++)
{
printf("%c",a[i]-32);
}
getch();
}
<Output>
Program 10:
10. Take a lowercase string from the
user and print its length and
uppercase string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10];
clrscr();
printf("enter the string : ");
gets(a);
printf("\n\tstring length is : %d",strlen(a));
printf("\n\tstring is : %s",strupr(a));
getch();
}
<Output>
Program 11:
11. A program that uses function
digit(n, k) that return the value
of the kth digit the right of the
number N. For e.g. The function call
digit (254693, 2) should return 9.
#include<stdio.h>
#include<conio.h>
int digit(int n,int k)
{
while(k>1)
{
n/=10;
k--;
}
return n%10;
}
void main()
{
int n,k;
clrscr();
printf("enter a number : ");
scanf("%d",&n);
printf("enter the position of the digit
(from the right):");
scanf("%d",&k);
if(k<1)
{
printf("invalid position");
}
else
{
int result = digit(n,k);
printf("the %dth digit from the right of %d
is :%d \n",k,n,result);
}
getch();
}
<Output>
Program 12:
12. Demonstrate to find if the given
no. is prime or not. The
function should accept the number as
an argument and
return if the no. is prime or not.
#include<stdio.h>
#include<conio.h>
void prime(int i)
{
int x,count = 0;
for(x=1;x<=i;x++)
{
if(i%x==0)
count++;
}
if(count==2)
{
printf("prime number");
}
else
{
printf("not prime number");
}
}
void main()
{
int i;
clrscr();
printf("enter the number = ");
scanf("%d",&i);
prime(i);
getch();
}
<Output>
Semester-1 C programming practical
Program 1:
1. Define structure called state having
member variables as state name,
number of districts and total
population. Input and display 5 State
data.
#include<stdio.h>
#include<conio.h>
// Define the structure
struct State {
char name[100];
int numberOfDistricts;
long totalPopulation;
};
int main() {
struct State states[5];
// Input data for 5 states
for (int i = 0; i < 3; i++) {
printf("Enter details for State %d:\n", i +
1);
printf("State Name: ");
scanf(" %s", states[i].name); // Read string
with spaces
printf("Number of Districts: ");
scanf("%d", &states[i].numberOfDistricts);
printf("Total Population: ");
scanf("%ld", &states[i].totalPopulation);
printf("\n");
}
// Display the data
printf("\nState Data:\n");
for (int i = 0; i < 3; i++) {
printf("State %d:\n", i + 1);
printf("Name: %s\n", states[i].name);
printf("Number of Districts: %d\n",
states[i].numberOfDistricts);
printf("Total Population: %ld\n",
states[i].totalPopulation);
printf("\n");
}
getch();
}
<Output>
Program 2:
2. Define a structure called Item
having member variables: Item
array and modifies each element with
an increase of 10% in code, Item
name, item price. Create an array of
structure to store five Items. Create a
function which accepts the Item
the price.
#include<stdio.h>
#include<conio.h>
struct item
{
int code;
char name[10];
int price;
};
void main()
{
struct item it[5];
int i;
clrscr();
for(i=0;i<3;i++)
{
printf("\n enter code, name and price");
scanf("%d%s%d",
&it[i].code,&it[i].name,&it[i].price);
}
for(i=0;i<3;i++)
{
it[i].price += it[i].price*0.1;
printf("\n %d %s %d",
it[i].code,it[i].name,it[i].price);
}
getch();
}
<Output>
Program 3:
3. Define a structure to represent a
date. Use your structures
that accept two different dates in the
format mm dd of the
same year. Write a C program to
display the month names of
both dates.
#include<stdio.h>
#include<conio.h>
struct date
{
int month;
int day;
};
case 7: printf("july");
break;
case 8: printf("august");
break;
case 9: printf("september");
break;
case 10: printf("octomber");
break;
case 11: printf("november");
break;
case 12: printf("december");
break;
default:
printf("invalid month ");
}
}
void main()
{
struct date d1,d2;
clrscr();
printf("enter the first date(mm dd)");
scanf("%d%d",&d1.month,&d1.day);
printf("enter the second date(mm dd)");
scanf("%d%d",&d2.month,&d2.day);
printf("first date: ",d1.day);
printf("second date: ",d2.day);
getch();
}
<Output>
Program 4:
4. Define a structure that can describe
a Hotel. It should have members that
include name, address, grade, room
charges, grade and no of rooms. Write
a function to print out all hotel
details with room charges less than a
given value.
#include <stdio.h>
#include <string.h>
#include<conio.h>
// Define the Hotel structure
struct Hotel {
char name[100];
char address[100];
int grade;
float roomCharges;
int numberOfRooms;
};
// Function to print hotel details with
room charges less than a given value
void
printHotelsWithRoomChargesLessThan(str
uct Hotel hotels[], float maxCharges) {
printf("Hotels with room charges less
than %.2f:\n", maxCharges);
for (int i = 0; i < 2; i++) {
if (hotels[i].roomCharges < maxCharges) {
int main() {
// Example usage
struct Hotel hotels[100] = {
{"Hotel A", "123 Street, City", 4, 15.00,
50},
printHotelsWithRoomChargesLessThan(ho
tels, maxCharges);
getch();
}
<Output>
Program 5:
#include<stdio.h>
#include<conio.h>
struct state
{
char sta[10];
int eng_clg;
int med_clg;
int mgmt_clg;
int uni;
int t_clg;
};
void main()
{
struct state s1[5];
int i,max=0;
clrscr();
printf("\n Enter Data");
for(i=0;i<3;i++)
{
printf("state = ");
scanf("%s",&s1[i].sta);
printf("eng_clg =");
scanf("%d",&s1[i].eng_clg);
printf("med_clg =");
scanf("%d",&s1[i].med_clg);
printf("mgmt_clg =");
scanf("%d",&s1[i].mgmt_clg);
printf("univercity = ");
scanf("%d",&s1[i].uni);
s1[i].t_clg =
s1[i].eng_clg+s1[i].med_clg+s1[i].mgmt_cl
g+s1[i].uni;
if(s1[i].t_clg>max)
max=s1[i].t_clg;
}
for(i=0;i<3;i++)
{
if(max==s1[i].t_clg)
{
<Output>
Program 6:
6. Define a structure by name time
with members seconds, minutes and
hours. If time1 and time2 are two
variables of the structure type, write a
program to find the difference in time
using a function.
#include <stdio.h>
#include<conio.h>
struct time {
int seconds;
int minutes;
int hours;
};
// Calculate differences
diff.seconds = t2.seconds - t1.seconds;
diff.minutes = t2.minutes - t1.minutes;
diff.hours = t2.hours - t1.hours;
return diff;
}
int main() {
struct time time1, time2, difference;
// Input for the first time
printf("Enter time1 (hours minutes
seconds): ");
scanf("%d %d %d", &time1.hours,
&time1.minutes, &time1.seconds);
Program 7:
7. Declare structure having state
name, population, literacy rate and
per capita income. Input 5 records.
Display the state whose literacy rate is
highest and whose per capita income
is highest.
#include<stdio.h>
#include<conio.h>
struct state
{
char name[10];
int ppltn;
int litr;
int incm;
};
void main()
{
struct state s[5];
int i, maxlitr=0,maxincm=0,indlit,indincm;
clrscr();
for(i=0;i<3;i++)
{
printf("\n enter state name, population,
literacy rate and income");
scanf("%s%d%d%d",s[i].name,&s[i].ppltn
,&s[i].litr,&s[i].incm);
}
for(i=0;i<3;i++)
{
if(s[i].litr>maxlitr)
maxlitr=s[i].litr;
if(s[i].incm>maxincm)
maxincm=s[i].incm;
}
for(i=0;i<3;i++)
{
if(maxlitr==s[i].litr)
{
printf("\n State details with max literature
rate");
printf("\n
%s\n%d\n%d\n%d\n\n",s[i].name,s[i].pplt
n,s[i].litr,s[i].incm);
break;
}
}
for(i=0;i<3;i++)
{
if(maxincm==s[i].incm)
{
printf("\n State details with max income");
printf("\n
%s\n%d\n%d\n%d\n\n",s[i].name,s[i].pplt
n,s[i].litr,s[i].incm);
break;
}
}
getch();
}
<Output>
Program 8:
8. Define a structure employee with
member variables having
employee name, basic pay, dearness
allowance, house rent,
and net salary. Store data of 5
employees. Write a function
which calculates the net salary of
employees and prints all
employee details in descending order
of their net salary.
#include<stdio.h>
#include<conio.h>
struct emp
{
char name[10];
int bpay;
int da;
int hra;
int net_salary;
};
void cal(struct emp e1[],int size)
{
int i;
for(i=0;i<size;i++)
{
e1[i].net_salary =
e1[i].bpay+e1[i].da+e1[i].hra;
}
}
void main()
{
struct emp e1[5],tmp;
int i,j;
for(i=0;i<3;i++)
{
printf("\n Enter name,bpay,da,hra of
employee");
scanf("%s%d%d%d",&e1[i].name,&e1[i].b
pay,&e1[i].da,&e1[i].hra);
}
cal(e1,3);
for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++)
{
if(e1[i].net_salary<e1[j].net_salary)
{
tmp = e1[i];
e1[i]=e1[j];
e1[j]=tmp;
}
}
}
printf("\n Employee Details in a
Descending Order \n\n");
for(i=0;i<3;i++)
{
printf("\n %s %d %d %d %d",
e1[i].name,e1[i].bpay,e1[i].da,e1[i].hra,e1[
i].net_salary);
}
getch();
}
<Output>
Program 9:
#include <stdio.h>
#include <coino.h>
// Define the population structure
struct Popculation {
int men;
int women;
};
// Define the state structure that includes
the population structure
struct State {
char name[50];
struct Population population; // Nested
structure
};
scanf("%d", &state->population.women);
}
getch 0;
}
<Output>
Program 10:
10. Demonstrate the use of Union.
#include<stdio.h>
#include<conio.h>
union student
{
char name[20];
int roll;
};
void main()
{
union student stu = {"abc"};
clrscr();
stu.roll = 101;
printf("\n\t student name : %s",stu.name);
printf("\n\t student roll no :%d",stu.roll);
getch();
}
<Output>
Semester-1 C programming practical
4. Pointers
Program 1:
1. Demonstrate a user-defined
function which will swap the
values of two variables using
pointer (Call by reference).
#include <stdio.h>
#include <conio.h>
// Function to swap values using pointers
void swap(int *a, int *b) {
int temp; // Temporary variable for
swapping
temp = *a; // Store the value at address a
in temp
*a = *b; // Assign the value at address b to
the address a
*b = temp; // Assign the value in temp to
the address b
}
int main() {
int x = 5, y = 10; // Initialize two variables
clrscr();
printf("Before swapping:\n\t x = %d,\n\t y
= %d\n", x, y);
// Call the swap function with the
addresses of x and y
swap(&x, &y);
printf("After swapping:\n\t x = %d, \n\t y
= %d\n", x, y);
getch();
}
<Output>
Program 2:
2. Demonstrate a user defined
function calc(), which will return
the sum, subtraction, multiplication,
#include <stdio.h>
#include <conio.h>
void calc(int *sum, int *multi,int *sub,int
*div, int a, int b) {
*sum = a + b; // Calculate sum
*multi = a * b; // Calculate multi
*sub = a - b;
*div = a/b;
}
int main() {
int num1, num2;
int sum, multi,sub,div;
clrscr();
// Input two numbers
printf("Enter two numbers: ");
#include<stdio.h>
#include<conio.h>
int length(char *p)
{
int l = 0;
while(*p!='\0')
{
l++;
p++;
}
return l;
}
void main()
{
char str[10];
int l;
clrscr();
printf("enter string =");
gets(str);
l = length(str);
printf("\nlength = %d",l);
getch();
}
<Output>
Program 4:
4. Write a program in C to find the
maximum number between
two numbers using a pointer.
#include <stdio.h>
#include <conio.h>
int max(int *m,int *n)
{
if(*m>*n)
return *m;
else
return *n;
}
void main()
{
int num1,num2,temp;
clrscr();
#include <stdio.h>
#include <conio.h>
int main() {
int arr[5]; // Declare an array of size 5
int *p=arr; // Variable to store the number
of elements
int i;
clrscr();
// Input array elements using pointer
printf("Enter elements:\n");
for ( i = 0; i <5; i++) {
printf("Element %d: ", i + 1);
scanf("%d",arr + i); // Using pointer
arithmetic to input elements
}
// Print array elements using pointer
printf("Array elements are:\n");
for ( i = 0; i < 5; i++) {
printf("%d ", *(arr + i)); // Using pointer
#include<stdio.h>
#include<conio.h>
void main()
{ int a1[100],a2[100],*p1,*p2,i;
clrscr();
*p1=&a1;
*p2=&a2;
printf("enter array elements ...\n");
for(i=0;i<5;i++)
{
printf("enter elements in array[%d]: ",i);
scanf("%d",&*(p1+i));
}
printf("\n array elements...");
for(i=0;i<5;i++)
{
printf("%d\t",*(p1+i));
}
for(i=0;i<5;i++)
{
*(p2+i) = *(p1+i);
}
printf("\n after copy of array using
pointer\n ");
for(i=0;i<5;i++)
{
printf("\t%d",*(p2+i));
}
getch();
}
<Output>
Program 7:
7. Write a C program to compare two
strings using pointers.
#include<stdio.h>
#include<conio.h>
int main()
{
char *a,*b,str1[50],str2[50];
clrscr();
printf("enter a first string : ");
gets(str1);
printf("enter a second string : ");
gets(str2);
a = str1;
b = str2;
while(*a!='\0' && *b!='\0')
{
if(*a!=*b)
{
printf("both string are not equals ");
}
break;
}
if(*a==*b)
{
printf("both string are equals");
}
getch();
}
<Output>
Program 8:
8. Write a C program to find reverse of
a string using pointers.
#include <stdio.h>
#include <conio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
char *p1,*p2;
clrscr();
printf("enter a string : ");
gets(str1);
p1 = str1+strlen(str1)-1; //base
address+length-1;
p2 = str2;
while(p1>=str1)
{
*p2 = *p1;
p2++;
p1--;
}
*p2 = '\0';
printf("original string : %s",str1); ssssss
printf("\nreverse string : %s",str2);
getch();
}
<Output>
Program 9:
9. Write a C program to sort array
using pointers.
#include<stdio.h>
#include<conio.h>
void sort(int *ptr)
{
int i,j,t;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(*(ptr+i)>*(ptr+j))
{
t = *(ptr+i);
*(ptr+i) = *(ptr+j);
*(ptr+j) = t;
}
}
}
for(i=0;i<5;i++)
{
printf("\n\t%d",*(ptr+i));
}
}
void main()
{ int arr[5] = {100,500,200,400,300};
clrscr();
sort(arr);
getch();
} <Output>
Program 10:
10. Write a C program to concatenate
two strings using pointers.
#include <stdio.h>
#include <conio.h>
void concatenateStrings(char *str1, char
*str2)
{
while (*str1 != '\0') {
str1++;
}
// Copy characters from the second string
to the end of
// the first string
while (*str2 != '\0') {
*str1 = *str2;
str1++;
str2++;
}
// Ensure the resulting string is null-
terminated
*str1 = '\0';
}
// Driver Code
int main()
{ char str1[50];
char str2[50];
clrscr();
printf("\n\t enter first string =");
gets(str1);
printf("\n\t enter second string =");
gets(str2);
concatenateStrings(str1, str2);
printf("\n\tConcatenated String: %s\n",
str1);
getch();
}
<Output>