Wednesday, 8 March 2017

HackerRank Day 1 Data Types 30 Day Solution In C (Concatenation)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i = 4;
    double d = 4.0;
    char s[] = "HackerRank ";
    int j;
    double dd;
    char str[100],*p,a[100];
    scanf("%d",&j);
    scanf("%lf",&dd);
    scanf(" %[^\n]",str);
    p=strcpy(a,s);
    // Read and save an integer, double, and String to your variables.
    printf("%d\n",i+j);
    // Print the sum of both integer variables on a new line.
    printf("%.1lf\n",d+dd);
    // Print the sum of the double variables on a new line.
    p=strcat(a,str);
    printf("%s\n",p);
    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
   
return 0;
}


Student file to insert,display,delete,update,sort records using C

#include<stdio.h>
struct student
{
int rollno;
char name[30];
float mark;
}stud;
//    FUNCTION TO INSERT RECORDS TO THE FILE
void insert()
{
FILE *fp;
fp = fopen("Record", "a");
printf("Enter the Roll no   :");
scanf("%d", &stud.rollno);
printf("Enter the Name      :");
scanf("%s", &stud.name);
printf("Enter the mark      :");
scanf("%f", &stud.mark);
fwrite(&stud, sizeof(stud), 1, fp);
fclose(fp);
}
//    FUNCTION TO DISPLAY RECORDS
void disp()
{
FILE *fp1;
fp1 = fopen("Record", "r");
printf("\nRoll Number\tName\tMark\n\n");
while (fread(&stud, sizeof(stud), 1, fp1))
printf("  %d\t\t%s\t%.2f\n", stud.rollno, stud.name, stud.mark);
fclose(fp1);
}
//    FUNCTION TO SEARCH THE GIVEN RECORD
void search()
{
FILE *fp2;
int r, s, avl;
printf("\nEnter the Roll no you want to search  :");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
printf("Roll No %d is not available in the file\n",r);
else
{
fp2 = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp2))
{
s = stud.rollno;
if (s == r)
{
printf("\nRoll no = %d", stud.rollno);
printf("\nName    = %s", stud.name);
printf("\nMark    = %.2f\n", stud.mark);
}
}
fclose(fp2);
}
}
//    FUNCTION TO DELETE A RECORD


void deletefile()
{
FILE *fpo;
FILE *fpt;
int r, s;
printf("Enter the Roll no you want to delete :");
scanf("%d", &r);
if (avlrollno(r) == 0)
printf("Roll no %d is not available in the file\n", r);
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = stud.rollno;
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
fwrite(&stud, sizeof(stud), 1, fpo);
printf("\nRECORD DELETED\n");
fclose(fpo);
fclose(fpt);
}

}
//    FUNCTION TO UPDATE THE RECORD
void update()
{
int avl;
FILE *fpt;
FILE *fpo;
int s, r, ch;
printf("Enter roll number to update:");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
{
printf("Roll number %d is not Available in the file", r);
}
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = stud.rollno;
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
else
{
printf("\n\t1. Update Name of Roll Number %d", r);
printf("\n\t2. Update Mark of Roll Number %d", r);
printf("\n\t3. Update both Name and Mark of Roll Number %d", r);
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter Name:");
scanf("%s", &stud.name);
break;
case 2:
printf("Enter Mark : ");
scanf("%f", &stud.mark);
break;
case 3:
printf("Enter Name: ");
scanf("%s", &stud.name);
printf("Enter Mark: ");
scanf("%f", &stud.mark);
break;
default:
printf("Invalid Selection");
break;
}
fwrite(&stud, sizeof(stud), 1, fpt);
}
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
{
fwrite(&stud, sizeof(stud), 1, fpo);
}
fclose(fpo);
fclose(fpt);
printf("RECORD UPDATED");
}
}
/* FUNCTION TO SORT THE RECORD */
void sort()
{
int a[20], count = 0, i, j, t, c;
FILE *fpo;
fpo = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fpo))
{
a[count] = stud.rollno;
count++;
}
c = count;
for (i = 0; i<count - 1; i++)
{
for (j = i + 1; j<count; j++)
{
if (a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
printf("Roll No.\tName\t\tMark\n\n");
count = c;
for (i = 0; i<count; i++)
{
rewind(fpo);
while (fread(&stud, sizeof(stud), 1, fpo))
{
if (a[i] == stud.rollno)
printf("\n %d\t\t %s \t\t %2f",stud.rollno, stud.name, stud.mark);
}

}
}
//    FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE //
int avlrollno(int rno)
{
FILE *fp;
int c = 0;
fp = fopen("Record", "r");
while (!feof(fp))
{
fread(&stud, sizeof(stud), 1, fp);

if (rno == stud.rollno)
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//FUNCTION TO CHECK THE FILE IS EMPTY OR NOT
int empty()
{
int c = 0;
FILE *fp;
fp = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp))
c = 1;
fclose(fp);
return c;
}
// MAIN PROGRAM
void main()
{
int c, emp;
do
{
printf("\n\t---Select your choice---------\n");
printf("\n\t1. INSERT\n\t2. DISPLAY\n\t3. SEARCH");
printf("\n\t4. DELETE\n\t5. UPDATE\n\t6. SORT");
printf("\n\t7. EXIT");
printf("\n\n------------------------------------------\n");
printf("\nEnter your choice:");
scanf("%d", &c);
printf("\n");
switch (c)
{
case 1:
insert();
break;
case 2:
emp = empty();
if (emp == 0)
printf("\nThe file is EMPTY\n");
else
disp();
break;
case 3:
search();
break;
case 4:
deletefile();
break;
case 5:
update();
break;
case 6:
emp = empty();
if (emp == 0)
printf("\n The file is EMPTY\n");
else
sort();
break;
case 7:
exit(1);
break;
default:
printf("\nYour choice is wrong\nPlease try again...\n");
break;

}
} while (c != 7);
}



C Program used to scan a string with space and display by using scanf("%[^\n]s",name);

#include <stdio.h>

int main (int argc, char const *argv[])
{
    char name[20];
    scanf("%[^\n]s",name);
    printf("%s\n", name);
    return 0;
}

C Program to print numbers following pattern

     1
  2 3 4
5 6 7 8 9

#include<stdio.h>
int main()
{
  int i,j,k;
  k=1;
  for(i=1;i<=5;i+=2)
  {
    for(j=5;j>=1;j--)
    {
      if(j>i)
        printf(" ");
      else
        printf("%d ",k++);
    }
    printf("\n");
  }
  return 0;
}



C Program to Print following number pattern

1
12
123
1234
12345


#include <stdio.h>

int main()
{
    int i, j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }

    return 0;
}