44607774 c programming lab manual
TRANSCRIPT
-
7/28/2019 44607774 C Programming Lab Manual
1/46
Exercise 1:
Solving problems such as temperature conversion, student grading, income taxcalculation, etc., which expose students to use basic C operators
Temperature conversion
Algorithm:
Step1: start
Step2: read Fahrenheit value
Step3: c=f-32*5/9
Step4: print/write c
Step5: stop
Program:
#include
main( ) {
float f, c;
clrscr( );
printf(enter Fahrenheit value);
scanf(%f,&f);
c=f-32*5/9;
printf(centigrade value is %f,c);
getch( ); }
Student grading
Algorithm:
Step1:start
Step2:read marks of the student
m1,m2,m3
step3: if m1>30 and m2>30 and m3>30
Step4:add the marks of the studentm=m1+m2+m3..
Step5:find the percentage of the marks
p=(m/(n*100))*100
Step6:if percentage is in between 70 to 100
p=70
Step7:print / write first class
-
7/28/2019 44607774 C Programming Lab Manual
2/46
Step8:if percentage is in between 50 to 69
p=50
Step9:write second class
Step10:if percentage 40 to 49
p=40
Step11:write third class
Step12:if percentage 30 && m2>30 && m3>30)
{
sum=m1+m2+m3;p=sum/300*100;
if(p=70)
printf(first class);
if(p=50)
printf(second class);
if(p=40)
printf(third class);
if(p>40)
printf(pass);
}
else
printf(fail);
}
-
7/28/2019 44607774 C Programming Lab Manual
3/46
Exercise 2
2s complement of a number is obtained by scanning it from right to left and
complementing all the bits after the first appearance of a 1. Thus 2s complement of
11100 is 00100. Write a C program to find the 2s complement of a binary number.
Algorithm:
Step1:start
Step2:read binary value
Step3:if binary value is 1
Step4:if next value is 1 change to 0
Step5:if next value is 0 change to 1
Step6:stop
Program:
#include
#include
main()
{
int a[20],n,i,count=0,j,k,l;
clrscr();
printf("enter number of digits in binary value");
scanf("%d",&n);
for(i=0;i=1)
{
if(a[j-1]==0)
-
7/28/2019 44607774 C Programming Lab Manual
4/46
a[j-1]=1;
else
a[j-1]=0;
}
}
printf("\nafter 2's complement ");
for(k=0;k0
Step4:r=n%10
Step5:sum=sum+r
Step6: n=n/10
Step7:print/write sum
Step8:stop
Program:
#include
#include
main()
-
7/28/2019 44607774 C Programming Lab Manual
5/46
{
int n,sum=0,r;
clrscr();
printf("enter n value");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("%d",sum);
getch();
}
b)
Algorithm:
Step1:start
Step2:read n value
Step3:f1=0,f2=1
Step4: for i=0 to n
Step5:f3=f1+f2
Step6:print f3
Step7:f1=f2
Step8:f2=f3
Step9:print f3
Step10:stop
Program:
#include
void main(){
int i,f1=0,f2=1,f3,n;
clrscr();
printf("enter n value");
scanf("%d",&n);
printf("\n %d \n %d",f1,f2);
-
7/28/2019 44607774 C Programming Lab Manual
6/46
for(i=3;i
-
7/28/2019 44607774 C Programming Lab Manual
7/46
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
}
getch();
}
d)
Algorithm:
Step1:startStep2:read n value
Step3:f1=0,f2=1
Step4:for i 0 to n
Step5:f3=f1+f2
Step6:if f3=n
Step7:Count+1
Step8:f1=f2,f2=f3
Step9:if count=1
Print Fibonacci number
Step10:if count!=1
Print not a Fibonacci number
Program:
#include
void main()
{
int n,count=0,f1=0,f2=1,f3,i;
clrscr();printf("enter n value");
scanf("%d",&n);
for(i=0;i
-
7/28/2019 44607774 C Programming Lab Manual
8/46
{
count++;
break;
}
f1=f2;
f2=f3;
}
if(count==1)
printf("%d is fibinacci number",n);
else
printf("%d is not fibonacci number",n);
getch();
}
Exercise 4
a) Write a C program to calculate the following Sum:
Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!b) Write a C program to find the roots of a quadratic equation.
a)
Algorithm:
Step1:start
Step2:enter x value
Step3:for count ,power 0 to 10
Step4:fact=1
Step5:for fcoun power to 1
Step6: fact =fact* fcoun;
Step7:sum=sum+(-1)counter * xpower /fact
Step8:print sum
Step9:stop
Program:
#include
#include
void main()
{
int counter,f_coun;
-
7/28/2019 44607774 C Programming Lab Manual
9/46
float sum=0,x,power,fact;
clrscr();
printf("");
printf("\n\n\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! -
X^10/10!");
printf("\n\n\n\tENTER VALUE OF X : ");
scanf("%f",&x);
for(counter=0, power=0; power=1; fcoun--)
fact *= fcoun;
//EQ. FOR SUM SERIES
sum=sum+(pow(-1,counter)*(pow(x,power)/fact));
}
printf("SUM : %f",sum);
getch();
}
b)
Algorithm:
Step1:start
Step2:enter a ,b,c
Step3:d=b*b-4*a*c
Step4:if d>0
Step5:r1=-b+sqrt(b*b-4*a*c)/(2*a)
Step6:r2=-b-sqrt(b*b-4*a*c)/(2*a)
Step7:print r1,r2
Step8:if d=0Step9:r1=r2=-b/2*a
Step10:if d
-
7/28/2019 44607774 C Programming Lab Manual
10/46
Program:
#include
main()
{
float a,b,c,r1,r2;
int d;
clrscr();
printf("enter a,b,c values");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=-b+sqrt(b*b-4*a*c)/(2*a);
r2=-b-sqrt(b*b-4*a*c)/(2*a);printf("roots are \n");
printf("\n r1=%f",r1);
printf("\n r2=%f",r2);
}
else if(d==0)
{
r1=r2=-b/(2*a);
printf("roots are \n");
printf("\n r1=%f",r1);
printf("\n r2=%f",r2);
}
else
printf("\n roots are imaginary");
getch();
}
Exercise 5
a) The total distance travelled by vehicle int seconds is given by distance = ut+1/2at
2
where u and a are the initial velocity (m/sec.) and acceleration (m/sec2). Write C
program to find the distance travelled at regular intervals of time given the values of u
and a. The program should provide the flexibility to the user to select his own timeintervals and repeat the calculations for different values of u and a.
b) Write a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result. (Consider the operators +,-,*, /, % anduse Switch Statement)
-
7/28/2019 44607774 C Programming Lab Manual
11/46
a)
Algorithm:
Step1:start
Step2:read number of time intervals
Step3:for counter 1 to time interval
Step4:read time, velocity ,acceleration
Step5: distance = velocity*time + (acceleration*time*time)/2
Step6:stop
Program:
#include
#include
void main(){
int tim_intrval, counter,time;
float accl, distance=0, velos;
clrscr();
printf("");
printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");
scanf("%d",&tim_intrval);
for(counter = 1; counter
-
7/28/2019 44607774 C Programming Lab Manual
12/46
b)
Algorithm:
Step1:start
Step2:read a,b values and read one operator
Step3: case +: c=a+b addition is performed ,print c
Step4: case -: c=a-b subtraction is performed ,print c
Step5:case *: c=a*b multiplication is performed ,print c
Step6:case %: c=a%b modulus is performed ,print c
Step7:case /: c=a/b division is performed ,print c
Step8:stop
Program:
#include
#include
main( )
{
float a,b,c;
int c1;
char op;
clrscr( );
printf("enter a,b,op values");
scanf("%f%f",&a,&b);
op=getche( );
switch(op)
{
case '+':
c=a+b;
printf("addition value is %f",c);
break;
case '-':
c=a-b;printf("subtraction value is %f",c);
break;
case '*':
c=a*b;
printf("multiplication value is %f",c);
break;
-
7/28/2019 44607774 C Programming Lab Manual
13/46
case '%':
c1=(int)a%(int)b;
printf("remainder value is %d",c1);
break;
case '/':
c=a/b;
printf("division value is %f",c);
break;
default:
printf("not valid");
break;
}
getch( );
}
Exercise 6
a) Simple programming examples to manipulate strings.b) Verifying a string for its palindrome property
Algorithm:
step1:startstep2:read two strings string1,string2
step3:read n value
step4: case1:concat two strings
print string
step5: case2:compare two strings
print the value
step6: case3:copy the string2 to string1
print the string1
step7: case 4:length of the string
print the length
step8: case 0:exit
step9: stop
-
7/28/2019 44607774 C Programming Lab Manual
14/46
Program:
#include
#include
main()
{
int i,c;
char string1[50],string2[50],string3[50];
clrscr();
printf("\n1.string concatination \n2.compare the strings \n3.copy the string \n
4.find the length of the string \n 0.exit\n" );
printf("\n enter your choice");
scanf("%d",&i);
switch(i)
{case 1:
printf("enter two strings");
scanf("%s%s",&string1,&string2);
strcat(string1,string2);
printf("%s",string1);
break;
case 2:
printf("enter two strings");
scanf("%s%s",&string1,&string2);
c=strcmp(string1,string2);
printf("%d",c);
break;
case 3:
printf("enter two strings");
scanf("%s%s",&string1,&string2);
strcpy(string1,string2);
printf("%s",string1);
break;
case 4:printf("enter the string");
scanf("%s",string1);
c=strlen(string1);
printf("%d",c);
break;
case 0:
-
7/28/2019 44607774 C Programming Lab Manual
15/46
exit(0);
}
getch();
}
b)
Algorithm:
step1:start
step2:read the string string1
step3:find the reverse of the string string2
step4:if string1 is equal to string2
step5:print string1 is palidrome or notstep6:stop
Program:
#include
#include
enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
int left,right,len=strlen(string);
enum Boolean matched=true;
if(len==0)
return 0;
left=0;
right=len-1;
/* Compare the first and last letter,second & second last & so on */
while(left
-
7/28/2019 44607774 C Programming Lab Manual
16/46
right--;
}
}
return matched;
}
int main()
{
char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;
}
Exercise 7
Write a C program that uses functions to perform the following operations:
i. To insert a sub-string in to given main string from a given position.ii. To delete n Characters from a given position in a given string.
iii. To replace a character of string either from beginning or ending or at a
specified location
Algorithm:
1. start
2. Read strings first and second.
3. do
4. begin
5. if u select one of them.
6. i.1 means insert string
-
7/28/2019 44607774 C Programming Lab Manual
17/46
7. ii. 2 means delete string.
8. iii.3 means replacing character
9. read integer value
10.switch
11.Begin
12.case 1:
13.Call insert function
14.Break;
15.Case 2:
16.Call delete function
17.Break;
18.Case 3:
19.Call replacing character function
20.Default:21.Invalid choice
22.If u want to continue read character
23.End of while loop
24.Stop.
25.Procedure for insert function
26.initialize i=0
27.read position value for where insert substring into main string
28.while i
-
7/28/2019 44607774 C Programming Lab Manual
18/46
30.c[i]=first[i];
31.i++;
32.end of while
33.for j=0 to ending of second string
34.c[i++]=second[j];
35.for j=pos to ending of first string
36.c[i]=first[j];
37.print string c
procedure for delete function
38.Initailize i=0;
39.Calculating string length of second string40.While i
-
7/28/2019 44607774 C Programming Lab Manual
19/46
procedure for replacing character function
48.read position ,character
49.first[position]=character
50.print string c
Program:
#include
#include
#include
void main(){
int n,q,j,pos;
char first[20],second[20];
char ch;
clrscr();
printf("enter first string");
gets(first);
printf("enter second string");
gets(second);
do
{
printf("if u choose one of them ");
printf("1.insert substring\n2.delete character\n3.replacing character");
scanf("%d",&q);
switch(q)
{
case 1:
insert(first,second);break;
case 2:
delete(second);
break;
case 3:
-
7/28/2019 44607774 C Programming Lab Manual
20/46
replace(first);
break;
}
printf("u want to continue (y/n):");
ch=getche();
}while(ch=='y');
getch();
}
insert(char *first,char *second)
{
int m,q,i,j,pos;
char c[20];
i=0;printf("enter given position");
scanf("%d",&pos);
m=strlen(second);
q=strlen(first);
while(i
-
7/28/2019 44607774 C Programming Lab Manual
21/46
printf("enter how many characters to delete");
scanf("%d",&n);
i=0;
m=strlen(second);
while(i
-
7/28/2019 44607774 C Programming Lab Manual
22/46
Algorithm:
1. Start
2. Call procedure Read_complex_numbers()
3. Call procedure write_complex_numbers()
4. Call procedure Add_complex_numbers()
5. Call procedure Multiply_complex_numbers()
6. Stop
Algorithm for Read Complex numbers
1. Start
2. Read real part and inmaginary part of first complex number
3. Read real part and inmaginary part of Second complex number
4. Stop
Algorithm for Write Complex numbers
1. Start
2. Write real part and inmaginary part of first complex number
3. Write real part and inmaginary part of Second complex number
4. Stop
Algorithm for Add Complex numbers
1. Start
2. Result.real_part=number1.real_part +number2.real_part
3. Result.imaginary_part=number1.imaginary_part
+number2.imaginary_part
4. Stop
Algorithm for Write Complex numbers
1. Start
2. Result.real_part=(number1.real_part *number2.real_part)
(number1.imaginary_part *number2.imaginary_part)
3. Result.imaginary_part=(number1.real_part *number2.imaginary_part) +
(number2.real_part *number1.imaginary_part)
-
7/28/2019 44607774 C Programming Lab Manual
23/46
4.Stop
Program:
#include
#include
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 :
EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();}
}
void arithmetic(int opern)
{
-
7/28/2019 44607774 C Programming Lab Manual
24/46
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First
Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
main();
}
Exercise 9
a) Addition of Two Matrices
b) Calculating transpose of a matrix in-place manner.c) Matrix multiplication by checking compatibility
a)
-
7/28/2019 44607774 C Programming Lab Manual
25/46
Algorithm:
Matrix Addition:
1. Start the procedure
2. read no. of rows and columns of the matrix A i.e m,n
3. read no. of rows and columns of the matrix B i.e p,q
4. if m!=p or n!=q then print matrix addition is not possible and goto
step (15)
5. else go to next step
6. read matrix A and matrix B
7. for i=0 to m-1 do
8. begin
9. for j=0 to n-1 do10.begin
11.C[i][j]= A[i][j]+B[i][j];
12.end
13.end
14.print the resultant matrix C.
15. Stop
Matrix Transpose:
1. start the procedure
2. read the size of the matrix A i.e. m,n
3. take a resultant matrix B.
4. for i=0 to m-1 do
5. begin
6. for j=0 to n-1 do
7. begin
8. read element a[i][j]
9. end
10.end11.for i=0 to m-1 do
12.begin
13.for j=0 to n-1 do
14.begin
15.b[j][i]=a[i][j];
16.end
-
7/28/2019 44607774 C Programming Lab Manual
26/46
17.end
18.print the resultant matrix B which is the transpose of the given matrix
A.
19.stop.
Matrix Multiplication:
1. start the procedure
2. read no. of rows and columns of the matrix A i.e m,n
3. read no. of rows and columns of the matrix B i.e p,q
4. if n!=p then print matrix multiplication is not possible and goto step
(15)
5. else go to next step
6. read matrix A and matrix B
7. for i=0 to m-1 do8. begin
9. for j=0 to n-1 do
10.begin
11.for k=0 to q-1 do
12.begin
13.C[i][j]= c[i][j]+A[i][k]*B[k][j];
14.end
15.end
16.print the resultant matrix C.
17.stop
Program: (Addition & Multiplication)
#include
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();printf("************************************");
printf("\n\t\tMENU");
printf("\n**********************************");
printf("\n[1]ADDITION OF TWO MATRICES");
printf("\n[2]MULTIPLICATION OF TWO MATRICES");
printf("\n[0]EXIT");
-
7/28/2019 44607774 C Programming Lab Manual
27/46
printf("\n**********************************");
printf("\n\tEnter your choice:\n");
scanf("%d",&ch);
if(ch0)
{
printf("Valid Choice\n");
}
switch(ch)
{
case 1:
printf("Input rows and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix A:\n");
for(i=0;i
-
7/28/2019 44607774 C Programming Lab Manual
28/46
printf("resultant matrix is %d*%d\n",m,q);
printf("Input A matrix\n");
read_matrix(a,m,n);
printf("Input B matrix\n");
/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two matrices*/
printf("\n =====Matrix Multiplication=====\n");
for(i=0;i
-
7/28/2019 44607774 C Programming Lab Manual
29/46
return 0;
}
/*Function to write the matrix*/
int write_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i
-
7/28/2019 44607774 C Programming Lab Manual
30/46
for(j=0;j
-
7/28/2019 44607774 C Programming Lab Manual
31/46
6. Stop
return f
Alogrithm: (Recursive)
1. Start the procedure
2. read a number
3. fact= factorial(n)
4. print fact
5. Stop
Algorithm for factorial():
1. start the procedure
2. if n==0 return 13. else return n*factorial(n-1)
4. Stop
Program:
#include
#include
unsigned int recr_factorial(int n);
unsigned int iter_factorial(int n);
void main(){
int n,i;
long fact;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
if(n==0)
printf("Factorial of 0 is 1\n");
else
{printf("Factorial of %d Using Recursive Function is
%d\n",n,recr_factorial(n));
printf("Factorial of %d Using Non-Recursive Function is
%d\n",n,iter_factorial(n));
}
getch();
-
7/28/2019 44607774 C Programming Lab Manual
32/46
}
/* Recursive Function*/
unsigned int recr_factorial(int n) {
return n>=1 ? n * recr_factorial(n-1) : 1;
}
/* Non-Recursive Function*/
unsigned int iter_factorial(int n) {
int accu = 1;
int i;
for(i = 1; i
-
7/28/2019 44607774 C Programming Lab Manual
33/46
Program:
#include
#include
#include
unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);
int main(void)
{
int a,b,iGcd;
clrscr();
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d Using Recursive Function is%d\n",a,b,GcdRecursive(a,b));
printf("GCD of %d and %d Using Non-Recursive Function is
%d\n",a,b,GcdNonRecursive(a,b));
getch();
}
/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;
else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q){
unsigned remainder;
remainder = p-(p/q*q);if(remainder==0)
return q;
else
GcdRecursive(q,remainder);}
a) iii)
-
7/28/2019 44607774 C Programming Lab Manual
34/46
Algorithm:(Recursive)
1. Start
2. Initialize sour,temp,dest
3. Read n value
4. Call procedure hanoi
5. End
Procedure for hanoi
1. If(n!=0) then
2. Call procedure hanoi
3. Print n,sour and dest values
4. Call procedure hanoi
5. End if
Program: (Recursive)
#include
void main()
{
int n;
char sour='A',temp='B',dest='C';
clrscr();
printf("enter n value");
scanf("%d",&n);
hania(n,sour,temp,dest);
getch();
}
hania(int n,int sour,int temp,int dest)
{
if(n!=0)
-
7/28/2019 44607774 C Programming Lab Manual
35/46
{
hania(n-1,sour,dest,temp);
printf("\n%d disks move from %c to %c",n,sour,dest);
hania(n-1,temp,sour,dest);
}
}
Exercise 11
a) Write a C functions to find both the largest and smallest number of an array of
integers.b) Write a C function that uses functions to perform the following:
i) that displays the position/ index in the string S where the string T begins, or 1
if S doesnt contain T.
ii) to count the lines, words and characters in a given text.
a)
Algorithm:
Algorithm MIN_MAX:
1. Start
2. Read the elements of array a[]
3. large=largest(a,n)
4. small=smallest(a,n)5. print large
6. print small
7. Stop
Algorithm largest:
1. Start
2. max=a[0]
3. for i=1 to n-1 do4. if(max
-
7/28/2019 44607774 C Programming Lab Manual
36/46
1. Start
2. min=a[0]
3. for i=1 to n-1 do
4. if(a[i]
-
7/28/2019 44607774 C Programming Lab Manual
37/46
for(i=1;i
-
7/28/2019 44607774 C Programming Lab Manual
38/46
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}
b)ii)
Algorithm:
1. Start
2. Initialize variables
3. While end=0 do
4. Initialize c-0
5. While read characters until \n then goto step 6
6. Line[c++]=ctr
7. Line [c]=\0
8. If line=0 then goto step 9
9. Break
10.Else
11.Words++
12.For i=0 to \0
13.If line=null or \t then
14.Words++
15.End else
16.Lines=lines+1
17.Characters=characters+strlen(line)
-
7/28/2019 44607774 C Programming Lab Manual
39/46
18.End while
19.Print characters ,words and lines
20.end
Program:
#include
main()
{
char line[81], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf("KEY IN THE TEXT.\n");
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");
while( end == 0)
{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != '\n')line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
-
7/28/2019 44607774 C Programming Lab Manual
40/46
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
}
Exercise 12
a) Write a C function to generate Pascals triangle.b) Write a C function to construct a pyramid of numbers.
a)
Algorithm:
1. Start
2. Initialize q=0 and bin
3. Read n value
4. While (q
-
7/28/2019 44607774 C Programming Lab Manual
41/46
16.Incremetnt q by 1
17.End while
18.Stop
Program:
#include
#include
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q0;--p)
printf(" ");
for(x=0;x
-
7/28/2019 44607774 C Programming Lab Manual
42/46
1. Start
2. Initialize x=35
3. Read number
4. For y=0 to num then
5. Goto x and y location
6. For i=0 to y then goto step 7
7. Print abs(i)
8. X=x-3
9. End for
10.Stop
Program:
#include
#include
void main(){
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y
-
7/28/2019 44607774 C Programming Lab Manual
43/46
Exercise 13
Write a C function to read in two numbers, x and n, and then compute the sum of thisgeometric progression:
1+x+x2+x3+.+xn
Write a C function to read in two numbers, x and n(no. of terms), and then computesin(x) and cos(x).
Algorithm:
1. Start
2. Read x and n
3. s_sum=1
4. for i=1 to n do
5. s_sum=s_sum+pow(x,i)
6. print s_sum7. Stop
Program:
#include
#include
#include
void main()
{
int s_sum,i,x,n;
clrscr();
printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);
if(n
-
7/28/2019 44607774 C Programming Lab Manual
44/46
printf("Sum of series=%d\n",s_sum);
}
getch();
}
Exercise 16
a) Write a C program which copies one file to another.b) Write a C program to reverse the first n characters in a file. (Note: The file name and n
are specified on the command line)
a)
Algorithm:
1. start the procedure2. read a file name
3. open that source file in read mode
4. if the file name not exists then print file does not exists and go to
step 9.
5. else go to next step
6. open a destination file in a write mode.
7. repeat
8. read each character from source file and copy that character into
destination file9. until the character read is End of the File character(EOF)
10.print the destination file
11.stop the procedure
Program:
#include
#include
#include
void main(int argc, char *argv[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(argc!=3)
-
7/28/2019 44607774 C Programming Lab Manual
45/46
{
puts("Invalid number of arguments.");
exit(0);
}
fs = fopen(argv[1],"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
ft = fopen(argv[2],"w");
if (ft==NULL)
{
puts("Target file cannot be opened.");
fclose(fs);exit(0);
}
while(1)
{
ch=fgetc(fs);
if (ch==EOF)
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}
b)
Program:
#include
#include
#include
#include
void main(int argc, char *argv[])
{
-
7/28/2019 44607774 C Programming Lab Manual
46/46
char a[15];
char s[20];
char n;
int k;
int j=0;
int i;
int len;
FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='\0';
getch();
}