Wednesday, August 1, 2012

Problem 1

Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.


Find the sum of all the multiples of 3 or 5 below 1000.

Problem 1 Solution

#include
#include
void addfriend();
void disconinfo();

int main()
{
 int ans;

 system("cls");
 printf("----------------Menu-------------------------\n");
 printf("1. Add new friend.                           \n");
 printf("2. Display contact info.                     \n");
 printf("3. Exit                                      \n");
 printf("---------------------------------------------\n");
 printf("Enter menu number:");
 scanf("%d",&ans);

 switch(ans)
 {
  case 1:
   addfriend();
   main();
   break;
  case 2:
   disconinfo();
   main();
   break;
  case 3:
   exit(1);
   break;
  default:
   printf("\nERROR : Invalid menu Item");
   printf("\nPress any key to continue....");
   getch();
   main();
 }
 return 0;
}

void addfriend()
{
 char name[20],tele[10],email[30],c;
 int i;
 FILE *fp;

 system("cls");
 printf("---------New friend info--------\n");
 printf("Name : ");
 scanf("%s",name);
 printf("Phone-No:");
 scanf("%s",tele);
 printf("e-Mail : ");
 scanf("%s",email);

 fp=fopen("Directory.txt","a+");

 if(fp!=NULL)
 {
  c='A';
  while(c!=EOF)
  {
   c=fgetc(fp);
  }

  fputs("\n",fp);
  fputs(name,fp);
  fputs(" ",fp);
  fputs(tele,fp);
  fputs(" ",fp);
  fputs(email,fp);

  fclose(fp);

  main();
 }
 else
 {
  printf("ERROR : While openning the Directory.txt");
 }
}

void disconinfo()
{
 char name[20],tele[10],email[30],c;
 FILE *fp;

 system("cls");

 fp=fopen("Directory.txt","r");
 printf("%-20s ","Name");
 printf("%-10s ","Telephone");
 printf("%-30s\n","E-mail");
 printf("----------------------------------------------------------------------\n");
 if(fp!=NULL)
 {
  while(!feof(fp))
  {
      if(feof(fp))
   {
       break;
   }
   fscanf(fp,"%s",name);
   printf("%-20s ",name);
   fscanf(fp,"%s",tele);
   printf("%-10s ",tele);
   fscanf(fp,"%s",email);
   printf("%-30s ",email);
   fscanf(fp,"%c",&c);
   printf("\n");
  }
  fclose(fp);
  printf("\nPress any key to continue....");
  getch();
 }
 else
 {
  printf("ERROR : Directory.txt cannot open");
 }
}















No comments:

Post a Comment