CP Mid2
CP Mid2
CP Mid2
}
}
int checkPrime(int n)
{
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
return isPrime;
}
fclose(fptr);
return 0;
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Sum = %d",add(n));
return 0;
}
int add(int n)
{
if(n != 0)
return n + add(n-1);
else
return n;
}
4. Average, using an array
#include<stdio.h>
void main(){
int a[100], n, i, sum = 0;
float avg;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for(i = 0; i<n;i++){
scanf("%d", &a[i]);
sum += a[i];
}
printf("\nSum: %d, %d", sum, n);
avg = (float)sum/(float)n;
printf("\nAverage: %.2f", avg);
int main(){
int i;
printf("Enter information of students:\n");
for(i=0; i<5; ++i){
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
for(i=0; i<5; ++i){
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
switch(n)
{
case 1:
add(fname);
break;
case 2:
show(fname);
break;
case 3:
update(fname);
break;
case 4:
exit(0);
default:
printf("Wrong choice.\n");
}
}
}
void add(char *a){
FILE *fp;
fp = fopen(a, "a+");
emp* temp = (emp*) malloc(sizeof(emp));
temp -> name = (char*) malloc(50 * sizeof(char));
if(fp == NULL)
printf("Couldn't open FILE");
else{
printf("Enter Employee's id, name: ");
scanf("%d %s", &temp -> eID, temp -> name);
fwrite(&temp -> eID, sizeof(int), 1, fp);
fwrite(temp -> name, 50, 1, fp);
}
fclose(fp);
free(temp);
free(temp -> name);
}
void update(char *a){
FILE *fp;
int id;
char name[50];
fp = fopen(a, "a+");
emp *temp = (emp*) malloc(sizeof(emp));
temp -> name = (char*) malloc(50 * sizeof(char));
if(fp == NULL)
printf("Couldn't open FILE");
else{
printf("Enter Employee's id and name: ");
scanf("%d %s", &id, name);
fseek(fp, 0L, SEEK_SET);
fread(&temp -> eID, sizeof(int), 1, fp);
while(temp -> eID != id){
fread(&temp -> eID, sizeof(int), 1, fp);
fread(temp -> name, (50 * sizeof(char)), 1, fp);
}
printf("Record Found for eID: %d", temp -> eID);
fwrite(name, (50 * sizeof(char)), 1, fp);
}
fclose(fp);
free(temp);
free(temp -> name);
}
void show(char *a){
FILE *fp;
char name[50];
long size, n, ctr;
fp = fopen(a, "a+");
emp* temp = (emp*) malloc(sizeof(emp));
temp -> name = (char*) malloc(50 * sizeof(char));
if(fp == NULL)
printf("Couldn't open FILE");
else{
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
int emps = sizeof(int) + 50;
rewind(fp);
n = size/emps;
printf("\nNo. Of Records: %d", n);
ctr = 0;
while(ctr < n){
fread(&temp -> eID, sizeof(int), 1, fp);
printf("\nID: %d", temp -> eID);
fread(temp -> name, (50 * sizeof(char)), 1, fp);
printf("Name: %s\n", temp -> name);
ctr++;
}
fclose(fp);
free(temp);
free(temp -> name);
}
}
12. Program to read 2 files and merge them alternatively
#include<stdio.h>
void main(){
char f1[20], f2[20], ch1, ch2, s[100];
FILE *fp1, *fp2, *fp3;
printf("Enter the two filenames: ");
scanf("%s %s", f1, f2);
fp1 = fopen(f1, "r");
fp2 = fopen(f2, "r");
fp3 = fopen("merged.txt", "w");
}
16. Program to append one file to another #include <stdio.h>
#inclde<stdio.h>
void main(int argc,char **argv)
{
FILE *fp1, *fp2;
int ch;
int cursor = 0;
fp1 = fopen(argv[1], "a");
fp2 = fopen(argv[2], "r");
while(((ch = fgetc(fp2))) != EOF){
fputc(ch, fp1);
}
C++:
1. Program to find the factorial of a number.
#include<iostream>
using namespace std;
int main(){
int n,i,fact=1;
cout<<"Enter a number:";
cin>>n;
for(i=1;i<=n;i++){
fact=fact*i;
}
cout<< "Factorial: " << fact << endl;
}
8. Multiply Matrices
#include<iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
cout<<"Enter number of rows and columns of first matrix:";
cin>>m>>n;
cout<<"Enter number of rows and columns of second matrix:";
cin>>p>>q;
if(n!=p)
{
cout<<"Matrices cannot be multiplied!"<<endl;
}
cout<<"Enter elements of first matrix: ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter elements of second matrix: ";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
cout<<"Mulplication Matrix:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
}
9. Sort Elements
#include<iostream>
using namespace std;
int main(){
int n, i, a[50], j, temp;
cout<<"Enter total number of elements:";
cin>>n;
cout<<"Enter numbers:";
for(i=0; i<n; i++){
cin>>a[i];
}
for(i=0; i<(n-1); i++){
for(j=0; j<(n-i-1); j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Sorted list:"<<endl;
for(i=0; i<n; i++){
cout<<a[i]<<" ";
}
}