SECTION – 1
C Programming Lab
Session
1
Ex 1: Write an interactive
program to calculate simple Interest and Compound Interest.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float
pri,amt,rate,si,ci,time,interest,i,j=1;
clrscr();
printf("\n\n\t\tEnter
the principle -> ");
scanf("%f",&pri);
printf("\n\n\t\tEnter
the rate -> ");
scanf("%f",&rate);
printf("\n\n\t\tEnter
the time In Year -> ");
scanf("%f",&time);
//
-------Programm For Simple Interest---------
interest=(pri*rate*time)/100;
si=pri+interest;
printf("\n\n\t\tYour
Interest is %f",interest);
printf("\n\n\t\tYour
Simple Interest is %f",si);
//
------Programm For Compound Interest--------
for(i=1;
i<=time; i++)
{
j=(rate+100)/100*j;
}
ci=pri*j;
interest=ci-pri;
printf("\n\n\t\tYour
Interest in Compound is %f",interest);
printf("\n\n\t\tYour
Compound Interest is %f",ci);
getch();
}
Output:
Ex
2: Write an
interactive program that uses loop to input the income and calculate and report
the owed tax amount. Make sure that your calculation is mathematically accurate
and that transaction errors eliminated.
Assume that the
United States of America uses the following income tax code formula for their
annual income:
First
US$ 5000 of income : 0% tax
Next
US$ 10,000 of income : 10% tax
Next
US$ 20,000 of income : 15% tax
An
amount above US$35,000 : 20% tax
For example, somebody
earning US$ 38,000 annually would owe
US$5000x0.00+10,000x0.10+20,000x0.15+3000x0.20,
which
comes to US$4600.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float
income,i=0,j=0,k=0,tax=0;
clrscr();
printf("\n\n\t\tEnter
Your Income Tax -> ");
scanf("%f",&income);
//--------Calculate
The Tax---------
if(income>35000)
{
income=income-35000;
i=10000*.10;
j=20000*.15;
k=income*.20;
tax=i+j+k;
printf("\n\n\t\tYour
Tax is %f",tax);
}
else
if(income>20000 && income<=35000)
{
income=income-15000;
i=10000*.10;
j=income*.15;
tax=i+j;
printf("\n\n\t\tYour
Tax is %f",tax);
}
else
if(income>10000 && income<=20000)
{
income=income-15000;
i=10000*.10;
j=income*.15;
tax=i+j;
printf("\n\n\t\tYour
Tax is %f",tax);
}
else
if(income>5000 && income<=10000)
{
income=income-5000;
tax=income*.10;
printf("\n\n\t\tYour
Tax is %f",tax);
}
else
{
printf("\n\n\t\t
You have no tax");
}
getch();
}
Output:
Ex
3: Write an interactive
program that reads in integers until a 0 is entered. If it encounters 0 as
input, then it should display:
- the total no. of even and odd integers.
- average value of even integers.
- average value of odd integers.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
val,even=0,odd=0,sum_even=0,sum_odd=0,avg_even,avg_odd;
clrscr();
first:
printf("\n\n\t\tEnter
value -> ");
scanf("%d",&val);
if(val==0)
{
goto
last;
}
else
if(val%2==0)
{
even++;
sum_even=sum_even+val;
goto
first;
}
else
{
odd++;
sum_odd=sum_odd+val;
goto
first;
}
last:
avg_even=sum_even/even;
avg_odd=sum_odd/odd;
printf("\n\n\t\tYour
Total Even No is %d",even);
printf("\n\n\t\tYour
Total Odd No. is %d",odd);
printf("\n\n\t\tYour
Avg value of Even is %d",avg_even);
printf("\n\n\t\tYour
Avg value of Odd is %d",avg_odd);
getch();
}
Output:
Ex
4: Write an
interactive program to generate the divisors of a given integers.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
val,i;
clrscr();
printf("\n\n\t\tEnter
value ->");
scanf("%d",&val);
for(i=1;
i<=val; i++)
{
if(val%i==0)
{
printf("\n\n\t\t
%d",i);
}
}
getch();
}
Output:
Session
2
Ex
5: Write a
program to find all Armstrong Number in the range of 0 and 999.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
val,i,j=0,val1;
clrscr();
printf("\n\n\t\tEnter
Number To Check Armstrong -> ");
scanf("%d",&val);
val1=val;
while(val>=1)
{
i=val%10;
i=i*i*i;
j=i+j;
val=val/10;
}
if(val1==j)
{
printf("\n\n\t\tYour
No Is Armstrong");
}
else
{
printf("\n\n\t\tYour
No is Not Armstrong");
}
getch();
}
Output:
Ex 6: Write a program to check
whether a given number is a perfect number or not.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
val,i,j=0;
clrscr();
printf("\n\n\tEnter
the Number to Check its Perfect or Not -> ");
scanf("%d",&val);
for(i=1;
i<val; i++)
{
if(val%i==0)
{
j=j+i;
}
}
if(j==val)
{
printf("\n\n\t\tYour
No is Perfect No");
}
else
{
printf("\n\n\t\tYour
No is not a perfect no");
}
getch();
}
Output:
Ex
7: Write a
program to check whether given two numbers are amicable numbers or not.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
val,val1,i,j=0,k,l=0;
clrscr();
printf("\n\n\t\tEnter
first value -> ");
scanf("%d",&val);
printf("\n\n\t\tEnter
Second value -> ");
scanf("%d",&val1);
for(i=1;
i<val; i++)
{
if(val%i==0)
{
j=j+i;
}
}
for(k=1;
k<val1; k++)
{
if(val1%k==0)
{
l=l+k;
}
}
if(l==val
&& j==val1)
{
printf("\n\n\t\tNo
is amicable");
}
else
{
printf("\n\n\t\tNot
a Amicable");
}
getch();
}
Output:
Ex
8: Write a program to find the roots of
a quadratic equation.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
double a,b,c,d,root1,root2;
clrscr();
printf("\n- A quadratic equation is in the forrm a * x
* x + b * x + c = 0");
printf("\n\n- To solve the equation ,please provide the
value of a, b & c -");
printf("\n\n a = ");
scanf("%lf", &a);
printf("\n b = ");
scanf("%lf", &b);
printf("\n c = ");
scanf("%lf", &c);
d=(b*b-4*a*c);
if(d<0)
{
printf("\n Cannot claculate roots, as these would be
complex numbers.\n");
getch();
exit(0);
}
root1=(-b+sqrt(d))/(2.0*a);
root2=(-b-sqrt(d))/(2.0*a);
printf("\n The roots of the quadratic equation are %lf
& %lf", root1,root2);
getch();
}
Output:
Session
3
Ex
9: Write a
function invert(x,p,n) that returns x with the n bits that begin at position p
inverted. You can assume that x,p & n are integer variables and that the
function will return an integer.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int
intUserInput, intUserInput1, intUserInput2;
int
intCompResult;
int
invert(int, int, int);
clrscr();
printf("\n\n\t
Please insert integer to invert: ");
scanf("%d",
&intUserInput);
printf("\n\n\t
Please insert starting point to invert: ");
scanf("%d",
&intUserInput);
printf("\n\n\t
Please insert Length to invert: ");
scanf("%d",
&intUserInput2);
intCompResult=invert(intUserInput,
intUserInput1, intUserInput2);
printf("\n\n\t
Invert no. is : %d", intCompResult);
getch();
}
int invert(int x, int p, int n)
{
int
intbinary[8];
int
i;
int
y;
int
r=0;
for(i=0;i<8;i++)
{
intbinary[i]=0;
}
i=0;
y=0;
while(x>0)
{
intbinary[i]=x%2;
x=x/2;
i++;
}
for(i=0;i<8;i++)
{
if(i==p)
{
for(i=p;i>p-n;i--)
{
if(intbinary[i]==0)
{
intbinary[i]=1;
}
else
{
intbinary[i]=0;
}
}
i=i+n;
}
}
for(i=0;i<8;i++)
{
r=r+(intbinary[i]*pow(2,i));
}
return
r;
}
Output:
Ex 10: Write
a function that calculates the compounded interest amount for a given initial
amount, interest rate & no. of years. The interest is compounded annually.
The return value will be the interest amount. Use the following function
definition: float comp_int_calc(floatint_amt, float rate, int years); Write a
program that will accept the initial amount, interest rate & the no. of
years and call the function with these values to find out the interest amount
and display the returned value.
Code:
#include<stdio.h>
#include<conio.h>
float interest(float int_amt,float
rate, int year);
void main()
{
int
int_amt,year;
float
rate,amt;
clrscr();
printf("\n\n\t
Enter the Principle Amt.-> ");
scanf("%d",&int_amt);
printf("\n\n\t
Enter the Rate of Interest -> ");
scanf("%f",&rate);
printf("\n\n\t
Enter the No. of Years -> ");
scanf("%d",&year);
amt=interest(int_amt,rate,year);
printf("\n\n\t\tYour
compound Interest is %f",amt);
getch();
}
float interest(float int_amt,float
rate, int year)
{
float
interest,amt,ci;
float
i,j=1;
for(i=1;
i<=year; i++)
{
j=(rate+100)/100*j;
}
interest=int_amt*j;
ci=int_amt+interest;
return
ci;
}
Output:
Ex
11: Break up the program that you wrote to solve above problem
into two separate source files. The main function should be in one file &
the calculation function must be in another file. And modify the program so
that the interest rate is a symbolic constant and is no longer input from the
keyboard. And put all the C preprocessor directives into a separate header file
that is included in the two program source files.
Code:
file-1.c
#include "header.h"
main()
{
float amt,interest;
int year;
float comp_int_calc(float,float,int);
clrscr();
printf("Enter the initial amount: ");
scanf("%f",&amt);
printf("Enter the Number of years: ");
scanf("%f",&year);
interest=comp_int_calc(amt,roi,year);
printf("\nThe int is %.2f",interest);
getch();
}
#include "header.h"
main()
{
float amt,interest;
int year;
float comp_int_calc(float,float,int);
clrscr();
printf("Enter the initial amount: ");
scanf("%f",&amt);
printf("Enter the Number of years: ");
scanf("%f",&year);
interest=comp_int_calc(amt,roi,year);
printf("\nThe int is %.2f",interest);
getch();
}
file-2.c
#include "header.h"
float comp_int_calc(float x,float y,int z)
{
float i;
i=x*pow((1+y/100),2);
return(i-x);
}
header.h
#include<stdio.h>
#include<math.h>
#define roi 10
Then press Alt+P in Turbo C and enter a project file name, e.g. Q11.prj. Create a new project file of the same name e.g. Q11.prj and enter the following in it-
file-1.c
file-2.c
header.h
Now compile the project file and the desired output will be obtained.
Output:
Ex
12: Define two separate macros,MIN &
MAX, to find and return, respectively the minimum & maximum of two values.
Write a sample program that usesthese macros.
Code:
#include<stdio.h>
#include<conio.h>
#define min(x,y)(x<y ? x:y)
#define max(x,y)(x>y ? x:y)
void main()
{
int
i,j;
clrscr();
printf("\n\n\t
Enter two numbers to compare :-");
printf("\n\n\tFirst
Number -> ");
scanf("%d",
&i);
printf("\n\tSecond
Number -> ");
scanf("%d",
&j);
printf("\n\n\tThe
maximum number is %d", max(i,j));
printf("\n\n\tThe
minimum number is %d", min(i,j));
getch();
}
Output:
Session
4
Ex
13: Write
a program that will take as input a set of integers and find and display the
largest and the smallest values within the input data values.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
arr[5],i,j,k,ii;
clrscr();
for(i=0;
i<5; i++)
{
printf("\n\n\t
Enter the Number -> ");
scanf("%d",&arr[i]);
}
i=arr[0];
ii=arr[0];
for(j=0;
j<5; j++)
{
if(arr[j]<ii)
{
ii=arr[j];
}
if(arr[j]>i)
{
i=arr[j];
}
}
printf("\n\n\t\tGreatest
value is %d",i);
printf("\n\n\t\tSmalles
value is %d",ii);
getch();
}
Output:
Ex 14: Write an interactive program that will take as input a set
of 20 integers and store them in an array and using a temporary array of equal
length, reverse the order of the integers & display the values.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
arr[20],arr1[20],i,j=0;
clrscr();
//--------Input
In First Array-------
printf("
- Enter 20 Integers value to store in Array - \n\n");
for(i=0;
i<20; i++)
{
printf("\t
Enter value -> ");
scanf("%d",&arr[i]);
}
//-------For
Reverse Order--------
for(i=19;
i>=0; i--)
{
arr1[j]=arr[i];
j++;
}
getch();
clrscr();
//--------Print
First Array------------
printf("\n\n\t\tFirst
Array Without Reverse");
for(i=0;
i<20; i++)
{
printf("\n\t\t%d",arr[i]);
}
getch();
clrscr();
//-------Print
Second Array-------------
printf("\tArray
In Reverse Order");
for(i=0;i<19;
i++)
{
printf("\n\t\t%d",arr1[i]);
}
getch();
}
Output:
Ex
15: Write an interactive program to do the following computation
by providing the option using the switch statement:
(i)
Add two matrices.
(ii)
Subtract two matrices.
(iii)
Multiply two matrices.
Code:
#include<stdio.h>
#include<conio.h>
int arr[3][3],arr1[3][3],arr2[3][3],i,j,sum,k;
void input()
{
printf("\n\n\tEnter
Value For first Array\n\n");
for(i=0; i<3;
i++)
{
for(j=0;
j<3; j++)
{
printf("\t
Enter The Value -> ");
scanf("%d",&arr[i][j]);
}
}
printf("\n\n\t\tEnter
Value For Second Array\n\n");
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
printf("\t\tEnter
The Value -> ");
scanf("%d",&arr1[i][j]);
}
}
}
void display()
{
getch();
clrscr();
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
printf("\t%d",arr2[i][j]);
}
printf("\n\n");
}
}
void addition()
{
input();
//int
i,j;
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
arr2[i][j]=arr[i][j]+arr1[i][j];
}
}
}
void sub()
{
//int
i,j;
input();
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
arr2[i][j]=arr[i][j]-arr1[i][j];
}
}
}
void multiply()
{
input();
//int
i,j,k;
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
arr2[i][j]=0;
for(k=0;
k<3; k++)
{
arr2[i][j]=arr2[i][j]+arr[i][k]*arr1[k][j];
}
}
}
}
void main()
{
int
ch;
clrscr();
printf("\n\n\t\tEnter
Any Choice");
printf("\n\n\t\t1.
Addition of Matrix");
printf("\n\n\t\t2.
Subtraction of Matrix");
printf("\n\n\t\t3.
Multiply of Matrix");
printf("\n\n\t\tEnter
Your Choice - ");
scanf("
%d",&ch);
getch();
clrscr();
switch(ch)
{
case
1:
{
addition();
display();
break;
}
case
2:
{
sub();
display();
break;
}
case
3:
{
multiply();
display();
break;
}
default:
{
printf("\n\n\t\tWrong
choice");
}
}
getch();
}
Output:
Session
5
Ex
16: Write
a program to check if the given matrix is square or not.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
intAUserMatrix[3][3];
int i,j;
void
check_msquare(int a[3][3]);
clrscr();
printf("\n\n\t
Please enter the matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n\t\tMatrix[%d][%d]=",
i,j);
}
}
printf("\n\t\tGiven
matrix is: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n\t\t%d",
intAUserMatrix[i][j]);
}
printf("\n");
}
check_msquare(intAUserMatrix);
getch();
}
void check_msquare(int matrix[3][3])
{
int
row,col,sumRow[3],sumCol[3];
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
sumCol[col]=sumCol[col]+matrix[row][col];
}
}
for(col=0;col<3;col++)
{
for(row=0;row<3;row++)
{
sumRow[row]=sumCol[row]+matrix[row][col];
}
}
if(sumCol[0]==sumCol[1]
&& sumCol[0]==sumCol[2] && sumCol[0]==sumRow[0] &&
sumRow[0]==sumRow[1] && sumRow[0]==sumRow[2])
{
printf("\n\n\t\tMagic
Square");
}
else
{
printf("\n\t\tNot
a magic square\n");
}
}
Output:
Ex 17: Write
a program to print the upper and lower triangle of matrix.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
arr[3][3],i,j,k=2;
clrscr();
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
printf("\tEnter
value -> ");
scanf("%d",&arr[i][j]);
}
}
printf("\n\n");
for(i=0;
i<3; i++)
{
for(j=0;
j<=2; j++)
{
if(arr[i]==arr[k])
{
printf("\t-");
k--;
}
else
{
printf("\t%d",arr[i][j]);
k--;
}
}
printf("\n\n");
k=2;
}
getch();
}
Output:
Ex
18: Write a program To compute transpose
of a matrix.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
arr[3][3],i,j;
clrscr();
printf("\t-
Enter Values To Compute Transpose of a Matrix - \n\n");
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
printf("\tenter
value -> ");
scanf("%d",&arr[i][j]);
}
}
printf("\n\n");
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
printf("\t\t%d",arr[i][j]);
}
printf("\n\n");
}
printf("\n\n");
for(i=0;
i<3; i++)
{
for(j=0;
j<3; j++)
{
printf("\t\t%d",arr[j][i]);
}
printf("\n\n");
}
getch();
}
Output:
Ex 19: Write
a program to find the inverse of a Matrix.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int
AUserMatrix[3][3];
int i,j;
clrscr();
printf("\n\n\tPlease
insert matrix:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n\tMatrix[%d][%d]=",
i,j);
scanf("%d",
&AUserMatrix[i][j]);
}
}
printf("\n\t\t
Given matrix is :\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t\t%d",AUserMatrix[i][j]);
}
printf("\n");
}
getch();
}
Output:
Session
6
Ex
20: Using
Recursion, Reverse ‘n’ Characters.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void reverse(char chrAParam[],int intParamLen)
{
if(intParamLen>-1)
{
printf("%c",
chrAParam[intParamLen]);
intParamLen=intParamLen-1;
reverse(chrAParam,
intParamLen);
}
}
void main()
{
char
chrAUserInput[50];
clrscr();
printf("\n\n\t\tPlease
insert string : ");
gets(chrAUserInput);
printf("\n\n\t\t
Reverse String is : ");
reverse(chrAUserInput,strlen(chrAUserInput));
getch();
}
Output:
Session
7
Ex
21: Write
a program to convert a given lowercase string to upper case string without
using the inbuilt string function.
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()
{
char ChrUserLcase[100];
int i ;
clrscr();
printf ("\t\n Plese insert String in Lower Case : ->
");
gets(ChrUserLcase) ;
printf ("\t\n Converted Uper Case String : ->
");
for (i = 0 ; i <=strlen(ChrUserLcase)-1 ; i++)
{
if (ChrUserLcase[i]>=97 &&
ChrUserLcase[i]<=122 )
{
printf ("%c", ChrUserLcase[i]-32) ;
}
else
{
printf ("%c", ChrUserLcase[i]);
}
}
getch() ;
}
Output:
Ex 22: Write
a program to count number of vowels, consonants & spaces in a given string.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char
name[20];
int
i,space=0,vowel=0,conso=0;
clrscr();
printf("\n\tEnter
Name -> ");
gets(name);
for(i=0;
name[i]!=NULL; i++)
{
if(name[i]=='
')
{
space++;
}
else
if(name[i]=='a'|| name[i]=='A'|| name[i]=='e'|| name[i]
=='E'||
name[i]=='i'|| name[i]=='I'|| name[i]=='o'
||
name[i]=='O' || name[i]=='u' || name[i]=='U')
{
vowel++;
}
else
{
conso++;
}
}
printf("\n\n\tYour
Total Space is %d",space);
printf("\n\n\tYour
Total Vowel is %d",vowel);
printf("\n\n\tYour
Total Conso. is %d",conso);
getch();
}
Output:
Ex
23: Write a program to input a string and output the reversed
string, i.e. if "USF" is input, the program has to output
"FSU". You are not to use array notation to access the characters,
instead please use pointer notation.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char
name[20],*p;
int
i,j=0,k;
clrscr();
printf("\n\tEnter
Any String -> ");
gets(name);
for(i=0;
name[i]!=NULL; i++)
{
j++;
}
p=&name[j-1];
printf("\n\n\t\t");
for(i=j-1;
i>=0; i--)
{
printf("%c",*p);
p--;
}
getch();
}
Output:
Session
8
Ex
24: Write
a program to process the students-evolution records using structures.
Code:
#include<stdio.h>
#include<conio.h>
struct student
{
char
r_no[10],name[20];
int h,e,m;
}i;
void main()
{
clrscr();
printf("\n\tEnter
Your Roll_No -> ");
gets(i.r_no);
printf("\n\tEnter
Your Name -> ");
gets(i.name);
printf("\n\tEnter
Your Marks In Hindi -> ");
scanf("%d",&i.h);
printf("\n\tEnter
Your marks In English -> ");
scanf("%d",&i.e);
printf("\n\tEnter
Your marks In Maths -> ");
scanf("%d",&i.m);
printf("\n\n\t\tYour
R_no is %s",i.r_no);
printf("\n\n\t\tYour
Name is %s",i.name);
printf("\n\n\t\tYour
Marks in Hindi %d",i.h);
printf("\n\n\t\tYour
Marks in English %d",i.e);
printf("\n\n\t\tYour
Marks in maths %d",i.m);
getch();
}
Output:
Ex
25: Define a
structure that will hold the data for a complex number. Using this structure,
please write a program that will input two complex numbers and output the multiple
of two complex numbers. Use double variables to represent complex number
components.
Code:
#include<stdio.h>
#include<conio.h>
typedef struct
{
double rl;
double im;
}
complex;
void main()
{
complex a,b,c;
clrscr();
printf("\n\n Enter the first complex No.:-> ");
scanf("%lf%lf",&a.rl,&a.im);
printf("\n Enter the second complex No.:-> ");
scanf("%lf%lf",&b.rl,&b.im);
c.rl=(a.rl*b.rl)-(a.im*b.im);
c.im=(a.rl*b.im)+(b.rl*a.im);
printf("\n The multiple of the two complex numbers
are:=> %.2lf\+%.2lfi",c.rl,c.im);
}
Output:
Session
9
Ex
27: Write
a function that will return the length of a character string. You are not allowed
to use the strlen C library function.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char
name[20],*p;
int i,j=0,k;
clrscr();
printf("\n\tEnter
any String -> ");
gets(name);
p=&name[0];
printf("\n\t");
for(i=0;
name[i]!=NULL; i++)
{
printf("
%c",*p);
p++;
j++;
}
printf("\n\n\n\t\tYour
String Length is %d",j);
getch();
}
Output:
Ex
29: Write a sample program that uses this function to find the
display the minimum and the maximum values of an array of integers. Use an
array of 10 integers. You can either use scanf to input the values into that
array or initialize the array with values in the program itself.
Code:
#include<stdio.h>
#include<conio.h>
int arr[10],i,val,val1;
void input()
{
for(i=0;
i<10; i++)
{
printf("\n\tEnter
value -> ");
scanf("%d",&arr[i]);
}
clrscr();
getch();
}
void display()
{
for(i=0;
i<10; i++)
{
printf("\n\n\t\t%d",arr[i]);
}
}
void min()
{
val=arr[0];
for(i=0;
i<10; i++)
{
if(val>arr[i])
{
val=arr[i];
}
}
printf("\n\n\t\tYour
Minimum value is %d",val);
}
void max()
{
val1=arr[0];
for(i=0;
i<10; i++)
{
if(arr[i]>val1)
{
val1=arr[i];
}
}
printf("\n\n\t\tYour
Maximum value is %d",val1);
}
void main()
{
clrscr();
input();
display();
min();
max();
getch();
}\
Output:
Session
10
Ex
30: Write a
program that prompts the user the name of a file and then counts and displays the
number of bytes in the file. And create a duplicate file with the word
'.backup' appended to the file name. Please check whether file was successfully
opened, and display an error message,if not.
Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
int
IntFileSize=0,i;
char
chUserFile[20],c;
FILE*F1User,*F1Bak;
clrscr();
printf("\n
Please insert Filename & Extansion -> ");
scanf("%s",
chUserFile);
F1User=fopen(chUserFile,"r");
if(F1User==NULL)
{
printf("File
does not exist or File I/O Error");
getch();
exit(0);
}
strcat(chUserFile,".backup");
F1Bak=fopen(chUserFile,"w");
if(F1User==NULL)
{
printf("File
I/O Error!");
getch();
exit(0);
}
while((c=getc(F1User))!=EOF)
{
putc(c,F1Bak);
IntFileSize++;
}
printf("\n
file is %d bytes", IntFileSize);
fclose(F1User);
fclose(F1Bak);
getch();
}
Output:
Ex 31: Write
a program to create a file, open it, type-in some character and count the no.
of char. in file.
Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
int
IntFileSize=0;
FILE
*FIUser;
char
chUserFile[20];
char
chUserInput[100];
clrscr();
printf("\nPlease
Insert File to Create:-> ");
gets(chUserFile);
FIUser=fopen(chUserFile,"w");
if(FIUser==NULL)
{
printf("File
Creation Error");
getch();
exit(0);
}
printf("\n
Please Insert Character into File:=> ");
gets(chUserInput);
fputs(chUserInput,FIUser);
fclose(FIUser);
FIUser=fopen(chUserFile,"r");
while(getc(FIUser)!=EOF)
{
IntFileSize++;
}
printf("\n
size of file is %d",IntFileSize);
fclose(FIUser);
getch();
}
Output:
Ex 32: Write
a program that will input a person's first name,last name, SSN number and age
and write the information to a data file. One person's information should be in
a single line. Use the function fprintf to write to the data file. Accept the
information & write the data within
a loop. Your program should exit the loop when the word 'EXIT' is entered for
the first name. Remember to close the file before terminating the program.
Code:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
struct Datastru{
char fname[20] ;
char lname[20] ;
int SSNno ;
int age ;
}
main ()
{
struct Datastru StPerson ;
FILE *FlUser;
char chUserFile[20] ;
char chUserInput[100] ;
int i;
printf("\nPlease Insert data File to Create:->
") ;
scanf("%s",chUserFile);
FlUser = fopen(chUserFile,"w");
if(FlUser == NULL)
{
printf("File Creation Error");
getch() ;
exit(0);
}
while (strcmp(StPerson.fname,"EXIT") != 0 )
{
printf("\nPlease Insert person's First Name :->
") ;
scanf("%s",&StPerson.fname);
if(strcmp(StPerson.fname,"EXIT") == 0 )
{
fclose(FlUser) ;
exit(0);
}
fprintf(FlUser,"%s",&StPerson.fname) ;
printf("\nPlease Insert person's Last Name :->
") ;
scanf("%s",&StPerson.lname);
fprintf(FlUser,"%s",&StPerson.lname) ;
printf("\nPlease Insert person's Age :-> ") ;
scanf("%d",&StPerson.age);
fprintf(FlUser,"%d",StPerson.age) ;
printf("\nPlease Insert person's SSN No :-> ")
;
scanf("%d",&StPerson.SSNno);
fprintf(FlUser,"%d",StPerson.SSNno) ;
fprintf(FlUser,"\n") ;
}
fclose(FlUser) ;
}
Output:
0 comments:
Post a Comment