0% found this document useful (0 votes)
5 views54 pages

C Programming Exercises for Students

The document contains multiple C programming exercises from Panimalar Engineering College, including programs for converting Celsius to Fahrenheit, calculating simple interest, printing Fibonacci series, finding GCD, checking for vowels, and more. Each program includes sample input and output to demonstrate functionality. The document serves as a practical guide for students learning programming concepts and syntax 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)
5 views54 pages

C Programming Exercises for Students

The document contains multiple C programming exercises from Panimalar Engineering College, including programs for converting Celsius to Fahrenheit, calculating simple interest, printing Fibonacci series, finding GCD, checking for vowels, and more. Each program includes sample input and output to demonstrate functionality. The document serves as a practical guide for students learning programming concepts and syntax 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

Ex.

No:1(A)
Program to convert Celsius to Fahrenheit

PROGRAM:

#include<conio.h>
void main()
{
float cel,fahren;
clrscr();
printf("\n Enter the Temparature in Celsius : ");
scanf("%f",&cel);
fahrenheit = (1.8 * cel) + 32;
printf("\n Temperature in Fahrenheit : %f ",fahren);
getch();
}

Sample Input and Output:

Input:

Enter the Temperature in Celsius : 31


Output :
Temperature in Fahrenheit : 87.800003

RESULT:

PANIMALAR ENGINEERING COLLEGE


Ex No.: 1 (B)
Program to print the address and size of the data & data type

PROGRAM:

#include <stdio.h>
int main(void)
{
int x=62;
char a='b';
float p=8.5,q=3.32;
// Print Address
printf("%c is Stored at Address %u\n",a,&a);
printf("%d is Stored at Address %u\n",x,&x);
printf("%f is Stored at Address %u\n",p,&p);
printf("%f is Stored at Address %u\n",q,&q);
// Print Size
printf("\n size of short: %d", sizeof(short));
printf("\n size of int: %d", sizeof(int));
printf("\n size of long int: %d", sizeof(long));
return 0;
}

Sample Input and Output:

b is Stored at Address 65523


62 is Stored at Address 65524
8.500000 is Stored at Address 65518
3.320000 is Stored at Address 65514
Size of short: 2
Size of int: 2
Size of long int: 4

RESULT:

PANIMALAR ENGINEERING COLLEGE


Ex No.: 1 (C)
Program to perform simple interest calculation

PROGRAM :

#include<stdio.h>
int main()
{
float amount, rate, time, si;
printf("\nEnter Principal Amount : ");
scanf("%f", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%f", &rate);
printf("\nEnter Period of Time : ");
scanf("%f", &time);
si = (amount * rate * time) / 100;
printf("\nSimple Interest : %f", si);
return(0);
}

Sample Input and Output:

Input
Enter Principal Amount : 10000
Enter Rate of Interest : 5
Enter Period of Time : 4
Output
Si = 2000

RESULT:

PANIMALAR ENGINEERING COLLEGE


Ex. No: 2(A)
Program to print first n terms of the Fibonacci series

PROGRAM :

#include <stdio.h>
int main()
{
int a, b, c, i, n;
/* Reads a number from user */
printf("Enter value of n to print Fibonacci series : ");
scanf("%d", &n);
a = 0; / * Initialize the value * /
b = 1;
c = 0; / * Sum the value * /
for(i=1; i<=n; i++)
{
printf("%d, ", c);
a=b;
b=c;
c=a+b;
}
return 0;
}

Sample input and output:

Input :
Enter value of n to print Fibonacci series : 100
Output:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 2(B)
Program to find GCD(HCF) of any two values

PROGRAM :

#include <stdio.h>
int main()
{
int i, num1, num2, min, hcf=1;
/* Reads two numbers from user */
printf("Enter any two numbers to find HCF: ");
scanf("%d %d", &num1, &num2);
min = (num1<num2) ? num1 : num2; // Conditional Operator
for(i=1; i<=min; i++)
{
/* If i is factor of both number */
if(num1 % i==0 && num2 % i==0)
{
hcf = i;
}
}
printf("HCF of %d and %d = %d\n", num1, num2, hcf);
return 0;
}

Sample input and output:

Input :
Enter any two numbers to find HCF: 12 30
Output:
HCF of 12 and 30 = 6

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 2(C)

Program to check an alphabet vowel or consonant

PROGRAM :

#include <stdio.h>
int main()
{
char ch; // declare a variable to get any character .
printf("Enter any character: ");
scanf("%c", &ch); /* Reads a character from user */
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' ||
ch=='U')
{
printf("%c is VOWEL.\n", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("%c is CONSONANT.\n", ch);
}
return 0;
}

Sample input and output :

Input :
Enter any character : a
Output:
a is VOWEL.

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 2(D)
Program to print floyd’s triangle

PROGRAM :

#include <stdio.h>
int main()
{
int rows, a, b, number = 1;
printf("Number of rows of Floyd's triangle to print:");
scanf("%d",&rows);
printf("FLOYD'S TRIANGLE\n\n");
for ( a = 1 ; a <= rows ; a++ )
{
for ( b = 1 ; b <= a ; b++ )
{
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}

Sample input and output:


Input :
Number of rows of Floyd's triangle to print:5
Output:
FLOYD'S TRIANGLE
1
23
456
7 8 9 10
11 12 13 14 15
RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 3

Program to check given year is leap year or not

PROGRAM :

#include <stdio.h>
#include <conio.h>
void main()
{
int year;
clrscr();
printf("Enter the Year (YYYY) : ");
scanf("%d",&year);
if(year%4==0 && year%100!=0 || year%400==0)
printf("\nThe Given year %d is a Leap Year");
else
printf("\nThe Given year %d is Not a Leap Year");
getch();
}

Sample Input and Output :

Input :
Enter the Year (YYYY) : 1200
Output :
The Given year %d is a Leap Year

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 4
Program to perform Calculator operations

PROGRAM :

#include <stdio.h>
float add(float num1, float num2);
float sub(float num1, float num2);
float mult(float num1, float num2);
float div(float num1, float num2);
float squa(float num1, float num2);
int main()
{
char op;
float num1, num2, result=0.0f;
printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number 2]\n");
/* Input two number and operator from user */
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{
case '+':
result = add(num1, num2); break;
case '-':
result = sub(num1, num2); break;
case '*':
result = mult(num1, num2); break;
case '/':
result = div(num1, num2); break;
case 's':
result = squa(num1, num2); break;
default:
printf("Invalid operator");
}
/* Print the result */
printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
return 0;
}
float add(float num1, float num2)
{
return num1 + num2;
}

PANIMALAR ENGINEERING COLLEGE


float sub(float num1, float num2)
{
return num1 - num2;
}

float mult(float num1, float num2)


{
return num1 * num2;
}
float div(float num1, float num2)
{
return num1 / num2;
}
float squa(float num1, float num2)
{
return num1 * num2;
}

Sample Input and Output:

Choose operation to perform (+,-,*,/,): +


Enter first number: 3
Enter second number: 4
Result: 3 + 4 = 7.000000
Choose operation to perform (+,-,*,/,): -
Enter first number: 4
Enter second number: 5
Result: 4 - 5 = -1.000000
Choose operation to perform (+,-,*,/,): *
Enter first number: 5
Enter second number: 6
Result: 5 * 6 = 30.000000
Choose operation to perform (+,-,*,/,): /
Enter first number: 66
Enter second number: 6
Result: 66 / 6 = 11.000000

RESULT :

PANIMALAR ENGINEERING COLLEGE


[Link].: 5

Check the given number is Armstrong number or not

Program:
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while (num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}

Sample input and output:

Input :
Enter a number: 153
Output:
153 is an Armstrong number

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 6

Find sum of weights based on the given conditions

PROGRAM:

#include<stdio.h>
#include<math.h>
#include<conio.h>
int getWeight(int n)
{
int w=0;
float root=sqrt(n);
if(root==ceil(root))
w+=5;
if(n%4==0&&n%6==0)
w+=4;
if(n%2==0)
w+=3;
return w;
}
void main()
{
int nums[15];
int ws[15];
int i,j,t,n;
printf("Enter number of numbers : ");
scanf("%d",&n);
printf("\nEnter numbers : ");
for(i=0;i<n;i++)
scanf("%d",&nums[i]);

for(i=0;i<n;i++)
ws[i]=getWeight(nums[i]);
printf("\nBefore sorting:\n");
for(i=0;i<n;i++)
printf("%d:%d\t",nums[i],ws[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(ws[j]>ws[j+1])
{
t=ws[j+1];

PANIMALAR ENGINEERING COLLEGE


ws[j+1]=ws[j];
ws[j]=t;
t=nums[j+1];
nums[j+1]=nums[j];
nums[j]=t;
}
printf("\nSorted:\n");
for(i=0;i<n;i++)
printf("%d:%d\t",nums[i],ws[i]);
getch();
}

Sample input and output:


Input :
Enter number of numbers : 6
Enter numbers :
10
36
54
89
12
27
Output :

Before Sorting
10:3 36:12 54:3 89:0 12:7 27:0
Sorted:
89:0 27:0 10:3 54:3 12:7 36:12

RESULT :

PANIMALAR ENGINEERING COLLEGE


[Link].: 7

Populate an array with height of persons and find how many persons
are above the average height

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
void readArray(int [] , int );
double findAverage(int [] , int);
int countAboveAverage(int [] , int , double);
double average;
int count;
int main()
{
int arr[SIZE];
int n;
printf("Enter a value for n: ");
scanf("%d", &n);
readArray(arr, n);
average = findAverage(arr, n);
count = countAboveAverage(arr, n, average);
printf("The number of persons above the average height is %d:",count);
return 0;
}
void readArray(int arr[], int n)
{
int i;
for(i=0; i<n; i++)
{
printf("Please enter #%d: ", i+1);
scanf("%d", arr + i); /* or &arr[i] */
}
}
double findAverage(int arr[], int n)
{
int sum = 0;
int i;
for(i=0; i<n; i++)
sum+=arr[i];
return (double)sum / n;
}

PANIMALAR ENGINEERING COLLEGE


int countAboveAverage(int arr[], int n, double average)
{
int count=0;
int i;
for(i=0; i<n; i++)
count = arr[i] > average ? count+1: count;
return count;
}

Sample Input and output :

Input
Enter a value for n: 2
Please enter #1: 23
Please enter #2: 45

Output
The number of persons above the average height is 1

RESULT :

PANIMALAR ENGINEERING COLLEGE


[Link].: 8
Populate a two dimensional array with height and weight of persons and
compute the body mass index of the individuals
PROGRAM:

#include<stdio.h>
#include<conio.h>
#define feettomet 0.308
void main()
{
float a[10][10],bmi;
float ftom;
int n,i,j;
clrscr();
printf("enter the number o person\n");
scanf("%f",&n);
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
{
scanf("%f",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
j=0;
bmi=0.00;
ftom=0.00;
ftom=a[i][j+1]*feettomet;
bmi=a[i][j]/(ftom*ftom);
printf("%.2f %.2f %.2f\n",a[i][j],a[i][j+1],bmi);
}
}
Sample Input and Output:
Input
Enter upper limit:2
Enter weight and height: 53 5.5 65 5.5
Output
53 5.5 BMI: 18
65 5.5 BMI: 23.1
RESULT :

PANIMALAR ENGINEERING COLLEGE


[Link].: 9

Program to reverse a string without changing the position of special characters

PROGRAM :

#include<stdio.h>
#include<string.h>
void reverse(char str[10])
{
int r,l;
char c;
r= strlen(str)-1;
l=0;
while(l<r)
{
if(!(str[l]>='a' && str[l]<='z') || (str[l]>='A' && str[l]<='Z'))
l++;
else if(!(str[r]>='a' && str[r]<='z') || (str[r]>='A' && str[r]<='Z'))
r--;
else
{
c=str[l];
str[l]=str[r];
str[r]=c;
l++;
r--;
}
}
printf("%s",str);
}
int main()
{
// char str1[10]="g@vat";
char str1[10];
printf("Enter the String to be Reverse : \n");
scanf(“%c”, str1);
printf("Reverse String is : \n");
reverse(str1);
return 0;
}

PANIMALAR ENGINEERING COLLEGE


Sample Input and Output :

Reverse String is :
tav@g

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 10

C program to convert decimal numbers to binary numbers

PROGRAM :

#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<string.h>
long decimalToBinary(long n);
void octal_hex(int n, char hex[]); int hex_octal(char hex[]);
int main()
{
long decimal;
char hex[20],c; int n;
printf("Enter a decimal number\n");
scanf("%ld", &decimal);
printf("Binary number of %ld is %ld", decimal,
decimalToBinary(decimal));
printf("Enter octal number: ");
scanf("%d",&n);
octal_hex(n,hex);
printf("Hexadecimal number:%s",hex);
getch();
return 0;
}
/* Function to convert a decinal number to binary number */
long decimalToBinary(long n)
{
int remainder;
long binary = 0, i = 1;
while(n != 0)
{
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary; }
void octal_hex(int n, char hex[])
{
int i=0,decimal=0, rem;
while (n!=0)

PANIMALAR ENGINEERING COLLEGE


{
rem = n%10;
n=n/10;
decimal=decimal+rem*pow(8,i);
++i;
} i=0;
while (decimal!=0)
{
rem=decimal%16;
switch(rem)
{
case 10:
hex[i]='A'; break;
case 11:
hex[i]='B'; break;
case 12:
hex[i]='C'; break;
case 13:
hex[i]='D'; break;
case 14:
hex[i]='E'; break;
case 15:
hex[i]='F'; break;
default:
hex[i]=rem+'0'; break;
}
++i;
decimal/=16;
}
hex[i]='\0';
strrev(hex); /* Function to reverse string. */
}
Sample Input and Output :
Input:
Enter a decimal number 25
Output:
Binary number of 25 is 11001
Enter octal number:127
Hexadecimal number:57

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 11 (A)

Program to find the total number of words

PROGRAM :

#include <stdio.h>
#include <conio.h>
void main()
{
char a[100];
int len,i,word=1;
clrscr();
printf("\nENTER A STRING: ");
gets(a);
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]!=' ' && a[i+1]==' ')
word=word+1;
}
printf("\nTHERE ARE %d WORDS IN THE STRING",word);
getch();
}

Sample Input And Output:

Input
ENTER A STRING abc abcd abcde abc

Output
THERE ARE 4 WORDS IN THE STRING

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 11 (B)
Program for Capitalize the first word of each sentence

PROGRAM

#include <stdio.h>
#include <conio.h>
void main()
{
char s[100];
int len,i;
clrscr();
printf("\nENTER A SENTENSE: ");
gets(s);
len=strlen(s);
printf("\n");
for(i=0;i<len;i++)
{
if((i==0 && s[i]>=97 && s[i]<=122) || (s[i-1]==32 && s[i]>=97 && s[i]<=122))
printf("%c",s[i]-32);
else
{
if(i!=0 && s[i-1]!=32 && s[i]>=65 && s[i]<=90)
printf("%c",s[i]+32);
else
printf("%c",s[i]);
}
}
getch();
}

Sample Input and Output :

Input
ENTER A SENTENSE c programming
Output
C Programming

RESULT :

PANIMALAR ENGINEERING COLLEGE


[Link].: 12
Tower of Hanoi using Recursion

PROGRAM :

#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

Sample Input and Output:


Input :
Enter the number of disks : 3
Output :
The sequence of moves involved in the Tower of Hanoi are :
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 13
Program to sort the list of numbers using pass by reference

PROGRAM:
#include<stdio.h>
#include<conio.h>
void sort(int m, int x[]);
void main()
{
int i,n;
int num[10];
printf("Enter upper limit value:\n");
scanf("%d",&n);
printf("Enter the values to sort\n");
for(i=0;i<n;i++)
scanf("%d",&num[i]);
sort(n,&num[0]);
printf("after sorting:\n");
for(i=0;i<n;i++)
printf("%4d",num[i]);
printf("\n");
getch();
}
void sort(int m,int x[])
{
int i,j,t;
for(i=1;i<=m-1;i++)
for(j=1;j<=m-i;j++)
if(x[j-1]>=x[j])
{
t=x[j-1];
x[j-1]=x[j];
x[j]=t;
}
}

PANIMALAR ENGINEERING COLLEGE


Sample Input and Output:

Input
Enter the limit value:
5
Enter the values to sort
45 6 9 100 4

Output
After sorting:
4 6 9 45 100

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 14

Program to generate salary slip of employees using structures and pointers

PROGRAM

#include<stdio.h>
#include<dos.h>
struct employee
{
int NO;
char NAME[10];
int DESIGN_CODE;
int DAYS_WORKED;
}EMP[12]={
{1,"GANESH",1,25},
{2,"MAHESH",1,30},
{3,"SURESH",2,28},
{4,"KALPESH",2,26},
{5,"RAHUL",2,24},
{6,"SUBBU",2,25},
{7,"RAKESH",2,23},
{8,"ATUL",2,22},
{9,"DHARMESH",3,26},
{10,"AJAY",3,26},
{11,"ABDUL",3,27},
{12,"RASHMI",4,29}
};
void main()
{
int EMPNO;
void gen_payslip(int);
clrscr();
printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");
scanf("%d",&EMPNO);
if(EMPNO>0 && EMPNO<13)
gen_payslip(EMPNO);
else
printf("\nYOU HAVE ENTERED WRONG EMP NO. !!");
getch();
}
void gen_payslip(int EMPNO)
{
struct date D;

PANIMALAR ENGINEERING COLLEGE


char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\n\t\t\tPANIMALAR ENGINEERING COLLEGE ");
printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
printf("\n\n\[Link].: %d\t\[Link]: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1: PAYRATE=400;
printf("\tDESIGNATION: CLERK");
break;
case 2: PAYRATE=300;
printf("\tDESIGNATION: SALESMEN");
break;
case 3: PAYRATE=250;
printf("\tDESIGNATION: HELPER");
break;
case 4: PAYRATE=350;
printf("\tDESIGNATION: [Link]");
break;
}
BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
PF=BASIC/10;
printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\t\tPAY RATE: %.0f\t\[Link]:%d/%d/%d
",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\t_____________________________________________________");
printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
printf("\n\t_____________________________________________________");
printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
printf("\n\n\t\t\t\t\t\[Link]\t%.0f",PTAX);
printf("\n\n\t___________________________________________________");
printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
printf("\n\t_____________________________________________________");
}

PANIMALAR ENGINEERING COLLEGE


Output

ENTER THE EMPLOYEE NO TO GENERATE PYSLIP : 1


PANIMALAR ENGINEERING COLLEGE
SALARY MONTH 12 2018
EMP NO. 1 [Link]: GANESH DESIGNATION:CLERK
DAYS WORKED : 25 PAY RATE : 400 GEN. DATE:15/9/2013
EARNING AMOUNT (RS.) DEDUCTIONS AMOUNT(RS.)

BASIC PAY 1000 P.F 1000

[Link] 200
GROSS EARN. 10000 TOTAL DEDUCTION. 1200

NET PAY 8800

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].: 15(A)

Pointer Demonstration the Use Of & And *

PROGRAM:

#include <stdio.h>
int main()
{
/* Pointer of integer type, this can hold the
* address of a integer type variable.
*/
int *p;
int var = 10;
/* Assigning the address of variable var to the pointer
* p. The p can hold the address of var because var is
* an integer type variable.
*/
p= &var;
printf("Value of variable var is: %d", var);
printf("\nValue of variable var is: %d", *p);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p);
printf("\nAddress of pointer p is: %p", &p);
return 0;
}

Output:
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50

[Link] Elements of an Array Using Pointer

PROGRAM:

#include <stdio.h>
int main() {
int data[5];
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)

PANIMALAR ENGINEERING COLLEGE


scanf("%d", data + i);
printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}

Output:
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4

[Link] to find Length of the string using pointer

PROGRAM:

#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main() {
char str[20];
int length;
clrscr();
printf("\nEnter any string : ");
gets(str);
length = string_ln(str);
printf("The length of the given string %s is : %d", str, length);
getch();
}
int string_ln(char*p) /* p=&str[0] */
{
int count = 0;
while (*p != '\0') {

PANIMALAR ENGINEERING COLLEGE


count++;
p++;
}
return count;
}

Output :
Enter the String : pritesh
Length of the given string pritesh is : 7

Program to Concatenation of String using Pointers

PROGRAM:

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
char * s1 = str1;
char * s2 = str2;
// Inputting 2 strings from user
printf("Enter 1st string: ");
gets(str1);
printf("Enter 2nd string: ");
gets(str2);
// Moving till the end of str1
while(*(++s1));
// Coping str2 to str1
while(*(s1++) = *(s2++));
printf("Concatenated string: %s", str1);
return 0;
}

Output
Enter 1st string: Information
Enter 2nd string: Technology
Concatenated string : Information Technology

PANIMALAR ENGINEERING COLLEGE


Program to Compare two string using pointers

PROGRAM:

#include<stdio.h>
int main()
{
char string1[50],string2[50],*str1,*str2;
int i,equal = 0;
printf("Enter The First String: ");
scanf("%s",string1);
printf("Enter The Second String: ");
scanf("%s",string2);
str1 = string1;
str2 = string2;
while(*str1 == *str2)
{
if ( *str1 == '\0' || *str2 == '\0' )
break;
str1++;
str2++;
}
if( *str1 == '\0' && *str2 == '\0' )
printf("\n\nBoth Strings Are Equal.");
else
printf("\n\nBoth Strings Are Not Equal.");
}

Output:-
Enter the first string: Panimalar
enter the second string: Panimalar
Both string are Equal

[Link] Number of Words, Digits, Vowels Using Pointers

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
/*low implies that position of pointer is within a word*/
#define low 1

PANIMALAR ENGINEERING COLLEGE


/*high implies that position of pointer is out of word.*/
#define high 0
void main() {
int nob, now, nod, nov, nos, pos = high;
char *str;
nob = now = nod = nov = nos = 0;
clrscr();
printf("Enter any string : ");
gets(str);
while (*str != '\0') {
if (*str == ' ') {
// counting number of blank spaces.
pos = high;
++nob;
} else if (pos == high) {
// counting number of words.
pos = low;
++now;
}
if (isdigit(*str)) /* counting number of digits. */
++nod;
if (isalpha(*str)) /* counting number of vowels */
switch (*str) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++nov;
break;
}
/* counting number of special characters */
if (!isdigit(*str) && !isalpha(*str))
++nos;
str++;
}
printf("\nNumber of words %d", now);
printf("\nNumber of spaces %d", nob);
printf("\nNumber of vowels %d", nov);

PANIMALAR ENGINEERING COLLEGE


printf("\nNumber of digits %d", nod);
printf("\nNumber of special characters %d", nos);
getch();
}
Output:
Enter any string : pritesh a taral from 2 IT C $
Number of words 7
Number of spaces 7
Number of vowels 6
Number of digits 1
Number of special characters 1

[Link] Two Matrices Using Multidimensional Arrays with Pointers

PROGRAM:

#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", (*(a+i)+j);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", (*(b+i)+j);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
*(*(sum+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);

PANIMALAR ENGINEERING COLLEGE


}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", *(*(sum+i)+j));
if (j == c - 1)
{
printf("\n\n");
}
}
return 0;
}

Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
Enter elements of 1st matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3
Sum of two matrices:
-2 8 7
10 8 6

15.F. Multiply Two Matrices Using Pointers

PROGRAM:

#include <stdio.h>
#define ROW 3
#define COL 3

PANIMALAR ENGINEERING COLLEGE


/* Function declarations */
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL]);
int main() {
int mat1[ROW][COL];
int mat2[ROW][COL];
int product[ROW][COL];
printf("Enter elements in first matrix of size %dx%d\n", ROW, COL);
matrixInput(mat1);
printf("Enter elements in second matrix of size %dx%d\n", ROW, COL);
matrixInput(mat2);
matrixMultiply(mat1, mat2, product);
printf("Product of both matrices is : \n");
matrixPrint(product);
return 0;
}
void matrixInput(int mat[][COL]) {
int row, col;
for (row = 0; row < ROW; row++) {
for (col = 0; col < COL; col++) {
scanf("%d", (*(mat + row) + col));
}
}
}
void matrixPrint(int mat[][COL]) {
int row, col;
for (row = 0; row < ROW; row++) {
for (col = 0; col < COL; col++) {
printf("%d ", *(*(mat + row) + col));
}
printf("\n");
}
}
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL]) {
int row, col, i;
int sum;
for (row = 0; row < ROW; row++) {
for (col = 0; col < COL; col++) {
sum = 0;
for (i = 0; i < COL; i++) {
sum += (*(*(mat1 + row) + i)) * (*(*(mat2 + i) + col));
}
*(*(res + row) + col) = sum;

PANIMALAR ENGINEERING COLLEGE


}
}
}

Output
Enter elements in first matrix of size 3x3
231
256
268

Enter elements in second matrix of size 3x3


121
234
567
Product of both matrices is :
13 19 21
42 55 64
54 70 82

[Link] Two Numbers Using Function Pointers

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
int multiplyNum(int , int); //function declaration or prototype
int(*fptr)(int,int);
int main()
{
int num1,num2,product; //variable declaration
fptr=multiplyNum;
printf("Enter the two number ");
scanf("%d %d",&num1,&num2);//taking two number as input from user
product=multiplyNum(num1,num2);//calling the function
//The product value (returned by the function) is stored in product variable.
printf("The product of these numbers :%d",product);
product=(*fptr)(num1,num2);
printf(“The products:%d”, product);
//display the product value
getch();
return 0;
}
int multiplyNum(int a, int b)//defining function based in declaration
{

PANIMALAR ENGINEERING COLLEGE


int result=a*b;//find product of two numbers
//and result stored in result variable
return result;//returning result
}

OUTPUT
Enter the two numbers 12
23
The product of these numbers :276

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].:16

Compute internal marks of students for five different subjects


using structures and functions

PROGRAM:
#include<stdio.h>
struct student
{
char name[20];
int rollno;
int m1,m2,m3,m4,m5,total,intern;
};
void total(struct student s[3])
{
int i=0;
for(i=0;i<3;i++)
{
s[i].total=s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5;
}
for(i=0;i<3;i++)
{
s[i].intern=((s[i].total)/5)*0.20;
}
for(i=0;i<3;i++)
{
printf(" %d\t %d",s[i].total,s[i].intern);
}
}
int main( )
{
int i,n;
struct student s[3];
printf("Enter name, roll no. and marks Below :: \n");
for(i=0; i<3; i++)
{
printf("\nEnter %d record :: \n",i+1);
printf("Enter Name :: ");
scanf("%s",s[i].name);
printf("Enter RollNo. :: ");
scanf("%d",&s[i].rollno);
printf("Enter marks");
scanf("%d%d%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3,&s[i].m4,&s[i].m5);
}

PANIMALAR ENGINEERING COLLEGE


printf("\n\tName\tRollNo\tMarks\t\Internals\n");
for(i=0; i<3; i++)
printf("\t%s\t%d\t\n", s[i].name, s[i].rollno);
total(s);
return 0;
}

Sample input and output :

Output:
Enter name, roll no. and marks Below ::
Enter 1 record ::
Enter Name :: ABC
Enter RollNo. :: 12
Ebter marks 100 100 100 100 100
Enter 2 record ::
Enter Name :: DEF
Enter RollNo. :: 13
Ebter marks 50 50 50 50 50
Enter 3 record ::
Enter Name :: HIJ
Enter RollNo. :: 80
Enter marks 80 80 80 80 80
Name RollNo Marks Internals
ABC 12 500 20
DEF 13 250 10
HIJ 80 400 16

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].:17

Program to demonstrate the difference between unions and structures

PROGRAM:
#include <stdio.h>
#include <string.h>
// declaring structure
struct struct_example
{
int integer;
float decimal;
char name[20];
};
// declaring union
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
// creating variable for structure and initializing values difference six
struct struct_example stru ={5, 15, "John"};
// creating variable for union and initializing values
union union_example uni = {5, 15, "John"};
printf("data of structure:\n integer: %d\n decimal: %.2f\n name: %s\n", [Link],
[Link], [Link]);
printf("\ndata of union:\n integer: %d\n" "decimal: %.2f\n name: %s\n", [Link],
[Link], [Link]);
// difference five
printf("\nAccessing all members at a time:");
[Link] = 163;
[Link] = 75;
strcpy([Link], "John");
printf("\ndata of structure:\n integer: %d\n " "decimal: %f\n name: %s\n", [Link],
[Link], [Link]);
[Link] = 163;
[Link] = 75;
strcpy([Link], "John");
printf("\ndata of union:\n integeer: %d\n " "decimal: %f\n name: %s\n", [Link],
[Link], [Link]);
printf("\nAccessing one member at a time:");

PANIMALAR ENGINEERING COLLEGE


printf("\ndata of structure:");
[Link] = 140;
[Link] = 150;
strcpy([Link], "Mike");
printf("\ninteger: %d", [Link]);
printf("\ndecimal: %f", [Link]);
printf("\nname: %s", [Link]);
printf("\ndata of union:");
[Link] = 140;
[Link] = 150;
strcpy([Link], "Mike");
printf("\ninteger: %d", [Link]);
printf("\ndecimal: %f", [Link]);
printf("\nname: %s", [Link]);
//difference four
printf("\nAltering a member value:\n");
[Link] = 512;
printf("data of structure:\n integer: %d\n decimal: %.2f\n name: %s\n", [Link],
[Link], [Link]);
[Link] = 512;
printf("data of union:\n integer: %d\n decimal: %.2f\n name: %s\n", [Link],
[Link], [Link]);
// difference two and three
printf("\nsizeof structure: %d\n", sizeof(stru));
printf("sizeof union: %d\n", sizeof(uni));
}
Output:
data of structure:
integer: 5
decimal: 15.00
name: John
data of union:
integer: 5
decimal: 0.00
name:
Accessing all members at a time:
data of structure:
integer: 163
decimal: 75.000000
name: John
data of union:
integer: 1852337994
decimal: 17983765624912253034071851008.000000
name: John

PANIMALAR ENGINEERING COLLEGE


Accessing one member at a time:
data of structure:
integer: 140
decimal: 150.000000
name: Mike
data of union:
integer: 1701538125
decimal: 69481161252302940536832.000000
name: Mike
Altering a member value:
data of structure:
integer: 512
decimal: 150.00
name: Mike
data of union:
integer: 512
decimal: 0.00
name:
sizeof structure: 28
sizeof union: 20

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].:18

Program to create address book and to locate employee


details using random access file

PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct employee
{
int empid;
char name[20];
char desig[20];
};
void main()
{
struct employee emp1, emp2;
FILE *fd1, *fd2;
char str[20];
int id, eno, pos, size;
fd1 = fopen("[Link]", "a");
fseek(fd1, 0, 2);
size = ftell(fd1);
id = 100 + size / sizeof(emp1);
printf("\n Enter employee details \n");
while(1)
{
[Link] = id++;
printf("\n Employee Id : %d", [Link]);
printf("\n Enter Name (\"xxx\" to quit) : "); scanf("%s",[Link]);
if (strcmp([Link],"xxx") == 0)
break;
printf("Enter Designation : ");
scanf("%s", [Link]);
fwrite (&emp1, sizeof(emp1), 1, fd1);
}
size = ftell(fd1);
fclose(fd1);
fd2 = fopen("[Link]", "r");
printf("\n Enter Employee id : ");
scanf("%d", &eno);
pos = (eno - 100) * sizeof(emp2);

PANIMALAR ENGINEERING COLLEGE


if (pos < size)
{
fseek(fd2, pos, 0);
fread(&emp2, sizeof(emp2), 1, fd2);
printf("Employee Name : %s\n", [Link]);
printf("Designation: %s\n", [Link]);
}
else
printf("\n Incorrect Employee Id \n"); fclose(fd2);
}

Sample Input and Output:

Enter employee details

Employee Id : 101 Enter Name ("xxx" to quit) : arun


Enter Designation : ASP
Employee Id : 102 Enter Name ("xxx" to quit) : Raju
Enter Designation : Prof
Employee Id : 103 Enter Name ("xxx" to quit) : xxx
Enter Employee id : 102
Employee Name : Raju
Designation: Prof

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].:19

Program to count a number of account holders whose balance is


less than the minimum balance using sequential access file

Program:
#include<stdio.h>
#include<conio.h>
void creation();
void deposit();
void withdraw();
void lowbal();
int a=0,i = 1001;
struct bank
{
int no;
char name[20];
float bal;
float dep;
}s[100];
int main()
{
int ch;
do
{
printf("\n*********************");
printf("\n BANKING ");
printf("\n*********************");
printf("\n1. Create New Account");
printf("\n2. Cash Deposit ");
printf("\n3. Cash Withdraw");
printf("\n4. Low Balance Enquiry");
printf("\n5. Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: creation();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: lowbal();

PANIMALAR ENGINEERING COLLEGE


break;
case 5:
break;
defalut: printf("Choice a Valid option !!");
getch();
}
}while(ch!=5);
}
void creation()
{
printf("\n*************************************");
printf("\n NEW ACCOUNT CREATION ");
printf("\n*************************************");
printf("\nYour Account Number is :%d",i);
s[a].no = i;
printf("\nEnter your Name: ");
scanf("%s",s[a].name);
printf("\nYour Deposit is Minimum Rs.500");
s[a].dep=500;
a++;
i++;
getch();
}
void deposit()
{
int no,b=0,m=0;
float aa;
printf("\n*************************************");
printf("\n CASH DEPOSIT ");
printf("\n*************************************");
printf("\nEnter your Account Number : ");
scanf("%d",&no);
for(b=0;b<i;b++)
{
if(s[b].no == no)
m = b;
}
if(s[m].no == no)
{
printf("\n Account Number : %d",s[m].no);
printf("\n Name : %s",s[m].name);
printf("\n Deposit : %f",s[m].dep);
printf("\n Deposited Amount : ");
scanf("%f",&aa);

PANIMALAR ENGINEERING COLLEGE


s[m].dep+=aa;
printf("\nThe Balance in Account is :%f",s[m].dep);
getch();
}
else
{
printf("\nACCOUNT NUMBER IS INVALID");
getch();
}
}
void withdraw()
{
int no,b=0,m=0;
float aa;
printf("\n*************************************");
printf("\n CASH WITHDRAW ");
printf("\n*************************************");
printf("\nEnter your Account Number : ");
scanf("%d",&no);
for(b=0;b<i;b++)
{
if(s[b].no == no)
m = b;
}
if(s[m].no == no)
{
printf("\n Account Number : %d",s[m].no);
printf("\n Name : %s",s[m].name);
printf("\n Deposit : %f",s[m].dep);
printf("\n Withdraw Amount : ");
scanf("%f",&aa);
if(s[m].dep<aa+500)
{
printf("\nCANNOT WITHDRAW YOUR ACCOUNT HAS MINIMUM BALANCE");
getch();
}
else
{
s[m].dep-=aa;
printf("\nThe Balance Amount in Account is:%f",s[m].dep);
}
}
else
{

PANIMALAR ENGINEERING COLLEGE


printf("INVALID");
getch();
}
getch();
}
void lowbal()
{
int no,b=0,m=0;
float aa;
printf("\n*************************************");
printf("\n FOLLOWING ACCOUNT HOLDER'S BALANCE IS LESS
THAN 1000 ");
printf("\n*************************************");
for(b=0;b<a;b++)
{
if(s[b].dep<1000)
{
printf("\n\n Account Number : %d",s[b].no);
printf("\n Name : %s",s[b].name);
}
}
}

Sample Input And Output


BANKING
***************************************************************************
[Link] New Account
[Link] Deposit
[Link] Withdraw
[Link] Balance Enquiry
[Link]
Enter your choice : 1
***************************************************************************
NEW ACCOUNT CREATION
Your Account Number is: 10001
Enter your Name: DIYA
Your Deposit is Minimum Rs.500

RESULT:

PANIMALAR ENGINEERING COLLEGE


[Link].:20

Program to create a Railway Reservation System

PROGRAM;

#include<stdio.h>
#include<conio.h>
int first=5,second=5,third=5;
struct node
{
int ticketno;
int phoneno;
char name[100];
char address[100];
}s[15];
int i=0;
void booking()
{
printf("enter your details");
printf("\nname:");
scanf("%s",s[i].name);
printf("\nphonenumber:");
scanf("%d",&s[i].phoneno);
printf("\naddress:");
scanf("%s",s[i].address);
printf("\nticketnumber only 1- 10:");
scanf("%d",&s[i].ticketno);
i++;
}
void availability()
{
int c;
printf("availability cheking");
printf("\[Link] class\[Link] class\[Link] class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1: if(first>0)
{
printf("seat available\n");
first--;
}

PANIMALAR ENGINEERING COLLEGE


else
printf("seat not available");
break;
case 2: if(second>0)
{
printf("seat available\n");
second--;
}
else
printf("seat not available");
break;
case 3:
if(third>0)
{
printf("seat available\n");
third--;
}
else
printf("seat not available"); break;
default:
break;
}
}
void cancel()
{
int c;
printf("cancel\n");
printf("which class you want to cancel");
printf("\[Link] class\[Link] class\[Link] class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
third++;
break;
default:
break;

PANIMALAR ENGINEERING COLLEGE


}
printf("ticket is canceled");
}
void chart()
{
int c;
for(c=0;c<i;c++)
{
printf(“\n Ticket No\t Name\n”);
printf("%d \t" , s[c].ticketno);
printf("%s \n", s[c].name);
}
}
void main()
{
int n;
clrscr();
printf("welcome to railway ticket reservation\n");
while(1)
{
printf("[Link]\[Link] cheking\n [Link]\n [Link] \n 5. Exit\nenter your
option:");
scanf("%d",&n);
switch(n)
{
case 1:
booking(); break;
case 2:
availability();
break;
case 3:
cancel(); break;
case 4:
chart(); break;
case 5:
printf(“\n Thank you visit again!”);
getch();
exit(0);
default:
break;
}
}
getch();
}

PANIMALAR ENGINEERING COLLEGE


Sample Input and Output

Welcome to Railway Ticket Reservation


[Link]
[Link] cheking
[Link]
[Link]
5. Exit
Enter your option:1
Enter your details
Name: Arun
Phonenumber: 9876543210
Address: Chennai
Ticket number only 1- 10: 5
[Link]
[Link] cheking
[Link]
[Link]
5. Exit
Enter your option: 1
Enter your details
Name: Varun
Phonenumber:9876543210
Address: Chennai
Ticketnumber only 1- 10: 6
[Link]
[Link] cheking
[Link]
[Link]
5. Exit
Enter your option: 4

Ticket No Name
5 Arun
6 Varun
[Link]
[Link] cheking
[Link]
[Link]
[Link]
Enter your option:5

Thank you visit again!

PANIMALAR ENGINEERING COLLEGE


RESULT:

PANIMALAR ENGINEERING COLLEGE

You might also like