[go: up one dir, main page]

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

PPSC Lab Manual

The document contains exercises in C programming covering basic operations, laws of motion, control flow, prime and Armstrong numbers, Floyd's triangle, Pascal's triangle, and matrix multiplication. Each exercise includes source code examples and expected outputs for various tasks such as arithmetic operations, temperature conversion, and implementing a simple calculator. The document serves as a comprehensive guide for beginners to practice and understand fundamental programming concepts in C.
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 views47 pages

PPSC Lab Manual

The document contains exercises in C programming covering basic operations, laws of motion, control flow, prime and Armstrong numbers, Floyd's triangle, Pascal's triangle, and matrix multiplication. Each exercise includes source code examples and expected outputs for various tasks such as arithmetic operations, temperature conversion, and implementing a simple calculator. The document serves as a comprehensive guide for beginners to practice and understand fundamental programming concepts in C.
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/ 47

Exercise - 1 Basics I

1. a) Write a simple program using printf(), scanf()


1. b) Write a C Program to Perform Adding, Subtraction, Multiplication and
Division of two numbers

1. A) Write a simple program using printf(), scanf()

Description:
printf(): The printf() function is used for output. It prints the given statement to the console
scanf():The scanf() function is used for input. It reads the input data from the console.

Source Code:

#include<stdio.h>

int main()

int number;

printf("enter a number:");

scanf("%d",&number);

printf("cube of number is:%d ",number*number*number);

return 0;

Output:

enter a number:5
cube of number is:125
1. B) Write a C Program to Perform Adding, Subtraction, Multiplication
and Division of two numbers

Description: There are five fundamental arithmetic operators supported by C language, which are
addition(+), subtraction(-), multiplication(*), division(/) and modulus(%) of two numbers. All arithmetic
operators compute the result of specific arithmetic operation and returns its result.

Source Code:

/*
* C Program for Addition, Subtraction, Multiplication, Division
* and Modulus of two numbers
*/
#include <stdio.h>
#include <conio.h>
int main(){
/* Variable declation */
int num1,num2;
int sum, difference, product, modulo;
float quotient;
/* Taking input from user and storing it in num1 and num2*/
printf("Enter First Number: ");
scanf("%d",&num1);
printf("Enter Second Number: ");
scanf("%d",&num2);
/* Adding two numbers */
sum = num1 + num2;
/* Subtracting two numbers */
Difference = num1 - num2;
/* Multiplying two numbers*/
product = num1 * num2;
/* Dividing two numbers by typecasting one operand to float*/
quotient = (float)num1 / num2;
/* returns remainder of after an integer division */
modulo = num1 % num2;
printf("\n Addition = %d", sum);
printf("\n Subtraction = %d", difference);
printf("\n Multiplication = %d", product);
printf("\n Division = %.3f", quotient);
printf("\n Modulus = %d", modulo);
getch();
return 0;
}

Output:

Enter First Number: 25


Enter Second Number: 4
Addition = 29
Subtraction = 21
Multiplication = 100
Division = 6.250
Modulus = 1
Exercise - 2 Basics II

2. a) Write a C Program to Simulate 3 Laws at Motion (v=u+at,s=ut+1/2at 2, v2-u2=2as)


2. b) Write a C Program to convert Celsius to Fahrenheit and vice versa

2. A) Write a C Program to Simulate 3 Laws of motion(v=u+at , s=ut+1/2at 2 , v2-u2=2as)

i) v=u+at
SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
int v,u,a,t;
clrscr();
printf("\n enter the initial velocity");
scanf("%d",&u);
printf("\n enter the accelartion");
scanf("%d",&a);
printf("\n enter the time");
scanf("%d",&t);
v=u+(a*t);
printf("\n the final velocity is%d",v);
getch();
}

OUTPUT:
ii) s=ut+1/2at2
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int s,u,a,t;
clrscr();
printf("\n enter the initial velocity");
scanf("%d",&u);
printf("\n enter the accelartion");
scanf("%d",&a);
printf("\n enter the time");
scanf("%d",&t);
s=(u*t)+(0.5*a*t*t);
printf("\n the speed is%d",s);
getch();
}
OUTPUT:
iii) v2-u2=2as
SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int v,u,a,s;
clrscr();
printf("\n enter the initial velocity");
scanf("%d",&u);
printf("\n enter the accelartion");
scanf("%d",&a);
printf("\n enter the speed");
scanf("%d",&s);
v=sqrt((u*u)-(2*a*s));
printf("\n the final velocity is%d",v);
getch();
}

OUTPUT:
2. B) Write a C Program to convert Celsius to Fahrenheit and vice versa

Description:
We are going to perform the c program of temperature conversion from Fahrenheit to Celsius
and vice versa.
We can use the following formulas to do so:
C = (f-32)/1.8
f = 1.8 * C + 32

Source Code:
#include<stdio.h>
void main()
{
floatf,c;
printf("enter the temperature in centigrade\n");
scanf("%f",&c);
f=1.8*c+32;
printf("\n %f centigrade=%f fahrenheat",c,f);
printf("\n enter the temperature in fahrenheat");
scanf("%f",&f);
c=(f-32)/1.8;
printf("\n %f fahrenheat=%f centigrade",f,c);
}

Output:
enter the temperature in centigrade
34.2
34.200001 centigrade=93.559998 fahrenheat
enter the temperature in fahrenheat45.6
45.599998 fahrenheat=7.555555 centigrade
Exercise - 3 Control Flow – I

3 a) Write a C Program to Find Whether the Given Year is a Leap Year or not.
3 b) Write a C Program to Add Digits & Multiplication of a number

3 A) Write a C program to find whether the given year is Leap year or


not.

Source Code:
void main()
{
int year;
printf("Enter a year \n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year \n", year);
else if ((year % 4) == 0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
}

Output:
Enter a year
2012
2012 is a leap year

Enter a year
2009
2009 is not a leap year
3 B) Write a C program to add Digits & Multiplication of a number.

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,z=1;
printf("Enter a number\n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s = s + r; // s is used to calculate sum of the digits of a given number
z = z * r; // t is used to calculate product of the digits of a given number
n = n / 10;
} //closing of while
printf("\n Sum of the digits = %d",s);
printf("\n Product of the digits = %d",z);
getch();
} // closing for main

Output:
Enter a number
234
Sum of the digits = 9
Product of the digits = 24
Exercise - 4 Control Flow – II
4 a) i) Write a C Program to Find Whether the Given Number is Prime Number or Not
ii) Armstrong Number
b) Write a C program to print Floyd Triangle

4 A) i) Write a C program to find whether the given number is Prime number or not

Description:
Prime number is a number that is greater than 1 and divided by 1 or itself. In other words,
prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13,
17, 19, 23....are the prime numbers.
we will take an input from the user and check whether the number is prime or not.

Source Code:
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number :");
scanf("%d", &n);
//logic
for (i = 1; i<= n; i++)
{
if (n % i == 0)
{
c++;
}
}

if (c == 2)
{
printf("Prime number");
}
else
{
printf("Not a Prime number");
}
return 0;
}

Output
Enter any number n: 7
Primenumber

Enter any number :25


Not a Prime number
4. A. ii) Write a C program to find whether the given number is Armstrong number or not .

Description:
Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371
and 407 are the Armstrong numbers.
 Let's try to understand why 153 is an Armstrong number.
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
 Let's try to understand why 371 is an Armstrong number.
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1=371
Source code
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Armstrong number ");
else
printf("not an Armstrong number");
return 0;
}
Output:
enter the number=153
Armstrong number

enter the number=5


not an Armstrong number
4 B) Write a C program to print Floyd’s Triangle.
Description:
The Floyd's triangle is a right-angled triangle that contains consecutive natural numbers. In Floyd's
triangle, the number starts with 1 in the top left corner, and then it consecutive filling the defined rows
through the numbers.
For example: suppose we have defined 5 rows in Floyd's triangle, it generates the following
pattern in increasing order:
1
23
456
7 8 9 10
11 12 13 14 15
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
intnum, i, j, k = 1;
printf( " Enter a number to define the rows in Floyd's triangle: \n");
scanf( "%d", &num);
// use nested for loop
// outer for loop define the rows and check rows condition
for (i = 1; i<= num; i++)
{
// inner loop check j should be less than equal to 1 and print the data.
for (j = 1; j <= i; j++)
{
printf(" %2d", k++); // print the number
}
printf( "\n");
}
getch();
}
Output
Enter a number to define the rows in Floyd's triangle:
6

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Exercise – 5 Control Flow – III
5 a) Write a C Program to print Pascal Triangle
5 b) Write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide Using
Switch…case

5. A) Write a C program to print Pascal Triangle.


Description: We can display the pascal triangle at the center of the screen. For this, just add the
spaces before displaying every row. Generally, on a computer screen, we can display a
maximum of 80 characters horizontally. Half of 80 is 40, so 40th place is the center of the line.

Source Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int row=1; row<=n; row++)
{
int a=1;
for(int s=1; s<=40-row; s++)
printf(" ");
for(int =1; i<=row; i++)
{
printf("%d ",a);
a = a * (row-i)/i;
}
printf("\n");
}
return 0;
}
Output
Source Code:
void main()
{
int a,b,c,ch;
float d;
printf("\nEnter a b values ");
scanf("%d%d",&a,&b);
printf("\n\n1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT");
printf("\nEnter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("\nThe sum is %d",c);
break;
case 2:
c=a-b;
printf("\nThe subtraction is %d",c);
break;
case 3:
printf("\nThe multiplication is %d",c);
break;
case 4:
d=(float)a/b;
printf("\nThe division is %f",d);
break;
case 5: exit(0);
default: printf("\n\tINVALID OPTION\n");
break;
}
}

OUTPUT:
6. a) Write a program in C for multiplication of two square Matrices
,

Source Code:

#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
printf("\n\nMultiplication of two Matrices :\n");
printf("----------------------------------\n");
printf("\nInput the rows and columns of first matrix : ");
scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Mutiplication of Matrix is not possible.");
printf("\nColumn of first matrix and row of second matrix must be
same.");
}
else
{
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}

printf("\nThe Second matrix is :\n");


for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",brr1[i][j]);
}
//multiplication of matrix
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;
for(i=0;i<r1;i++) //row of first matrix
{
for(j=0;j<c2;j++) //column of second matrix
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");
}
Sample output:
Multiplication of two Matrices :
----------------------------------
Input the rows and columns of first matrix : 3
3
Input the rows and columns of second matrix : 3
3
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [0],[2] : 3
element - [1],[0] : 4
element - [1],[1] : 5
element - [1],[2] : 3
element - [2],[0] : 2
element - [2],[1] : 1
element - [2],[2] : 3
Input elements in the second matrix :
element - [0],[0] : 7
element - [0],[1] : 6
element - [0],[2] : 5
element - [1],[0] : 4
element - [1],[1] : 3
element - [1],[2] : 2
element - [2],[0] : 2
element - [2],[1] : 1
element - [2],[2] : 1
The First matrix is :
1 2 3
4 5 3
2 1 3
The Second matrix is :
7 6 5
4 3 2
2 1 1
The multiplication of two matrices is :

21 15 12
54 42 33
24 18 15
6. b) Write a program in C to find transpose of a given matrix

Source Code:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
// printing the transpose
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
OUTPUT:
Enter rows and columns: 2
2
Enter matrix elements:
Enter element a11: 1
Enter element a12: 3
Enter element a21: 5
Enter element a22: 6

Entered matrix:
1 3
5 6
Transpose of the matrix:
1 5
3 6
7. a) Write a C Program demonstrating of parameter passing in
Functions and returning values

Source Code:
#include<stdio.h>
float calculate_area(int);
void main()
{
int radius;
float area;
printf("\nEnter the radius of the circle : ");
scanf("%d",&radius);
area = calculate_area(radius);
printf("\nArea of Circle : %f ",area);
}
float calculate_area(int r)
{
float result;
result = 3.14 * r * r;
}
Sample output:
Enter the radius of the circle 10
Area of Circle:314.000
Are of Circle: 314.0000
7. b) Write a C Program illustrating Fibonacci, Factorial with Recursion
without Recursion

Factorial with recursion:


Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
long int fact=1;
long int factorial(int);
clrscr();
printf("\n Enter n:");
scanf("%d",&n);
printf("\n Factorial is %ld",factorial(n));
getch();
}
long int factorial(int n)
{
if (n<=1)
return 1;
else
return n*factorial(n-1);
}

Output:
Enter n 5
Factorial is 120
Factorial without recursion:
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{ int n;
void factorial(int);
clrscr();
printf("\nEnter n:");
scanf("%d",&n);
factorial(n);
getch();
}
void factorial(int m)
{
int f=1;
while(m>0)
f*=m--;
printf("\nFactorial=%d",f);
}
Output:
Enter n 5

Factorial is 120
Fibonacci with recursion:

Source code:
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
void main()
{
int n, i = 0, c;
clrscr();
printf("\nEnter the value :");
scanf("%d",&n);
printf("\nFibonacci Series using recursion:\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\t", fibonacci(i));
i++;
}
getch();

}
int fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( fibonacci(n-1) + fibonacci(n-2) );
}

OUTPUT:
Enter the value: 10
Fibonacci series using recursion:
0 1 1 2 3 5 8 13 21 34
Fibonacci without recursion:

Source code:

#include<stdio.h>
#include<conio.h>
main()
{
int n, first = 0, second = 1, next, c;
clrscr();
printf("Fibonacci Series\n");
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
getch();
return 0;
}

OUTPUT:
Fibonacci series
Enter the number of terms
10
First 10 terms of Fibonacci series are:-
0 1 1 2 3 5 8 13 21 34
Exercise – 8Functions
a) Write a program in C to add numbers using call by reference.
b) Write a program in C to swap elements using call by reference

8. a) Write a program in C to add numbers using call by reference

#include <stdio.h>

#include <stdlib.h>

int main()

int num1, num2, result;

printf("\nEnter the two number: ");

scanf("%d %d", &num1, &num2);

result = add(&num1, &num2);

printf("\nAddition of %d and %d is %d", num1, num2, result);

int add(int *no1, int *no2)

int res;

res = *no1 + *no2;

return res;

Output:

Enter the two number:

2 4

Addition of 2 and 4 is 6
8.b) Write a program in C to swap elements using call by reference

SOURCE CODE:
#include <stdio.h>
void swap(int * num1, int * num2);
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
printf("Before swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);
/* Pass the addresses of num1 and num2 */
swap(&num1, &num2);
/* Print the swapped values of num1 and num2 */
printf("After swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);
return 0;
}
void swap(int * num1, int * num2)
{
int temp;
temp = *num1;
*num1= *num2;
*num2= temp;
printf("After swapping in swap function n");
printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n\n", *num2);
}
OUTPUT:
Enter two numbers: 10 20
Before swapping in main
Value of num1 = 10
Value of num2 = 20
After swapping in swap function
Value of num1 = 20
Value of num2 = 10
After swapping in main
Value of num1 = 20
Value of num2 = 10
Exercise – 9Arrays and Pointers
a) Write a C Program to Access Elements of an Array Using Pointer
b) Write a C Program to find the sum of numbers with arrays and pointers.

9.a) Write a C Program to Access Elements of an Array Using Pointer

SOURCE CODE
#include<stdio.h>
Void main()
{
int *p,a[5],i;
p=&a[0];
printf("\nEnter the array values: \n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nThe values of array using pointer are: ");
for(i=0;i<5;i++)
{
printf("%d \t",*p);
p++;
}
}
output:
Enter the array values:
12345
The values of array using pointer are: 1 2 3 4 5

9.b) Write a C Program to find the sum of numbers with arrays and
pointers

SOURCE CODE:
#include<stdio.h>
main()
{
int *p,a[5],i,sum=0;;
p=&a[0];
printf("\nEnter the array values: \n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
sum = sum + *p;
p++;
}
printf("\nThe sum of array using pointer is: %d",sum);
}
output:
Enter the array values: 1 2 3 4 5
The sum of array using pointer is: 15

Exercise – 10Strings
a) Implementation of string manipulation operations with library function.
i) copy ii) concatenate iii) length iv) compare
b) Implementation of string manipulation operations without library function.
i) copy ii) concatenate iii) length iv) compare

10.a) Implementation of string manipulation operations with library


function.
i) copy ii) concatenate iii) length iv) compare

SOURCE CODE
AIM: i) Write a C program to copy one string to another string with
library function.
#include<stdio.h>
#include<string.h>
main()
{
char name1[40],name2[20];
printf("\nEnter the name1: ");
scanf("%s",name1);
printf("\nEnter the changed name: ");
scanf("%s",name2);
strcpy(name1,name2);
printf("\nThe name is: %s\n",name1);
}
output:
Enter the name1: nani
Enter the changed name: mothilal
The name is: mothilal
AIM: ii) Write a C program to concatenate one string to another sting
with library
function.
#include<stdio.h>
#include<string.h>
main()
{
char fname[40],lname[20];
printf("\nEnter first name: ");
scanf("%s",fname);
printf("\nEnter the last name: ");
scanf("%s",lname);
strcat(fname,lname);
printf("\nThe full name is: %s\n",fname);
}
output:
Enter first name: mothi
Enter the last name: lal
The full name is: mothilal
AIM: iii) Write a C program to find length of string with library
function.
#include<stdio.h>
#include<string.h>
main()
{
char name[40],n;
printf("\nEnter name: ");
scanf("%s",name);
n=strlen(name);
printf("\nThe length name is: %d\n",n);
}
output:
Enter name: mothilal
The length name is: 8
AIM: iv) Write a C program compare one string to another string with
library function.
#include<stdio.h>
#include<string.h>
main()
{
char name1[40],name2[20];
printf("\nEnter the name1: ");
scanf("%s",name1);
printf("\nEnter the name2: ");
scanf("%s",name2);
if(strcmp(name1,name2)==0)
printf("\nBoth names are equal\n");
else
printf("\nBoth names are NOT equal\n");
}
output 1:
Enter the name1: mothi
Enter the changed name: mothi
Both names are equal
output 2:
Enter the name1: mothi
Enter the name2: Mothi
Both names are NOT equal

b) Implementation of string manipulation operations without library


function.
i) copy ii) concatenate iii) length iv) compare

10.b) Implementation of string manipulation operations without library


function.
i) copy ii) concatenate iii) length iv) compare

AIM: i) Write a C program to copy one string to another string without


library
function.
#include<stdio.h>
#include<string.h>
main()
{
char name1[40],name2[20],i;
printf("\nEnter the name1: ");
scanf("%s",name1);
printf("\nEnter the changed name: ");
scanf("%s",name2);
for(i=0;name2[i]!='\0';i++)
{
name1[i]=name2[i];
}
name1[i]='\0';
printf("\nThe name is: %s\n",name1);
}
output:
Enter the name1: mothilal
Enter the changed name: mothi
The name is: mothi
AIM: ii) Write a C program to concatenate one string to another sting
without library
function.
#include<stdio.h>
#include<string.h>
main()
{
char fname[40],lname[20],i,j;
printf("\nEnter first name: ");
scanf("%s",fname);
printf("\nEnter the last name: ");
scanf("%s",lname);
while(fname[i]!='\0')
i++;
while(lname[j]!='\0')
{
fname[i]=lname[j];
i++;
j++;
}
strcat(fname,lname);
printf("\nThe full name is: %s\n",fname);
}
output:
Enter first name: mothi
Enter the last name: lal
The full name is: mothilal
AIM: iii) Write a C program to find length of string without library
function.
#include<stdio.h>
#include<string.h>
main()
{
char name[40],i=0;
printf("\nEnter name: ");
scanf("%s",name);
while(name[i]!='\0')
i++;
printf("\nThe length name is: %d\n",i);
}
output:
Enter name: mothilal
The length name is: 8
AIM: iv) Write a C program compare one string to another string
without library
function.
#include<stdio.h>
#include<string.h>
main()
{
char name1[20],name2[20],i=0,j=0,flag=0;
printf("\nEnter the name1: ");
scanf("%s",name1);
printf("\nEnter the name2: ");
scanf("%s",name2);
while(name1[i]!='\0')
i++;
while(name2[j]!='\0')
j++;
if(i!=j)
{
printf("\nBoth names are NOT equal\n");
}
else
{
for(i=0;name1[i]!='\0';i++)
{
if(name1[i]==name2[i])
flag=1;
else
flag=0;
}
if(flag==1)
printf("\nBoth names are equal\n");
else
printf("\nBoth names are NOT equal\n");
}
}
output 1:
Enter the name1: mothi
Enter the name2: mothi
Both names are equal
output 2:
Enter the name1: mothi
Enter the name2: mothilal
Both names are NOT equal
Exercises –11 Structures
a) Write a C program to find sum of n elements entered by user. To
perform this program,Allocate memory dynamically using malloc ()
function
b) Write a C program to find sum of n elements entered by user. To
perform this program, allocate memory dynamically using calloc ()
function

11.a Write a C program to find sum of n elements entered by user. To


perform this program, Allocate memory dynamically using malloc ()
function

SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");


scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);
return 0;
}
OUTPUT:
Enter number of elements: 5
Enter elements: 3
2
1
7
5
Sum = 18
11.b Write a C program to find sum of n elements entered by user.
To perform this program, allocate memory dynamically using
calloc () function

SOURCED CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
OUTPUT:
Enter number of elements: 5
Enter elements: 1
5
2
4
2
Sum = 14

You might also like