Monday, 9 November 2015

A program to illustrate the use of fgets( )

#include <stdio.h>

int main()
{
  FILE *fp;
  char str[60];

  /* opening file for reading */
  fp = fopen("file.txt" , "r");
  if(fp == NULL) {
     perror("Error opening file");
     return(-1);
  }
  if( fgets (str, 60, fp)!=NULL ) {
     /* writing content to stdout */
     puts(str);
  }
  fclose(fp);

  return(0);
}

Output:

Demo File

Write a program that uses a table of integers whose size will be specified interactively at run time

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

void main()
{
    int *p, *table;
    int size;
    printf("\nWhat is the size of table?");
    scanf("%d",&size);
    printf("\n");

    if((table = (int*)malloc(size *sizeof(int))) == 0)
       {
           printf("No space available \n");
           exit(1);
       }
    printf("\n Address of the first byte is  %u \n", table);
    /* Reading table values*/
    printf("\nInput table values\n");

    for (p=table; p<table + size; p++)
         scanf("%d",p);
    /* Printing table values <span id="IL_AD3" class="IL_AD">in reverse</span> order*/
    for (p = table + size -1; p >= table; p --)
       printf("%d is stored at address %u \n",*p,p);

getch();
}


Output:

What is the size of the table? 5
Address of the first byte is 2262
Input table values
11 12 13 14 15
15 is stored at address 2270
14 is stored at address 2268
13 is stored at address 2266
12 is stored at address 2264
11 is stored at address 2262

Write a program using pointer to read an array of integer and print element in reverse order

#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr,i,n;
clrscr();
printf(“Enter the no of elements:”);
scanf(“%d”,&n);

for(i=0; i<n; i++)
{
printf(“Enter %d element : “,i+1);
scanf(“%d”,&ptr[i]);
}

printf(“Array in reverse order\n”);
for(i=n-1; i>=0; i–)
{
printf(“%d\n”,ptr[i]);
}
getch();

}

Output:

Enter the Number of Elements:5
Enter 1 Element: 1

Enter 2 Element: 3

Enter 3 Element: 5

Enter 4 Element: 7

Enter 5 Element: 9

Array in Reverse Order
9
7
5
3
1

Write a program using pointer to concate two strings

#include<stdio.h>
int main(){
 int i=0,j=0;
 char *str1,*str2,*str3;
 puts("Enter first string");
 gets(str1);
 puts("Enter second string");
 gets(str2);
 printf("Before concatenation the strings are\n");
 puts(str1);
 puts(str2);
 while(*str1){
     str3[i++]=*str1++;
 }
 while(*str2){
     str3[i++]=*str2++;
 }
 str3[i]='\0';
 printf("After concatenation the strings are\n");
 puts(str3);
 return 0;
}


Output:
Enter first String:HI
Enter Second String:HELLO
After Concatenation the strings are HI HELLO

Write a program using pointer to compare two strings

#include<stdio.h>

int compare_string(char*, char*);

main()
{
    char first[100], second[100], result;

    printf("Enter first string\n");
    gets(first);

    printf("Enter second string\n");
    gets(second);

    result = compare_string(first, second);

    if ( result == 0 )
      printf("Both strings are same.\n");
    else
      printf("Entered strings are not equal.\n");

    return 0;
}

int compare_string(char *first, char *second)
{
  while(*first==*second)
  {
     if ( *first == '\0' || *second == '\0' )
        break;

     first++;
     second++;
  }
  if( *first == '\0' && *second == '\0' )
     return 0;
  else
     return -1;
}

Output:
Enter first String:HI
Enter Second String:HI
Both string are same

Define a structure called cricket that will describe the following information: a. Player name b. Team name c. Batting average

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

struct cricket
{
    char nm[20],team[20];
    int avg;
};

#define total 2

int main()
{
    struct cricket player[total],temp;
    int i,j;
    clrscr();
    for(i=0;i<total;i++)
    {
       printf("For player %d\n",i+1);
       printf("Enter the name of player : ");
       fflush(stdin);
       gets(player[i].nm);
       printf("Enter the team : ");
       fflush(stdin);
       gets(player[i].team);
       printf("Enter the batting average : ");
       fflush(stdin);
       scanf("%d",&player[i].avg);
    }
    printf("\nTeam       Name       Average\n");
    printf("                             \n");
    for(i=0;i<total;i++)
    {
       printf("%-10s %-10s %7d\n",player[i].team,player[i].nm,player[i].avg);
    }
    getch();
    return 0;
}

Output:

Enter the name of Player:Virat
Enter Team:India
Enter Batting Average:40
Enter the name of Player:Rohit
Enter Team:India
Enter Batting Average:45

Team         Name         Average
India        Virat         40
India        Rohit         45

Sunday, 8 November 2015

Write a calculator program (add, subtract, multiply, divide). Prepare user defined function for each functionality

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
print(“Enter two Numbers”);
Scanf(“%d%d”,&a,&b);
Add(a,b);
Subtract  (a,b);
Multiply(a,b);
Devide(a,b);
getch();

}

Void Add(int a,int b)
     {
printf(“add:%d”,a+b);


       }
Void Substract(int a,int b)
     {
printf(“Sub:%d”,a-b);


       }
Void Multiply(int a,int b)
     {
printf(“Mul:%d”,a*b);


       }
Void Devide(int a,int b)
     {
printf(“Div:%d”,a/b);


       }
Output:

Enter Two Numbers 10 20
Add:30
Sub:-10
Mul:200
DIV:0

Write a function prime that return 1 if it‘s argument is prime and return 0 otherwise

#include
#include
int prime(int);
int main()
{
int n,p;
clrscr();
printf(“Enter a number : “);
scanf(“%d”,&n);
p=prime(n);
if(p==1)
printf(“%d is prime\n”,n);
else
printf(“%d is not prime\n”,n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}

Output:
Enter a number:3
3 is prime

Write a program to find factorial of a number using recursion

#include<stdio.h>
int factorial(int n);
int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    printf("Factorial of %d = %ld", n, factorial(n));
    return 0;
}
int factorial(int n)
{
    if(n!=1)
    return n*factorial(n-1);
}

Write a function program to add first N numbers

#include <stdio.h>
 
int main()
{
   int n, sum = 0, c, value;
 
   printf("Enter the number of integers you want to add\n");
   scanf("%d", &n);
 
   printf("Enter %d integers\n",n);
 
   for (c = 1; c <= n; c++)
   {
      scanf("%d", &value);
      sum = sum + value;
   }
 
   printf("Sum of entered integers = %d\n",sum);
 
   return 0;
}

Write a function power that computes x raised to the power y for integer x and y and returns double type value.

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

void main()
{
    int x,y;
    double power();
    clrscr();

    printf("Enter value of x : ");
    scanf("%d",&x);
    printf("Enter value of y : ");
    scanf("%d",&y);

    printf("%d to power %d is = %f\n",x,y,power(x,y));
    getch();
}
double power(x,y)
int x,y;
{
    double p;
    p=1.0;
    if(y>=0)
       while(y--)
           p*=x;
    elsewhile(y++)
           p/=x;
    return(p);
}

Find given string is palindrom or not using string library function.

#include<stdio.h>

int main(){
  char str[100];
  int i=0,j=-1,flag=0;

  printf("Enter a string: ");
  scanf("%s",str);

  while(str[++j]!='\0');
  j--;

  while(i<j)
      if(str[i++] != str[j--]){
           flag=1;
           break;
      }
     

  if(flag == 0)
      printf("The string is a palindrome");
  else
      printf("The string is not a palindrome");

  return 0;
}

Write a program convert character into TOggLe character.

#include<stdio.h>

int main()
{
 char str[100];
 int i;
 printf("Please enter a string: ");

 // gets(str); 
 // fgets is a better option over gets to read multiword string .

 fgets(str, 100, stdin);
 
 // Following can be added for extra precaution for '\n' character
 // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL;

 for(i=0;str[i]!=NULL;i++)
 {
  if(str[i]>='A'&&str[i]<='Z')
   str[i]+=32;
  else if(str[i]>='a'&&str[i]<='z')
   str[i]-=32;
 }
 
 printf("String in toggle case is: %s",str);

 return 0;
}

Fabonacci series

#include <stdio.h>
int main()
{
  int count, n, t1=0, t2=1, display=0;
  printf("Enter number of terms: ");
  scanf("%d",&n);
  printf("Fibonacci Series: %d+%d+", t1, t2); 
  count=2;   
  while (count<n)  
  {
      display=t1+t2;
      t1=t2;
      t2=display;
      ++count;
      printf("%d+",display);
  }
  return 0;
}