Вы находитесь на странице: 1из 5

Tutorial -I

Q. N. 1: Given a set of a student examination marks (in the range 0 to 100), make a count of number of student passed the examination, A pass is awarded for all marks of so and above. Algorithm: 1. Initialize variables 2. Enter the No. of Student, Name and marks of the student 3. Calculate total, average for all student 4. Check the pass marks and find out the pass award 5. Print the value of name, Subject marks and grade 6. Repeat step 3 to 5 till no. of student Program: struct marks { char n[10]; int m[5]; float avg[3]; int tot[3]; }; struct marks stu[3]; void main() { int i,j,n; printf("Enter the number of students\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the name of the students-%d\n",i+1); scanf("%s",stu[i].n); stu[i].tot[i]=0; for(j=0;j<5;j++) { printf("Enter the marks%d\n",j+1); scanf("%d",&stu[i].m[j]); stu[i].tot[i]+=stu[i].m[j]); } stu[i].avg[i]=(float)(stu[i].tot[i]/5); } printf("NAME\tMARK1\tMARK2\tMARK3\tMARK4\tMARK5\tTOTAL\tAVERAGE\tRESU LT\tCLASS\n"); for(i=0;i<n;i++) { printf("%s %d %d %d %d %d %d %5.2f" ,stu[i].n,stu[i].m[0],stu[i].m[1], stu[i].m[2],

stu[i].m[3], stu[i].m[4], stu[i].tot[i],stu[i].avg[i]); if(stu[i].m[0]>40 &&stu[i].m[1]>40 &&stu[i].m[2]>40 &&stu[i].m[3]>40 &&stu[i].m[4]>40) { printf("Pass"); if(stu[i].avg[i]>59) printf("First"); if(stu[i].avg[i]<60&&stu[i].avg[i]>50) printf("Second"); if(stu[i].avg[i]<50) printf("Third"); } else printf("Fail"); } } Q. N. 2: Given a number n, compute n factorial (written as n!) where n>0. Algorithm: 1. Initialize variables 2. Take a number as an input from user. 3. Starting from 1 to input number 4. Calculate fact=fact*input number 5. Repeat step 4 till input number 6. Print the value of fact variable Program: int i,n,f=1; printf("enter the number"); scanf("%d",&n); for (i=1;i<=n;i++) { f=f*i; } printf("the factorial of the number is %d",n); }

Q. N. 3: Design a algorithm to evaluate the function sin(x) defined by the infinite series expression.

Algorithm: 1. Initialize variables 2. Take a values for x and n from user. 3. Starting from 2 to input number 4. Calculate modulus of n in each step 5. If modulus is not equal to zero then calculate sum=sum+(pow(x,n)/fact) 5. Repeat step 3 to 5 till input number 6. Print the value of sum variable Program: int sum=1,i,j,x,n; Printf("enter the values for x and n"); scanf(%d %d, &x ,&n); for(i=2;i<=n;i++) { double fact=1,j=1; for(k=1;k<=j;k++) { fact*=k; j++; } int l=i/2; if(l%2!=0) { sum=sum+(pow(x,n)/fact); } else return 0; } Printf(%f,sum); } Q. N. 4: Generate and Print the first n terms of Fibonacci sequence n>1. The first terms are: 0,1,1,2,3,5,7,13 each term beyond the first two is derived for the sum of its nearest predecessor. Solution: Algorithm: 1. Initialize variables 2. Take value how many times generate series 3. Calculate the sum and increment the initial values 4. Print the value of sum 5. Repeat step 3 and 4 till input number Program: a=0; b=1;

printf(" enter n for how many times generate series"); scanf("%d",&n); printf(" FIBONACCI SERIES"); printf("%d %d",a,b); for(i=0;i<n;i++) { c=a+b; a=b; b=c; printf("%d",c); }

Q. N. 5 Design an algorithm that accepts a positive integer and reverse the order of its digits. Algorithm: 1. Initialize variables 2. Take the digit to be reversed 3. Calculate the modulus value divided by 10 and integer value divided by 10 of entered value 4. Print the modulus 5. Repeat step 3 and 4 till integer value is greater than zero Program: { int a,b,c; printf("Enter number:\t); scanf("%d",a); printf("Reversal of number: "); do { b=a%10; printf("%d",b); a = a/10; } while(a>0); } Q.N. 6: Convert a decimal integer to its corresponding octal representation. Algorithm: 1. Initialize variables 2. Enter the number to find its octal equivalent 3. Calculate the modulus value divided by 8 and integer value divided by 8 of entered value 4. Print the modulus

5. Repeat step 3 and 4 till integer value is greater than zero Program: { int n,r[10],i; printf("Enter a number to find it's octal equivalent"); scanf("%d",&n); printf("The octal equivalent of %d is ",n); for(i=0;n!=0;i++) { r[i]=n%8; n=n/8; } i--; for(;i>=0;i--) printf("%d",r[i]); }

Q. N. 7 Given character representation of integer convert to its conventional decimal format. Algorithm: 1.Define and Initialize variables 2. Enter the string number to convert in conventional decimal format 3. Calculate the ascii to integer function 4. Print the value Program: { int i; char Input [256]; printf ("Enter a number: "); gets ( Input ); i = atoi (Input); printf ("Value entered is %d); return 0; }

Вам также может понравиться