Program using classes and object



#include
#include
using namespace std;
class Test
{
    private:
        int data1;
        float data2;
    public:
       void insertIntegerData(int d)
       {
          data1 = d;
          cout << "Number: " << data1;
        }
       float insertFloatData()
       {
           cout << "\nEnter data: ";
           cin >> data2;
           return data2;
        }
}; 
 int main()
 {
      Test o1, o2;
      float secondDataOfObject2;
      o1.insertIntegerData(12);
      secondDataOfObject2 = o2.insertFloatData();
      cout << "You entered " << secondDataOfObject2;
      return 0;
 }

-----------------------------------------------------------------------------------------------------------------------------------

Output : -
Number: 12

Enter data: 23.3

You entered 23.3

Programs using Files in C

/*Write a C program to read name and marks of n number of students from user and store them in a file.*/
#include
int main()
{
   char name[50];
   int marks, i, num;

   printf("Enter number of students: ");
   scanf("%d", &num);

   FILE *fptr;
   fptr = (fopen("C:\\student.txt", "w"));
   if(fptr == NULL)
   {
       printf("Error!");
       exit(1);
   }
   for(i = 0; i < num; ++i)
   {
      printf("For student%d\nEnter name: ", i+1);
      scanf("%s", name);

      printf("Enter marks: ");
      scanf("%d", &marks);

      fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);
   }

   fclose(fptr);
   return 0;
}


--------------------------------------------------------------------------------
OUTPUT :-

Programs using Functions and Pointers in C

/* C Program to swap two numbers using pointers and function. */
#include
void swap(int *n1, int *n2);
int main()
{
    int num1 = 5, num2 = 10;
    // address of num1 and num2 is passed to the swap function
    swap( &num1, &num2);
    printf("Number1 = %d\n", num1);
    printf("Number2 = %d", num2);
    return 0;
}

void swap(int * n1, int * n2)
{
    // pointer n1 and n2 points to the address of num1 and num2 respectively
    int temp;
    temp = *n1;
    *n1 = *n2;
    *n2 = temp;
}





OUTPUT :-