Saturday, September 28, 2013

C Programming: Linked Lists To File

Leave a Comment
C Programming: Linked Lists To File. This program creates a linked list of 5 integers and stores it in a txt file. The function -linkedlisttofile() (accepts the linked list as a parameter). This function opens a file list.txt and copies the element in the linked list to the file.


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


typedef struct node
{
        int n;
        struct node *next;
}*List;

void linkedlisttofile(List L);
int main()
{
    int ctr, num;
    List L, *trav, p;
   
    L = NULL;
    L = (List)malloc(sizeof(struct node));
    *trav = L;
   
    printf("Enter 5 integers: ");
    for(ctr = 0; ctr < 5; ctr++)
    {
            scanf("%d", &(*trav)->n);
            (*trav)->next = (List)malloc(sizeof(struct node));
            *trav = (*trav)->next;
    }
    (*trav)->next = NULL;
    printf("\nContents of the List: ");
    for(p = L; p->next != NULL; p = p->next)
    {
          printf("%d ", p->n);
    }
   
    linkedlisttofile(L);
    printf("\n\nFile Copy Complete!");
    getch();
    return 0;
}
void linkedlisttofile(List L)
{
     FILE *fp;
     List p;
    
     fp = fopen("list.txt", "w");
    
     for(p = L; p->next != NULL; p = p->next)
     {
          fprintf(fp, "%d ", p->n);
     }
     fclose(fp);
}

Here's the output of the program: 



Read More...

Friday, July 19, 2013

C Programming: Adding Fractions and Reducing the Sum to Lowest Term

Leave a Comment
C Programming: Adding Fractions and Reducing the Sum to Lowest Term. This program shows you how to make a program that adds two fraction numbers and reduce their sum to lowest term. This also shows how to return an entire array from a function.

#include <stdio.h>
#include <conio.h>


int * addFrac(int [], int []);
int main()
{
    int frac1[2], frac2[2];
    int *fsum;
  
    printf("***This Program Adds Two Fraction Numbers***\n\n");
    printf("Enter first fraction number: ");
    scanf("%d/%d", &frac1[0], &frac1[1]);
    printf("Enter second fraction number: ");
    scanf("%d/%d", &frac2[0], &frac2[1]);
  
    fsum = addFrac(frac1, frac2);
    printf("\nThe sum of %d/%d and %d/%d is: %d/%d", frac1[0], frac1[1], frac2[0], frac2[1], *fsum, *(fsum+1));
 
    getch();
    return 0;
}


Read More...

Monday, July 1, 2013

C Programming: Empty Box

Leave a Comment
C Programming: Empty Box. This program shows you how to make an empty box. There are two programs here, first one is not empty and the second is empty.


#include <stdio.h>
#include <conio.h>

int main()
{
    int r, c, row, col;
   
    printf("Enter number of rows: ");
    scanf("%d", &row);
    printf("Enter number of columns: ");
    scanf("%d", &col);
    printf("\n\n");
    for(r = 0; r < row; r++)
    {
          for(c = 0; c < col; c++)
          {
                printf("*");
          }
          printf("\n");
    }
   
    getch();
    return 0;
}

Here's the output of the program: 


Read More...

Tuesday, March 19, 2013

C Programming: Prime or Composite?

Leave a Comment
C Programming: Prime Numbers? This program shows you how to check if the number inputted by the user is a prime number. There are two versions of the program here, one has function and the other doesn't.


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


int main()
{
    int num, ctr = 2;
   
    printf("Enter a number: ");
    scanf("%d", &num);

    if(num == 1)
    {
           printf("\n%d is neither a prime or a composite number!", num);
           getch();
           exit(0);
    }
    if(num == 2)
    {
           printf("\n%d is a prime number!", num);
           getch();
           exit(0);
    }
    while((num % ctr) != 0)
    {
               ctr++;
               if(ctr == num)
               {
                      printf("\n%d is a prime number!", num);
                      getch();
                      exit(0);
               }
    }
    printf("\n%d is a composite number!", num);
   
    getch();
    return 0;
}

Read More...

Sunday, March 10, 2013

C Programming: Basic Arithmetic Operations

Leave a Comment
C Programming: Basic Arithmetic Operations. This program performs basic arithmetic operations like addition, subtraction, multiplication, and division. There are two versions of the program here, the first one is the longer one and the other has more efficiency.
 
#include <stdio.h>
#include <conio.h>

int main()
{
          int x, y, sum, diff, prod, qout;
        
          printf("Enter value for x: ");
          scanf("%d", &x);
          printf("Enter value for y: ");
          scanf("%d", &y);

          sum = x + y;

          diff = x - y;
          prod = x * y;
          qout = x / y;

          printf("\nThe sum of %d and %d is: %d", x, y, sum);

          printf("\nThe difference of %d and %d is: %d", x, y, diff);
          printf("\nThe product of %d and %d is: %d", x, y, prod);
          printf("\nThe qoutient of %d and %d is: %d", x, y, qout);
        
          getch();
          return 0;
}
Read More...

Friday, March 8, 2013

C Programming: Swapping Values

Leave a Comment
C Programming: Swapping Values. This program will help enhance your skills in manipulating variables and its value. Swapping values is very helpful in some programs especially sorting algorithms like bubble sort. In this program we need a temporary variable to store the value of the first variable. There are two versions of the program here, first one has no functions and the other one have.

#include <stdio.h>
#include <conio.h>

 int main()

{
           int x = 5, y = 7, temp;

           printf("Before swapping: x = %d and y = %d\n", x, y);

      
           temp = x;
           x = y;
           y = temp;

           printf("\nAfter swapping: x = %d and y = %d", x, y);


           getch();

           return 0;
}

Read More...

Saturday, January 26, 2013

Computer Tips and Tricks: How to Change Windows Account Password Without Knowing the Old Password

Leave a Comment
1. Open the command prompt (Go to Start > Run > type cmd then Enter).



2. Type net user and hit Enter.



Read More...