0% found this document useful (0 votes)
26 views20 pages

C PRACTICAL No Name

The document contains practical programming exercises for BSC (CS) students at Shri Dnyaneshwar Mahavidyalaya Newasa. It includes C programs for various tasks such as swapping numbers using pointers, comparing strings, dynamic memory allocation, checking for palindromes, calculating string length, creating a student structure, and calculating electricity bills. Each exercise provides a solution with code examples and expected outputs.
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)
26 views20 pages

C PRACTICAL No Name

The document contains practical programming exercises for BSC (CS) students at Shri Dnyaneshwar Mahavidyalaya Newasa. It includes C programs for various tasks such as swapping numbers using pointers, comparing strings, dynamic memory allocation, checking for palindromes, calculating string length, creating a student structure, and calculating electricity bills. Each exercise provides a solution with code examples and expected outputs.
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/ 20

Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa


Department Of BSC (CS)
Practical No 1: Date:- / /
Name of Student :-
Seat No.:- Remark :-

Q1)
A)Write a c program to interchange two no using pointer.

Solution:-

#include<stdio.h>
int main()
{
int x, y, *a,*b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before swapping x=%d y=%d",x,y);
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
printf("After swapping x=%d y=%d",x,y);
return 0;
}
OUTPUT:-
B) Derefrencing operation using pointer.

Solution:-

#include<stdio.h>
int main()
{
int x=10;
int *ptr;

ptr=&x;
printf("Value of x: %d\n", *ptr);
return 0;
}
OUTPUT:-
Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa


Department Of BSC (CS)
Practical No 2: Date:- / /
Name of Student :-
Seat No.:- Remark :-

Q.1)
A)Program to Compair two string using pointer without library
function.

Solution:-

#include<stdio.h>
int compare_string(char*,char*);
int main(){
char first[1000], second[1000];
int result;
printf("Input a string\n");
gets(first);
printf("Input a string\n");
gets(second);
result = compare_string(first, second);
if(result == 0)
printf("The strings are same.\n");
else
printf("The string are different.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while (*first == *second)
{
if (*first == '\0' || *second == '\0')
break;
first++;
second++;
}
if(*first == '\0' && *second == '\0')
return 0;
else
return -1;
}
OUTPUT:-
B)Write a c program to find maximum of two number using macro.

Solution:-

#include<stdio.h>
#define MAXI(x,y) ((x>y)? x:y)
int main(){
int a, b, max;
printf("Enter first number:");
scanf("%d", &a);
printf("Enter second number:");
scanf("%d", &b);
max=MAXI(a,b);
printf("Maximum number is :%d\n", max);
return 0;
}
OUTPUT:-
Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa


Department Of BSC (CS)
Practical No 3: Date:- / /
Name of Student :-
Seat No.:- Remark :-

Q.1)
A)Program show dynamic allocation of memory function.
Solution:-
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr;
int n, i, sum=0;
n=5;
printf("Enter number of element:%d\n",n);
ptr=(int*)calloc(n,sizeof(int));
if (ptr == NULL)
{
printf("Memory not allowed.\n");
exit(0);
}
else
{
printf("memory successfulls allocated using calloc:\n");
for (i=0;i<n;++i)
{
ptr[i]=i+1;
}
printf("The elements of the array are:");
for(i=0;i<n;i++)
{
printf("%d",ptr[i]);
}
}
return 0;
}
OUTPUT:-
Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa


Department Of BSC (CS)
Practical No 4: Date:- / /
Name of Student :-
Seat No.:- Remark :-

Q.1)
A) Program using pointer to check given string is palindrome or
not.
Solution:-
#include<stdio.h>
void is Palindrome(char* string)
{
char *ptr, *rev;
ptr = string;
while(*ptr != '\0')
{
++ptr;
}
--ptr;
for (rev = string; ptr>=rev;)
{
if (*ptr == *rev)
{
--ptr;
rev++;
}
else
break;
}
if (rev>ptr)
printf("string is Palindrome");
else
printf("string is not Palindrome");
}
int main()
{
char str[1000];
printf("Enter any string:");
gets(str);
isPalindrome(str);
return 0;
}
OUTPUT:-
B)Write a c program calculate length of string strlen () function.

Solution:-

#include<stdio.h>
#include<string.h>
int main()
{
char a[20]="Program";
printf("length of string a=%d", strlen(a));
return 0;
}
OUTPUT:-
Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa


Department Of BSC (CS)
Practical No 5: Date:- / /
Name of Student :-
Seat No.:- Remark :-

Q.1)
A)Write a c program to create student structure having fields roll no
, name accept details of 1 student & write a function to display the
details.

Solution:-

#include<stdio.h>
struct student{
int roll_no;
char name[50];
};
void displayStudent(struct student s)
{
printf("Roll No:%d\n",s.roll_no);
printf("Name:%s\n",s.name);
}
int main()
{
struct student s;

printf("Enter Roll No:");


scanf("%d",&s.roll_no);
printf("Enter Name:");
scanf("%s",&s.name);

printf("\nDetails of the student:\n");


displayStudent(s);
return 0;
}
OUTPUT:-
B)Write a c program to calculate electricity bill using function.

Solution:-

#include<stdio.h>
float calculate_bill(float units){
float bill=0;
if(units<=100){
bill = units*0.50;
}else if (units<=200){
bill=100*0.50+(units-100)*0.75;
}else if (units <=300){
bill=100*0.50+100*0.75+(units-200)*1.20;
}else{
bill=100*0.50+100*0.75+100*1.20+(units-300)*1.50;
}
return bill;
}
int main(){
int units;
float bill;
printf("Enter the number of units consumed:");
scanf("%d",&units);
bill = calculate_bill(units);
printf("Total electricity bill:%.2f\n",bill);
return 0;
}
OUTPUT:-

You might also like