The document contains code snippets and questions related to C programming. It includes programs to:
1. Convert between octal and decimal numbers, withdraw money from an ATM, find the largest element in an array using recursion, separate even and odd numbers in an array into two arrays.
2. Determine if a matrix is sparse, calculate the sum of array elements using pointers, append the contents of one file to another file.
3. Create and update student records in a file by sorting, adding, searching, editing, and deleting records.
2. • QUESTION1. convert octal to decimal
• ANSWER:
• The Octal Number System is a type of computer and digital
numbering system which uses the Base-8 system.
• Octal numbers therefore have a range of just “8” digits, (0,
1, 2, 3, 4, 5, 6, 7) making them a Base-8 numbering system.
• Then the main characteristics of an Octal Numbering
System is that there are only 8 distinct counting digits from
0 to 7 with
• each digit having a weight or value of just 8 starting from
the least significant bit (LSB).
•
•
3. • program:
• #include<stdio.h>
• #include<math.h>
•
• int oct;
• int decimal=0;
• int a;
• int i=0;
•
• void main()
• { clrscr();
• printf("ENTER A OCTAL NUMBER:n");
• scanf("%d",&oct);
• if(oct==8 ||oct==9)
• {
4. printf("**** IT IS NOT AN OCTAL NUMBER KINDLY USE(0-7) DIGIT ONLY **** n");
exit();
}
while(oct!=0)
{ int mul =pow(8,i);
a=oct%10;
decimal+=a*mul;
i++;
// printf(" decimal digit after step %d is %d",i-1,decimal);
oct=oct/10;
}
printf(" $$$$$vREQUIRED DECIMAL EQIVALENT IS %d $$$$n",decimal);
getch();
}
5. • q2: Write an algorithm and its corresponding C program to illustrate an ATM money withdrawal operation from user’s savings’ account.
•
• code:
• #include<stdio.h>
• unsigned long AMOUNT=5000,DEPOSIT,WITHDRAW;
• int CHOICE,PIN,K,TRY=0;
• char TRANSACTION='Y';
• void main()
• {
• clrscr();
• if(PIN!= 1000)
• {
• printf("n _______WELCOME TO RBL BANK_________ n");
• printf("nENTER YOUR PIN NUMBER :");
• scanf("%d",&PIN);
•
• }
• do
• {
• clrscr();
• printf("*****WELCOME TO XYZ BANK*****n");
• printf("1.WITHDRAW CASHn");
• printf("2.QUITn");
• printf("*****************************nn");
• printf("ENTER YOUR CHOICE : ");
• scanf("%d",&CHOICE);
• switch(CHOICE)
• {
•
• case 1:
• printf("nENTER THE AMOUNT TO WITHDRAW CASHn");
• scanf("%ld",&WITHDRAW);
• if(WITHDRAW%100 != 0)
• {
• printf("nPLEASE ENTER THE AMOUNT IN MULTIPLIES OF 100n");
• }
6. • else if(WITHDRAW>(AMOUNT-1000))
• {
• printf("nINSUFFICIENT BALANCEn");
• }
• else
• {
• AMOUNT = AMOUNT-WITHDRAW;
• printf("nPLEASE COLLECT CASHn");
• printf("nYOUR CURRENT BALANCE IS %ldn",AMOUNT);
• }
• break;
•
• case 2:
• printf("nTHANK YOU FOR USING THIS ATMn");
• break;
• default :
• printf("nINVALID CHOICEn");
• }
• printf("nnDO YOU WISH TO DO ANOTHER TRANSCACTION?(Y/N): ");
• fflush(stdin);
• scanf("%c",&TRANSACTION);
• }while(TRANSACTION=='Y' || TRANSACTION=='y');
• printf("nTHANK YOU FOR USING THIS ATMn");
• getch();
• }
7. • algorithm:
• 1.set minimum amount(5000 for example).
• 2.declare withdraw argument.
• display "welcome to rbl bank "
• 3.enter a valid pin
• 4.do
• enter amount to withdraw
• IF(withdraw % 100!=0)
• plz enter amount in multiple of 100
• ELSE IF(WITHDRAW>(AMOUNT-1000)
• display "INSUFFICIENT BALANCE"
• ELSE
• AMOUNT = AMOUNT-WITHDRAW;
• display "PLEASE COLLECT CASH "
• DO YOU WISH TO DO ANOTHER TRANSCACTION?(Y/N)
•
•
• 5.while(TRANSACTION=='Y' ) continue from step 4.
• 6.THANK YOU FOR USING THIS ATM
•
8. • QUESTION 3:
• Write a program to find the largest element in an array using Recursion
• CODE:
• #include<stdio.h>
• #define MAX_S 1000
• int largest(int arr[],int position,int lar);
• int la,result;
• int main()
• {
• int arr[ MAX_S ];
• int n,i;
• printf(" ENTER SIZE OF ARRAY n");
• scanf(" %d ",&n);
•
• printf("ENTER ELEMNENTS OF ARRAY n");
•
• for(i=0;i<n;i++)
• { scanf("%d ",&arr[i]);
• }
• la=arr[0];
10. • QUESTION 4: Write a C program to separate even and odd numbers of an array and put them in
two separate arrays.
•
• CODE:
• #include<stdio.h>
• # define max_size 1000
• void main()
• {
• int array[max_size];
• int even[max_size];
• int odd[max_size];
• int i,n;
• int e=0,o=0;
•
• printf("enter size of array");
• scanf("%d",&n);
•
• printf("entered size %d n",n);
• printf("enter element in the array ");
• for(i=0;i<=n-1;i++)
• {
12. • Question 5; Write a C program to determine a given matrix is a sparse matrix
• Code:
• #include <stdio.h>
•
•
• int array[10][10];
• int i, j, m, n;
• int counter = 0;
• void main()
• {
• clrscr();
• printf("Enter the order of the matix n");
• scanf("%d %d", &m, &n);
• printf("Enter the co-efficients of the matix n");
• for (i = 0; i < m; ++i)
• {
• for (j = 0; j < n; ++j)
• {
• scanf("%d", &array[i][j]);
• if (array[i][j] == 0)
• {
• ++counter;
• }
• }
• }
• if (counter > ((m * n) / 2))
• {
• printf("The given matrix is sparse matrix n");
• }
• else
• printf("The given matrix is not a sparse matrix n");
• printf("There are %d number of zeros", counter);
• getch();
• }
13. • Question 6:
•
• Write an interactive C program to calculate the sum of array elements using pointer
• Code:
• #include<stdio.h>
• #define MAX 100
• int array[MAX];
• int *ptr;
• int i,n;
• int sum=0;
• void main()
• {
• clrscr();
• printf(" ENTER SIZE OF ARRAY n");
• scanf("%d",&n);
•
• printf("ENTER ELEMENTS OF ARRAY n");
• for(i=0;i<n;i++)
• { scanf("%d",&array[i]);
• }
• ptr=array;
• for(i=0;i<=n-1;i++)
• {
• sum+=*ptr;
• ptr++;
• }
• printf("n REQUIRED SUM IS %d",sum);
• getch();
• }
14. • Question 7: Write an interactive C program to append the contents of a file at the end of another file without
using any built-in functions.
•
• #include <stdio.h>
• #include <stdlib.h>
•
• main()
• {
• FILE *fsring1, *fsring2, *ftemp;
• char ch, file1[20], file2[20], file3[20];
•
• printf("Enter name of first file ");
• gets(file1);
• printf("Enter name of second file ");
• gets(file2);
• printf("Enter name to store merged file ");
• gets(file3);
• fsring1 = fopen(file1, "r");
• fsring2 = fopen(file2, "r");
• if (fsring1 == NULL || fsring2 == NULL)
• {
• perror("Error has occured");
• printf("Press any key to exit...n");
• exit(EXIT_FAILURE);
16. • 8. Write an interactive C program to create a file containing student’s records and also give a provision to
update/modify the records too.
• Code:
• #include<stdio.h>
• #include<conio.h>
• #include<process.h>
• typedef struct student {
• int roll;
• char name[20];
• float marks;
• }3 stud;
• void sort(int n)
• {
• FILE *fp_index, *fp_record;
• int i, j, r1, r2, r3;
• stud s1, s2, s3;
• fp_index = fopen("index.txt", "r+b");
• fp_record = fopen("record.txt", "r+b");
• for (i = 0; i < n - 1; i++)
• {