Course objectives


Draw the flow chart and write a Recursive C function to find the factorial of number n! defined



Download 0.68 Mb.
Page4/5
Date05.08.2017
Size0.68 Mb.
#26699
1   2   3   4   5

Draw the flow chart and write a Recursive C function to find the factorial of number n! defined

by fact(n)=1, if n=0, otherwise fact(n)=n*fact(n-1),using this function write a c program to compute

the binomial co-efficient nCr. Tabulate the results for different values of n and r using

suitable messages.

Algorithm:

ALGM: Factorial of number [A recursive C function to find the factorial of number n! defined

by fact(n)=1, if n=0, otherwise fact(n)=n*fact(n-1)]


FUNCTION FACTORIAL (N: INTEGER): INTEGER

(* RECURSIVE COMPUTATION OF N FACTORIAL *)


BEGIN

(* TEST FOR STOPPING STATE *)

IF N <= 0 THEN

FACTORIAL := 1

ELSE

FACTORIAL := N * FACTORIAL(N - 1)



END; (* FACTORIAL *)

Return 1


Return fact(n-1)

stop


start

Enter n, r

res = fact(n)/(fact(n-r)*fact(r))

If n<=0


Flowchart


T F



Program :

#include

#include

int fact(int n)

{

if(n==0)

{

return 1;

}

return (n*fact(n-1));

}

int main()

{

int n,r,res;

clrscr();

printf("Enter the value of n and r\n");

scanf("%d%d",&n,&r);

res=fact(n)/(fact(n-r)*fact(r));

printf("The NCR is = %d",res);

getch();

}
Output:

  1. Enter the value of n and r

4 2

The NCR is =6



  1. Enter the value of n and

7 3

The NCR is =6



12. Copy the Contents of File
Given two university information files "studentname.txt" and "usn.txt" that contains students names

and USN respectively. Write a C program to a new file called "output.txt" and copy the content of

files "studentname.txt" and "usn.txt" into output file in the sequence shows below. Display the content

of output file "output.txt" on to the screen.

Note : Students are required to create two files “Studname.txt” and “Studusn.txt” with the Contents

studname.txt studusn.txt

Amar 1RN10CS005

Suresh 1RN10CS012

Kiran 1RN10CS036

Shashi 1RN10CS072

ALGORITHM:

Input : Create the file fp1,fp2,fp3

Output : Print whether the files are found or not

step1:start

Step2: create the files (fp1,fp2, & fp3)

Step3: while(!feof((fp1)&&!feof(fp2))

Step4:Both files the not created the print the file not found

a). if only one file is created then print files not found.

b).if both files are created the print files are found.

Step5: stop


FLOWCHART:

start


Create fp1, fp2, fp3

While(!feof(fp1) &&

!feof(fp2))

File not created

File not found

File found

stop

Both the files are not Both files only one file is



Created are created created

Program

/* Program on merge two files */

#include

#incude

int main()

{

FILE *fp1,*fp2,*fp3;

char usn[20], name[20];

clrscr();

fp1=fopen("studname.txt", "r");

if(fp1 == NULL)

printf(" File not found");

fp2=fopen("studusn.txt", "r");

if(fp2 == NULL)

printf(" File not found");

fp3=fopen("output.txt","w");

while( !feof(fp1) && !feof(fp2) )

{

fscanf(fp1,"%s",name);

fscanf(fp2,"%s",usn);

fprintf(fp3,"%15s %10s\n", name, usn);

}

fclose(fp1);

fclose(fp2);

fclose(fp3);

fp3=fopen("output.txt","r");

printf("\n----------------------------\n");

printf(" Name USN \n");

printf("----------------------------\n");

while(!feof(fp3))

{

fscanf(fp3,"%s",name);

fscanf(fp3,"%s \n",usn);

printf("%-15s %10s \n", name,usn);
}

fclose(fp3);

getch();

}
Output:

Name USN

Amar 1RN10CS005

Suresh 1RN10CS012

Kiran 1RN10CS036

Shashi 1RN10CS072
13. Student Record using Array of Structures
Write a C program to maintain a record of "n" student details using an array of structures with four fields (Roll number, Name, marks, and Grade). Each field is of an appropriate data type. Print the marks of the student given name as input.
ALGORITHM:
Input: Enter the number of students

Output: print the marks of the student.
Step1: start

Step2: enter the num of students

Step3: if(strcmp(s[i].name, sname==0)

Then


{

Print the marks of students name & USN

}

Else


{

Print the given date is invaid

}

Step4:stop



FLOWCHART:

start


stop

Enter n


Enter the details of 3 students

Enter the details of 2 students


Enter the details of 3 students but it will not display
If(strcmp(s[i].name, sname)==0)

N=3 N=2 N=3



Program:

/* program to maintain a record of student using structrue */

#include

#include

struct student

{

int rollno, marks;

char name[20], grade;

};

Void main()

{

int i,n,found=0;

struct student s[10];

char sname[20];

clrscr();

printf("Enter the number of student details n=");

scanf("%d",&n);

for(i=0;i

{

printf("\nenter the %d student details \n",i+1);

printf("enter the roll number:");

scanf("%d",&s[i].rollno);

printf("enter the student name without white spaces:");

scanf("%s", s[i].name);

printf("enter the marks : ");

scanf("%d", &s[i].marks);

printf("enter the grade : ");

fflush(stdin);

scanf("%c",&s[i].grade);

}

printf("\nStudent details are \n");

printf("\nRollno\tName\t\t\tMarks\tGrade\n");

for(i=0;i

printf("%d\t%s\t\t%d\t%c\n", s[i].rollno, s[i].name, s[i].marks, s[i].grade);

printf("\nEnter the student name to print the marks:");

scanf("%s", sname);

for(i=0;i

{

if(strcmp(s[i].name,sname)==0)

{

Printf(“\Marks of the student is : %d”,s[i].marks);

found=1;

}

}

if(found ==0)

printf(“ Given student name not found\n”);

getch();

}
Output:
Enter the number of student details n=3

enter the 1 student details

enter the roll number: 100

enter the student name without white spaces:vishwa

enter the marks : 90

enter the grade : A


enter the 2 student details

enter the roll number: 101

enter the student name without white spaces:madhu

enter the marks : 50

enter the grade : B
enter the 3 student details

enter the roll number: 102

enter the student name without white spaces:kiran

enter the marks : 45

enter the grade : C
Roll Name Marks Grad

100 vishwa 90 A

101 madhu 50 B

102 kiran 45 C

Enter the student name to print the marks: madhu

Marks of the student is : 50



14. Use of Pointers
Write a C program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of n real numbers.
Mean : is the average of the numbers. i.e the sum of a collection of numbers divided by the number of numbers in the collection

Standard deviation (SD): measures the amount of variation or dispersion from the average. For a finite set of numbers, the standard deviation is found by taking the square root of the average of the squared differences of the values from their average value.
ALGORITHM:

ALGM: COMPUTATION OF SUM, MEAN AND STANDARD DEVIATION [to compute the sum, mean and standard deviation of all elements stored in an array of n real numbers.]
Input: Enter the n values

Output: print the result for sum, mean, standard deviation
Step 1:start

Step2: Enter the value of n it may be integer or float

Step3: for(i=0; i

{

Sum=sum+*ptr;



Ptr++;

Mean=sum/n;

Ptr=a;

For(i=0;i

{

Sumstd=sumstd+pow((*ptr-mean),2);

Ptr++;

Step4:print the result for sum,mean,&stddev



Step5:stop
FLOWCHART:

start


stop

for(i=0; i

enter n

Print the result in integer(sum, mean, sumstd)


Print the result in floating point(sum, mean, sumstd)

Enter the values Enter the values

in integers in floating point

Program:

#include

#include

#include

int main()

{

float a[10], *ptr, mean, std, sum=0, sumstd=0;

int n,i;

clrscr();

printf("Enter the no of elements\n");

scanf("%d",&n);

printf("Enter the array elements\n");

for(i=0;i

{

scanf("%f",&a[i]);

}

ptr=a;

for(i=0;i

{

sum=sum+ *ptr;

ptr++;

}

mean=sum/n;

ptr=a;

for(i=0;i

{

sumstd=sumstd + pow((*ptr - mean),2);

ptr++;

}

std= sqrt(sumstd/n);

printf("Sum=%.3f\t",sum);

printf("Mean=%.3f\t",mean);

printf("Standard deviation=%.3f\t",std);

getch();

}
Output:

Enter the no of elements

5

Enter the array elements



5 3 9 8 7

Sum=32.000

Mean=6.400

Standard deviation=2.154



SAMPLE C PROGRAMS


Program to print hello world on output screen


#include


int main()


{


printf("Hello world!\n");


return 0;


}


Output:


Program to find the solution(e) for the equation e=ax +bx+c




2


#include


main()


{


int a,b,c,x,e;


printf("enter the values of a,b,c,x\n");


scanf("%d %d %d %d",&a,&b,&c,&x);


e=a*pow(x,2)+b*x+c;


printf("the answer is=%d",e);


return 0;


}


Output:


C Program to illustrate for loop(C program to print sum of n numbers)




#include


main()


{


int i,n;


float x,sum=0;avg;


printf("\n how many num");


scanf("%d",&n);


for(i=0;i<=n;i++)


{


printf("enter a num");


scanf("%f",&x);


sum+=x;


}


printf("%d",sum);


return 0;


}


Output:


Program illustrating left shift operation




#include


main()


{


long num,tempnum;


printf("enter an integer");


scanf("%ld",&num);


tempnum=num;


num=num<<2;


printf("%ld*4=%ld\n",tempnum,num);


}


Output:


Program to illustrate basic arithmetic expression




#include


main()


{


int i;


i=5;


printf("the value of i is :%d",i);


i=i+5;


printf("the value of i is :%d",i);


i=i-2;


printf("the value of i is :%d",i);


i=i*2;


prinf("the value of i is :%d",i);


i=i/4;


printf("the value of i is :%d",i);


i=i++;


printf("the value of i is :%d",i);


i=i--;


printf("the value of i is :%d",i);


return 0;


}


Output:


Program to illustrate assignment operator




#include


main()


{


int sum;


float money;


char letter;


double pi;


sum=10;


money=2.21;


letter='a';


pi=3.14;


printf("value of sum=%d\n",sum);


printf("value of money=%d\n",money);


printf("value of letter=%d\n",letter);


printf("value of pi=%d\n",pi);


getch();


return 0;


}


Output:


Program to add two numbers




#include


void main()


{


int sum,a,b;


printf("enter the values for a and b\n");


scanf("%d %d",&a,&b);


sum=a+b;


printf("%d",sum);


return;


}


Output:


Program to find area of circle




#include


#include


void main()


{


float radius,area;


clrscr();


printf("\n enter the radius of circle:");


scanf("%d",&radius);


area=3.14*radius*radius;


printf("\n area of circle :%f",area);


getch();


}


Output:


Program to illustrate do while




main()


{


int i=10;


do


{


printf("hello %d\n");


i=i-1;


}


while(i>0);


}


Output:


Program to illustrate do while




main()


{


int i=10;


do


{


printf("hello %d\n");


i=i-1;


}while(i>0);


}


Output:


WACP to add all num until user enters 0




#include


main()


{


int sum=0,num;


do


{


printf("enter a num");


scanf("%d",&num);


sum+=num;


}


while(num!=0);


printf("sum=%d",sum);


return 0;


}


Output:


Program to print integer values from 1 to n




#include


main()


{


int counter=0,n;


printf("enter how many no u want to print");


scanf("%d",&n);


counter=0;


do


{


counter++;


printf("%d",counter);


}


while(counter

return 0;


}


Output:


Program to check wether the given number is prime or not.




#include


main()


{


int n,c=2;


printf("enter a num to check if it is prime\n");


scanf("%d",&n);


for(c=2;c<=n-1;c++)


{


if(n%c==0)


{


printf("%d is not prime\n", n);


break;


}


}


if(c==n)


printf("%d is prme\n",n);


return 0;


}


Output:


Program to find the factorial of given number




#include


void main()


{


int i,fact=1,num;


printf("enter the num");


scanf("%d",&num);


if(num<=0)


fact=1;


else


{


for(i=1;i<=num;i++)


{


fact=fact*i;


}


}


printf("factorial of %d=%5d",num,fact);


}


Output:


Program to print fibonacci series




#include


void main()


{


int a,b,c,i,n;


a=0;


b=1;


printf("enter a number to define the length of fibonacci series");


scanf("%d",&n);


printf("\n the series is \n");


printf("%d\t%d",a,b);


for(i=0;i

{


c=a+b;


a=b;


b=c;


printf("%d",c);


}


getch();


}


Output:


Program to print number from 0 to 9 using for loop




#include


int main()


{


int i;


for(i=0;i<10;i++)


{


printf("%d\n",i);


}


getchar();


}


Output:


Program to add n number using for loop




#include


void main()


{


int n,sum=0,c,value;


printf("enter the numberof int to add\n");


scanf("%d",&n);


printf("enter %d integer\n",n);


for(c=1;c<=n;c++)


{


scanf("%d",&value);


sum=sum+value;


}


printf("sum of entered integers =%d\n",sum);


return 0;


}


Output:



Download 0.68 Mb.

Share with your friends:
1   2   3   4   5




The database is protected by copyright ©ininet.org 2024
send message

    Main page