Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include "stdio.h"
  2. int fact(int n)
  3. {
  4. int i, b[100];
  5. b[1]=1;
  6. if(n==1) return b[1];
  7. else {
  8. for(i=2;i<=n;i++)
  9. {
  10. b[i]=i*b[i-1];
  11. }
  12. return b[i-1];
  13. }
  14.  
  15. }
  16.  
  17. void main()
  18. {
  19. FILE *f_in=NULL;
  20. FILE *f_out=NULL;
  21.  
  22. int i,n,k,key,ans,p;
  23. float a[100];
  24.  
  25. f_in=fopen("in.txt","r");
  26. f_out=fopen("out.txt","a");
  27.  
  28. printf("Choose input. From console-1, from file-2\n");
  29. scanf("%d",&k);
  30.  
  31. if(k==1) {
  32. printf("Enter n\n");
  33. scanf("%d",&n);
  34. printf("Enter elements\n");
  35. for(i=0;i<n;i++)
  36. {
  37. scanf("%f",&a[i]);
  38. }
  39. }
  40. if(k==2){
  41. if(f_in!=NULL)
  42. {
  43. fscanf(f_in,"%d",&n);
  44. for(i=0;i<n;i++)
  45. {
  46. fscanf(f_in,"%f",&a[i]);
  47. }
  48. close(f_in);
  49. }
  50. }
  51.  
  52. printf("Choose output. To console-1, to file-2\n");
  53. scanf("%d",&key);
  54. if(key==1)
  55. {
  56. for(i=0;i<n;i++)
  57. {
  58. printf("%f ",a[i]+fact(i+1));
  59. }
  60. printf("\n");
  61. }
  62.  
  63. if(key==2)
  64. {
  65. if(f_out!=NULL)
  66. {
  67. for(i=0;i<n;i++)
  68. {
  69. fprintf(f_out,"%f ",a[i]+fact(i+1));
  70. close(f_out);
  71. }
  72. }
  73. }
  74.  
  75. if(k==1){
  76. printf("Do you want to repeat entering? Yes-1,No-0");
  77. printf("\n");
  78. scanf("%d",&ans);
  79. if(ans==1) main();
  80. }
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement