int main() { float p, r, n, i; printf("Enter principal amount: "); scanf("%f", &p); printf("Enter rate of interest:"> int main() { float p, r, n, i; printf("Enter principal amount: "); scanf("%f", &p); printf("Enter rate of interest:">
PPS Lab Manual
PPS Lab Manual
ENGINEERING COLLEGE
BASNA
COMPUTER ENGINEERING DEPARTMNENT
Lab Manual
CERTIFICATE
Date of submission:
Faculty College Seal HOD
Index
#include <stdio.h>
int main()
{
int number1, number2, addition, subtraction, multiply;
float divide;
return 0;
}
Output:
b. Write a program to find area of triangle (a=h*b*.5) a = area h = height b = base.
Description: Below is the step by step descriptive logic to find area of a triangle.
Solution:
#include<stdio.h>
int main()
{
float a,h,b;
printf("\n Enter height :");
scanf("%f",&h);
printf("\n Enter base :");
scanf("%f",&b);
a=b*h*0.5;
printf("\n The Area of triangle = %.2f",a);
return 0;
}
Output:
PRACTICAL -2
Aim: Write a program to calculate simple interest (i = (p*r*n)/100) i = Simple interest p =
Principal amount r = Rate of interest n = Number of years.
Description: Step by step descriptive logic to calculate simple interest.
Solution:
#include <stdio.h>
int main()
{
int n;
float p, r, I;
printf("\n Enter Amount :");
scanf("%f",&p);
printf("\n Enter No of Years :");
scanf("%d",&n);
printf("\n Enter Rate :");
scanf("%f",&r);
I = (p*r*n)/100;
printf("\n Interest = %.2f",I);
return 0;
}
Output:
PRACTICAL -3
Aim: a. Write a C program to interchange two numbers.
#include <stdio.h>
int main()
{
int a,b;
printf("Enter Value of a :");
scanf("%d",&a);
printf("Enter Value of B :");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
}
Output:
b. Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and
centimeter.
#include <stdio.h>
int main()
{
float km;
printf("Enter Length in Kilometer : ");
scanf("%f",&km);
printf("\n %.2f KM = %.2f Meters",km,km*1000);
printf("\n %.2f KM = %.2f Feets",km,km*3280.84);
printf("\n %.2f KM = %.2f Inches",km,km*39370.08);
printf("\n %.2f KM = %.2f Centimeters",km,km*1000*100);
return 0;
}
Output:
PRACTICAL -4
Aim: a. Write a program to compute Fahrenheit from centigrade (f=1.8*c +32)
#include <stdio.h>
int main()
{
float F,C;
printf("Enter Temperature in Celsius : " );
scanf("%f",&C);
F = (C * 1.8) + 32;
printf("\n %.2f Celsius = %.2f Fahrenheit",C, F);
return 0;
}
Output:
#include<stdio.h>
int main() {
float u, a, d;
int t;
PRACTICAL -5
Aim: a. Write a C program to find that the accepted number is Negative or Positive or
Zero.
Description: We will use the above logic inside if to check number for
negative, positive or zero, Step by step descriptive logic to check negative,
positive or zero.
1. Input a number from user in some variable say num.
2. Check if(num < 0), then number is negative.
3. Check if(num > 0), then number is positive.
4. Check if(num == 0), then number is zero
Solution:
#include<stdio.h>
int main()
{
int no;
printf("\n Enter any number : ");
scanf("%d",&no);
if(no==0)
{
printf("\n Entered Number is Zero");
}
else if(no>0)
{
printf("\n Entered Number is Positive");
}
else
{
printf("\n Entered Number is Negative");
}
return 0;
}
Output:
b. Write a program to read marks of a student from keyboard whether the student is pass
or fail( using if else)
#include<stdio.h>
int main()
{
int marks;
printf("\n Enter Marks from 0-70 :");
scanf("%d",&marks);
if(marks<23)
{
printf("\n Sorry ..! You are Fail");
}
else
{
printf("\nCongratulation ...! You are Pass");
}
return 0;
}
Output:
c. Write a program to read three numbers from keyboard and find out maximum out of
these three. (nested if else)
#include<stdio.h>
int main()
{
int a,b,c;
printf("\n Enter First Number :");
scanf("%d",&a);
printf("\n Enter Second Number :");
scanf("%d",&b);
printf("\n Enter Third Number :");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("\n First Number %d is maximum",a);
}
else
{
printf("\n Third Number %d is maximum",c);
}
}
else
{
if(b>c)
{
printf("\n Second Number %d is maximum",b);
}
else
{
printf("\n Third Number %d is maximum",c);
}
}
return 0;
}
Output:
PRACTICAL -6
Aim: Write a C program to check whether the entered character is capital, small letter, digit or any
special character.
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter Any Character :");
scanf("%c",&ch);
if(ch>='0' && ch<='9')
{
printf("\n Entered Character is Digit");
}
else if(ch>='A' && ch<='Z')
{
printf("\n Entered Character is Capital Letter");
}
else if(ch>='a' && ch<='z')
{
printf("\n Entered Character is Small Letter");
}
else
{
printf("\n Entered Character is Special Character");
}
return 0;
}
Output:
PRACTICAL -7
Aim: a. Write a program to read marks from keyboard and your program should display
equivalent grade according to following table(if else ladder)
Marks Grade
100 - 80 Distinction
79 - 60 First Class
59 - 40 Second Class
< 40 Fail
#include<stdio.h>
int main() {
int marks;
printf("\n Enter Marks between 0-100 :");
scanf("%d", & marks);
if (marks > 100 || marks < 0) {
printf("\n Your Input is out of Range");
} else if (marks >= 80) {
printf("\n You got Distinction");
} else if (marks >= 60) {
printf("\n You got First Class");
} else if (marks >= 40) {
printf("\n You got Second Class");
} else {
printf("\n You got Fail");
}
return 0;
}
Output:
b. Write a c program to prepare pay slip using following data. Da = 10% of basic, Hra = 7.50% of
basic, Ma = 300, Pf = 12.50% of basic, Gross = basic + Da + Hra + Ma, Nt = Gross – Pf.
#include<stdio.h>
int main() {
float basic;
printf("\n Enter Basic Salary :");
scanf("%f", & basic);
printf("\n===================================");
printf("\n SALARY SLIP");
printf("\n===================================");
printf("\n Basic : %.2f", basic);
printf("\n DA : %.2f", basic * 0.10);
printf("\n HRA : %.2f", basic * 0.075);
printf("\n MA : %.2f", 300.00);
printf("\n===================================");
printf("\n GROSS : %.2f", basic + (basic * 0.10) + (basic * 0.075) + 300.00);
printf("\n===================================");
printf("\n PF : %.2f", basic * 0.125);
printf("\n===================================");
printf("\n NET : %.2f", (basic + (basic * 0.10) + (basic * 0.075) + 300.00) - (basic * 0.125));
printf("\n===================================");
return 0;
}
Output:
PRACTICAL -8
Aim: a. Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.
#include<stdio.h>
int main()
{
int no;
printf("\n Enter Day no between 1-7 : ");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n Sunday");
break;
case 2:
printf("\n Monday");
break;
case 3:
printf("\n Tuesday");
break;
case 4:
printf("\n Wednesday");
break;
case 5:
printf("\n Thursday");
break;
case 6:
printf("\n Friday");
break;
case 7:
printf("\n Saturday");
break;
default:
printf("\n Please Enter Proper Input");
break;
}
return 0;
}
Output:
b. Write a C program to find out the Maximum and Minimum number from given 10
numbers
#include <stdio.h>
int main()
{
int a[10],i,min,max;
for(i=0;i<10;i++)
{
printf("\n Enter Interger Value [%d] : ",i+1);
scanf("%d",&a[i]);
if(i==0)
{
min=max=a[i];
}
else
{
if(min>a[i])
{
min=a[i];
}
if(max<a[i])
{
max=a[i];
}
}
}Z
printf("\n Minimum : %d",min);
printf("\n Maximum : %d",max);
return 0;
}
Output:
c. Write a C program to input an integer number and check the last digit of number is even
or odd.
#include <stdio.h>
int main()
{
int i;
printf("\n Enter any Number : ");
scanf("%d",&i);
// Condition can be write as
// if(i%2==0)
// ultimately Even number has last digit even and same for odd
if((i%10)%2==0)
{
printf("\n Last Digit of Number is Even");
}
else
{
printf("\n Last Digit of Number is Odd");
}
return 0;
}
Output:
#include <stdio.h>
int main()
{
int no,fact=1;
printf("\n Enter No to find its Factorial : ");
scanf("%d",&no);
scanf("%d",&no);
while(no>1)
{
fact=fact*no;
no=no-1;
}
printf("\n Factorial of entered no is : %d",fact);
return 0;
}
Output:
PRACTICAL -9
Aim: a. Write a program to reverse a number.
#include <stdio.h>
int main()
{
int no,rev=0;
printf("\n Enter No to make it Reverse : ");
scanf("%d",&no);
while(no>0)
{
rev=(rev*10)+(no%10);
no=no/10;
}
printf("\n Reverse of entered no is : %d",rev);
return 0;
}
Output:
#include <stdio.h>
int main()
{
int no=10,i=0,j=1;
printf(" %d %d",i,j);
while(no>0)
{
printf(" %d",i+j);
j=i+j;
i=j-i;
no--;
}
return 0;
}
Output:
c. Write a program to find out sum of first and last digit of a given number.
#include <stdio.h>
int main()
{
int no,sum=0;
printf("\n Enter Any Number :");
scanf("%d",&no);
if(no<10)
{
sum = sum + (no*2);
}
else
{
sum = sum + (no%10);
while(no>9)
{
no = no /10;
}
sum = sum + no;
}
printf("\n Sum of First & Last Digit is : %d",sum);
return 0;
}
Output:
d. Write a C program to find the sum and average of different numbers which are accepted
by user as many as user wants
#include <stdio.h>
int main()
{
int no,sum=0,i=0,val;
printf("\n How many nos you want to enter : ");
scanf("%d",&no);
while(i<no)
{
printf("Enter No [%d]:",i+1);
scanf("%d",&val);
sum=sum+val;
i++;
}
printf("\n Sum = %d",sum);
printf("\n Sum = %.2f",((float)sum)/no);
return 0;
}
Output:
e. Write a program to calculate average and total of 5 students for 3 subjects (use nested for loops)
#include<stdio.h>
int main()
{
int student=0,sum=0,marks=0,sub;
for(student=0;student<5;student++)
{
sum=0;
printf("\n Student - %d ",student+1);
for(sub=0;sub<3;sub++)
{
printf("\nEnter Marks for Subject - %d:- ",sub+1);
scanf("%d",&marks);
sum=sum+marks;
}
printf("\n For Student - %d : ",student+1);
printf("\n Sum = %d",sum);
printf("\n Average = %.2f",((float)sum)/sub);
}
return 0;
}
Output:
PRACTICAL -10
Aim: a.Read five persons height and weight and count the number of person having height
greater than 170 and weight less than 50,
#include<stdio.h>
int main()
{
int person,height,weight,count=0;
for(person=0;person<5;person++)
{
printf("\n Enter Detail of Person - %d",person+1);
printf("\n Enter Height : ");
scanf("%d",&height);
printf("\n Enter Weight : ");
scanf("%d",&weight);
if(height>170)
{
if(weight<50)
{
count++;
}
}
}
printf("\n Total Person having Height > 170 and Weight < 50 : %d",count);
return 0;
}
Output:
#include<stdio.h>
int main()
{
int no,i;
printf("\n Enter No to check whether its prime or not :");
scanf("%d",&no);
for(i=2;i<no;i++)
{
if(no%i==0)
{
printf("\n %d is not prime",no);
break;
}
}
(no==i)
{
printf("\n %d is prime",no);
}
return 0;
}
Output:
PRACTICAL -11
Aim: a. Write a program to evaluate the series 1^2+2^2+3^2+……+n^2
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("\n Sum of Series = %d",sum);
return 0;
}
Output:
#include<stdio.h>
int main()
{
int n,i;
float sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1.0/i);
}
printf("\n Sum of Series = %f",sum);
return 0;
}
Output:
#include<stdio.h>
int main()
{
int n,i,j,fact=1;
float sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact * j;
}
sum=sum+(1.0/fact);
}
printf("\n Sum of Series = %f",sum);
return 0;
}
Output:
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,x,fact=1;
float sum=0;
printf("\n Enter Highest Power Value (Max 9):");
scanf("%d",&n);
printf("\n Enter the Value of X :");
scanf("%d",&x);
for(i=0;i<=n;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact*j;
}
if(i%2==0)
{
sum=sum+(pow(x,i)/fact);
}
else
{
sum=sum-(pow(x,i)/fact);
}
}
printf("\n Sum of Series = %f",sum);
return 0;
}
Output:
PRACTICAL -12
ii.
#include <stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf(" *");
}
printf("\n");
}
return 0;
}
Output:-
iii.
#include <stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<i;j++)
{
printf(" ");
}
for(j=i;j<5;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:-
I. #include <stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",j+1);
}
printf("\n");
}
return 0;
}
Output:
II.
#include <stdio.h>
int main(void) {
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5 - i; j++) {
printf("%c", 'A' + j);
}
printf("\n");
}
return 0;
}
Output:
PRACTICAL -13
Aim: a. Write a C program to read and store the roll no and marks of 20 students using
array.
#include <stdio.h>
int main(void) {
int rollno[20], marks[20], i;
for (i = 0; i < 20; i++) {
printf("\n Enter Roll of Student [%d]", i + 1);
scanf("%d", & rollno[i]);
printf("\n Enter Mark of Student [%d]", i + 1);
scanf("%d", & marks[i]);
}
for (i = 0; i < 20; i++) {
printf("\n Roll No : %d Marks : %d", rollno[i], marks[i]);
} return 0;
}
Output:
b. Write a program to find out which number is even or odd from list of 10 numbers using
array
#include <stdio.h>
int main(void) {
int a[10], i;
for (i = 0; i < 9; i++) {
printf("\n Enter Value in Array at Position [%d] :", i + 1);
scanf("%d", & a[i]);
}
for (i = 0; i < 9; i++) {
if (a[i] % 2 == 0) {
printf("\n %d is an EVEN number.", a[i]);
} else {
printf("\n %d is an ODD number.", a[i]);
}
}
return 0;
}
Output:
#include <stdio.h>
int main(void) {
int a[50], i, n, max;
printf("\n Enter How many numbers you want to enter [Max 50] : ");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("\n Enter Value in Array at Position [%d] :", i + 1);
scanf("%d", & a[i]);
if (i == 0) {
max = a[i];
} else {
if (max < a[i]) {
max = a[i];
}
}
}
printf("\n Maximum Value in Array = %d", max);
return 0;
}
Output:
d. Write a C program to calculate the average, geometric and harmonic mean of n elements in an
array.
#include<stdio.h>
#include<math.h>
int main() {
float a[50], sum = 0, sum1 = 0, sum2 = 1;
int i, n;
printf("\n How many numbers you want to enter :");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("\n Enter Value at Position [%d] : ", i + 1);
scanf("%f", & a[i]);
sum = sum + a[i];
sum1 = sum1 + (1.0 / a[i]);
sum2 = sum2 * a[i];
}
printf("\n Average = %f", sum / n);
printf("\n Geometric Mean = %f", pow(sum2, (1.0 / n)));
printf("\n Harmonic Mean = %f", n * pow(sum1, -1));
return 0;
}
Output:
PRACTICAL -14
Aim: Write a program to sort given array in ascending order (Use Insertion sort, Bubble
sort, Selection sort, Merge sort, Quick sort, Heap sort).
int main() {
int a[10], i, j, n, min, temp;
printf("\n Enter How many numbers you want to enter: ");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("\n Enter Value at Position [%d] :", i + 1);
scanf("%d", & a[i]);
}
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
min = a[i];
for (j = i + 1; j < n; j++) {
if (a[j] < a[i]) {
min = j;
// Swap the found minimum element with the first element
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
printf(" %d ->", a[i]);
}
printf(" %d ->", a[i]);
return 0;
}
Output:
PRACTICAL -15
Aim: a. Write a program to find a character from given string.
#include <stdio.h>
int main() {
char str[20], ch, flag = 1;
int i = 0;
printf("\n Enter String ");
gets(str);
printf("Enter Character to Search in String :");
scanf("%c", & ch);
printf("\n Character ");
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
printf(" %d ", i + 1);
flag = 0;
}
}
if (flag == 1) {
printf("NOT FOUND");
}
return 0;
}
Output:
int main(){
char s[10],chr,repl_chr;
int i=0;
while(s[i]!='\0'){
if(s[i]==chr){
s[i]=repl_chr;
}
i++;
}
printf("\n Modified string after replacement is: %s",s);
getch();
}
Output:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, len, j;
printf("\n Enter any String : ");
gets(str);
printf("\n Enter the Character to delete : ");
scanf("%c", &ch);
len = strlen(str);
Output:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i;
printf("Enter String : ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nString in Uppercase: %s",str);
return 0;
}
Output:
PRACTICAL -16
Aim: a. Write a program that defines a function to add first n numbers.
#include <stdio.h>
int getsum(int);
int main(void) {
int n;
printf("Enter Any number n = ");
scanf("%d", & n);
printf("\n SUM = %d", getsum(n));
return 0;
}
int getsum(int n) {
return ((n * (n + 1)) / 2);
}
Output:
#include <stdio.h>
int isprime(int);
void main() {
int n, c = 0;
printf("\nEnter n: ");
scanf("%d", & n);
c = isprime(n);
if (c > 0)
printf("\nNumber %d is not prime.\n", n);
else
printf("\nNumber %d is prime.\n", n);
}
int isprime(int n) {
int i, c = 0;
for (i = 2; i < n; i++) {
if (n % i == 0) {
c++;
break;
}
}
return c;
}
Output:
c. Write a function Exchange to interchange the values of two variables, say x and y.
illustrate the use of this function in a calling function.
#include<stdio.h>
void swap(int,int);
int main()
{
int x=10,y=20;
printf("\n value of x and y before swapping:");
printf("\n value of x:\t%d",x);
printf("\n value of y:\t%d",y);
swap(x,y);
printf("\n value of x and y after swapping:");
printf("\n value of x:\t%d",x);
printf("\n value of y:\t%d",y);
getch();
}
void swap(int a,int b){
int temp=a;
a=b;
b=temp;
}
Output:
d. Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! +
… xn/ n!.
#include<stdio.h>
#include<conio.h>
int main()
{
float series(float,int),x;
int n;
printf("\nEnter X:");
scanf("%f",&x);
printf("\nEnter n:");
scanf("%d",&n);
printf("\nAns %f",series(x,n));
getch();
return 0;
}
float series(float x,int n)
{
long factorial(int);
float power(float,int);
float sum=0;
int i,s=1;
for(i=1;i<=n;i+=2)
{
sum+=(power(x,i)/factorial(i))*s;
s*=-1;
}
return sum;
}
float power(float x, int y)
{
float p=1;
int i;
for(i=1;i<=y;i++)p*=x;
return p;
}
long factorial(int p)
{
long f=1;
int i;
for(i=1;i<=p;i++)f*=i;
return f;
}
Output:
int fact(int);
int main() {
int n;
printf("\n Enter Value of n :");
scanf("%d", & n);
printf("Factorial = %d", fact(n));
return 0;
}
int fact(int n) {
if (n == 1) {
return 1;
}
#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}
int main(){
printf("%d",fun());
printf("%d",fun());
printf("%d",fun());
getch();
}
Output:
#include<stdio.h>
int cnt = 0;
int fun()
{
cnt++;
printf("%d",cnt);
}
int main(){
fun();
cnt++;
printf("%d",cnt);
fun();
getch();
}
Output:
b. Write a function that will scan a character string passed as an argument and convert all
lowercase character into their uppercase equivalents
#include<stdio.h>
int main()
{
char str[20];
printf("\n Enter a string:");
gets(str);
cUpper(str);
getch();
}
cUpper(char str[]){
int i=0;
while(str[i]!='\0'){
if(str[i]>='a'&& str[i]<='z')
printf("%c",str[i]-32);
else
printf("%c",str[i]);
i++;
}
}
Output:
struct book {
int id;
char name[20];
float price;
};
int main(void) {
struct book b1;
printf("\n Enter Book Id : ");
scanf("%d", & b1.id);
fflush(stdin);
printf("\n Enter Book Name : ");
scanf("%s", b1.name);
printf("\n Enter Book Price : ");
scanf("%f", & b1.price);
e. Define a structure type struct personal that would contain person name, date of joining
and salary using this structure to read this information of 5 people and print the same on
screen.
#include <stdio.h>
struct person {
char name[20];
char doj[10];
float salary;
}
p[5];
int main(void) {
int i = 0;
for (i = 0; i < 5; i++) {
printf("\n Enter Person Name : ");
scanf("%s", p[i].name);
printf("\n Enter Person Date of Joining (dd-mm-yyyy) : ");
scanf("%s", p[i].doj);
printf("\n Enter Person Salary : ");
scanf("%f", & p[i].salary);
}
d. Define structure data type called time_struct containing three member’s integer hour, integer
minute and integer second. Develop a program that would assign values to the individual number
and display the time in the following format: 16: 40:51
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
}t;
int main(void)
{
printf("\n Enter Hour : ");
scanf("%d",&t.hour);
printf("\n Enter Minute: ");
scanf("%d",&t.minute);
printf("\n Enter Second : ");
scanf("%d",&t.second);
Output:
PRACTICAL -18
Aim: Define a structure called cricket that will describe the following information: Player
name
Team name
Batting average
Using cricket, declare an array player with 50 elements and write a C program to read the
information about all the 50 players and print team wise list containing names of players with their
batting average.
#include <stdio.h>
#include <string.h>
struct cricket {
char player_name[20];
char team_name[20];
float batting_avg;
}
p[50], t;
int main(void) {
int i = 0, j = 0, n = 50;
j = 0;
for (i = 0; i < n; i++) {
if (strcmp(p[i].team_name, p[j].team_name) != 0 || i == 0) {
printf("\n Team Name: %s", p[i].team_name);
j = i;
}
printf("\n Player Name = %s", p[i].player_name);
printf("\n Batting Average = %f", p[i].batting_avg);
}
return 0;
}
Output:
PRACTICAL -19
Aim: a. Design a structure student record to contain name, branch and total marks
obtained. Develop a program to read data for 10 students in a class and print them.
#include <stdio.h>
struct student_record
{
char name[20];
char branch[20];
int total_marks;
}p[10];
int main(void)
{
int i=0,n=10;
for(i=0;i<n;i++)
{
printf("\n Enter Student Name : ");
scanf("%s",p[i].name);
printf("\n Enter Students Branch : ");
scanf("%s",p[i].branch);
printf("\n Enter Students Marks : ");
scanf("%d",&p[i].total_marks);
}
for(i=0;i<n;i++)
{
printf("\n Student %d Detail",i+1);
printf("\n Name = %s",p[i].name);
printf("\n Branch = %s",p[i].branch);
printf("\n Total marks = %d",p[i].total_marks);
}
return 0;
}
Output:
b. Write a program to print address of variable using pointer.
#include <stdio.h>
int main(void) {
int i = 15;
int * p;
p = & i;
printf("\n Address of Variable i = %u", p);
return 0;
}
Output:
c. Write a C program to swap the two values using pointers.
#include <stdio.h>
void swap(int * , int * );
int main(void) {
int i = 25, j = 35;
printf("\n Before Swapping i = %d j = %d", i, j);
swap( & i, & j);
printf("\n After Swapping i = %d j = %d", i, j);
return 0;
}
void swap(int * a, int * b) {
* a = * a + * b;
* b = * a - * b;
* a = * a - * b;
}
Output:
PRACTICAL -20
Aim: a. Write a C program to print the address of character and the character of string
using pointer.
Output:
#include <stdio.h>
int main(void)
{
int a[10]={2,4,6,7,8,9,1,2,3,4};
int *p,i=0;
p=&a[0];
while(i<10)
{
printf("\n Position : %d Value : %d",i+1,*(p+i));
i++;
}
return 0;
}
Output:
#include <stdio.h>
int main(void)
{
int a[10]={2,10,6,7,8,9,5,3,4,1};
int *p,i=0,j=0;
p=&a[0];
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(*(p+i) > *(p+j))
{
*(p+i) = *(p+i) + *(p+j);
*(p+j) = *(p+i) - *(p+j);
*(p+i) = *(p+i) - *(p+j);
}
}
}
printf("\n Sorted Values : ");
for(i=0;i<10;i++)
{
printf("%d ",*(p+i));
}
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
int main() {
char str[50], ch[50];
FILE * fp;
fp = fopen("Test.txt", "w");
fputs(str, fp);
fclose(fp);
fp = fopen("Test.txt", "r");
fgets(ch, 50, fp);
fclose(fp);
#include <stdio.h>
main()
{
FILE *f1, *f2, *f3;
int number, i;
printf("Contents of DATA file\n\n");
f1 = fopen("DATA", "w"); /* Create DATA file */
for(i = 1; i <= 30; i++)
{
scanf("%d", &number);
if(number == -1) break;
putw(number,f1);
}
fclose(f1);
f1 = fopen("DATA", "r");
f2 = fopen("ODD", "w");
f3 = fopen("EVEN", "w");
while((number = getw(f1)) != EOF)
{
if(number %2 == 0)
putw(number, f3); /* Write to EVEN file */
else
putw(number, f2); /* Write to ODD file */
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
f3 = fopen("EVEN", "r");
printf("\n\nContents of ODD file\n\n");
while((number = getw(f2)) != EOF)
printf("%4d", number);
printf("\n\nContents of EVEN file\n\n");
while((number = getw(f3)) != EOF)
printf("%4d", number);
fclose(f2);
fclose(f3);
}
Output: