Pointer Array in C


Provide any example program with the use of pointer and array at a time.

void printArray(int *ptr, size_t length);            
{         
    //for statment to print values using array             
    size_t i = 0;
    for( ; i < length; ++i )      
    printf("%d", ptr[i]);        
}   

 void printString(const char *ptr);            
{         
    //for statment to print values using array             
    for( ; *ptr!=NULL; ++ptr)        
    printf("%c", *ptr);        
}         

int main()    
{    
    int array[6] = {2,4,6,8,10};     
    const char* str = "Hello World!";
    printArray(array, 6);    
    printString(str);
    return 0;     
}

Recursive Function in C


Write a program to calculate factorial of a given integer number. Use recursive function

String Programming in C


Write a program to count vowel, consonant and word from a given string.

 main()
{
  char line[100];
  int i,con=0,vowel=0,word=0;
  printf("Enter your string :\n");
  gets(line);
for(i=0;line[i]!='\0';++i)
  {
   if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
    line[i]=='o' || line[i]=='u' || line[i]=='A' ||
    line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
    ++vowel;
  else if((line[i]>='a' && line[i]<='z')||(line[i]>='A' && line[i]<='Z'))
    ++con;
else if(line[i]==' ')word++;
}
  printf("\nVOWEL : %d",vowel);
  printf("\nCONSONENT : %d",con);
  printf("\nWORD : %d",(word+1));
}

Function Programming : Fibonacci in C

Write a program to generate a Fibonacci Series. Use function.

Hints: First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two numbers in series every other numbers are the sum of two previous numbers. For example 5 = 2 + 3 (addition of 2 & 3).

Source Code in C:
#include<stdio.h>
void fibonacci(int m, int n);
int main( )
{
     int a=0, b=1;
     printf("Fibonacci series is :\n\n");
     fibonacci(a, b);
}
void fibonacci(int m, int n)
{
      int sum=0, i;
      for(i=0; i<=50; i++)
      {
          sum=m+n;
          printf("  %d",sum);
          m=n;
          n=sum;
      }
}

Function Programming : divisible by 9 in C

Write a program to check Number which is dividable by 9 or not. Use function to call and return the value.
 
Source Code in C:
#include <stdio.h>
void check_divisible(int);
int main( )
{
     int n, result;
     printf("\n Enter the number to check");
     scanf("%d", &n);
     result=check_divisible(n);
     return 0;
}

void check_divisible(int a)
{
     int i=9;
     if (a%9 = =0)
          printf("\n This is divisible by 9");
     else
          printf("\n This is not divisible by 9");
}

Function Programming : Area of Circle in C

Write a program to find the area of a Circle. Use function to find area where area = pi * r ^ 2( r means radius, ^ means power and the value of pi is 3.144) .

Source Code in C:

#include<stdio.h>
float checkArea(float radius)
{
      float area;
      area=3.14159*(radius*radius) ;
      return area;
}
int main()
{
      float r, area_of_circle=0;
      printf ("\nEnter Radius: ");
      scanf ("%f", &r);
      area_of_circle=checkArea(r);
      printf ("\nArea is: %f", area_of_circle);
}

Function Programming : Divisible by 5 in C

Write a C Program using function to check whether the given input is divisible by 5 or not.

Source Code in C:

#include<stdio.h>
void divis (int p, int q);
int main()
{
     int a, b=5;
     scanf("%d", &a);
     divis(a, b);
}

void divis (int p, int q)
{
     if (p%q = =0)
          printf ("\n Given number is divisible by 5");
     else
          printf ("\n Given number is not divisible by 5");
 }

Function Programming : Print Array in C

Write a C Program to print all elements from an Array using c-function.

#include<stdio.h>
void array(int b[], int n);
int main( )
{
     int n, j;
     printf("\nThe size of array is : ");
     scanf("%d", &n);
     int a[n];
     printf("\nInsert your values: ");
     for(j=0; j<n; j++)
          scanf("%d", &a[j]);
     array(a, n);
}

void array(int b[],int n)
{
      int i;
      printf("\nYour values are: ");
      for(i=0; i<n; i++)
           {
                 printf("  %d", b[i]);
           }
}

Function Programming : Array programming in C

Write  a program using c-function to read from an array and do the sum of all elements from that array.

Source Code in C:

#include<stdio.h>
int arr(int a[], int len);
int main()
{
     int len, ret, i;
     printf ("\nEnter your array size: ");
     scanf ("%d", &len);
     int a[len];
     printf("\nEnter your values: ");
     ret=arr(a, len);
     printf("\nThe sum is: %d", ret);
 }

int arr(int b[ ], int n)
{
     int i, result =0;
         for(i=0; i<n; i++)
         {
              scanf ("%d", &b[i]);
              result += b[i];
         }
         for(i=0; i<n; i++)
               result +=  b[i];
          
         return (result);
}

Function Programming in C : Prob-5

Evaluate below equation using c function.

sum=x + x^2  +  x^3  + ...........+ x ^ n.

Source Code in C:

#include<stdio.h>
int main()
{
     int a;
     printf("\nEnter your number : ");
     scanf("%d",&a);
     sum(a);
}

int sum(int b)
{
     int i,c=1,sum=0;
     for(i=1; i<=5; i++)
         {
              c=b*i;
              sum+=c;
         }
   
    printf("\nThe sum is: %d", sum);
}

Function Programming : Even and Odd Number find using C

WAP using C-Function to check Odd Number and Even Number from a given input.

#include<stdio.h>
int evenodd(int a);
int main()
{
    int a;
    printf("\nEnter the number: ");
    scanf("%d",&a);
    evenodd(a);
}
int evenodd(int q)
{
if  (q%2 = = 0)
      printf("\nThis is a even number");
else if (q%2 = = 1)
      printf("\nThis is a odd number");
}

Function Programming : Strange Number in C

WAP to check whether the given input is Strange or Not.

#include<stdio.h>
int main()
{
     int digit;
     printf("\nEnter your digit: ");
     scanf("%d", &digit);
     strng(digit);
}
 
int strng(int d)
{
     int i, j, t=1;
     while (d!=0)
         {
               j=d%10;
              d=d/10;
              for (i=2; i<j; i++)
                  {
                       if ( j%i = =0 )
                      {
                            t=0;
                            break;
                       }
                   }
           }
 
if (t = = 1)
      printf("\nThis number is strange");
else
      printf("\nThis number is not strange");
}

Function Programming : Perfect Number in C

WAP using C-Function to check whether the given number is Perfect or Not.

#include<stdio.h>
int main()
{
    int i;
    printf("Enter your number : ");
    scanf("%d",&i);
    checkperfect(i);
}

int checkperfect(int j)
{
    int a,sum=0;
    for(a=1;a<=j/2;a++)
      {
         if (j%a = = 0)
         sum+=a ;
      }
  
    if(sum = = j)
         printf("\n Perfect Number \n");
    else
         printf("\n Not Perfect Number \n");
}

Function Programming : Check Prime in C

Write a Program to check whether the given input in Prime or Not . User defined function is mandatory to use.

Source Code in C Language:

#include<stdio.h>
int main()
{
    int inp;
    scanf("%d",&inp);
    printf("\nThis number is: ");
    pri(inp);
}
int pri(int p)
{
   int i,t;
   t=1;
   for(i=2; i<p; i++)
       {
          if(p%i==0)
            {
               t=0;
               break;
            }
       }
  
     if(t==0)
         printf("Not prime");
     else
        printf("Prime");
}

Equation of a Line



Finding the equation of a Line Given Two End Points



Write a program for finding the equation of a Line Given Two End Points (x1,y1) and (x2, y2)
The equation for a line is Y = MX + C
Where M = Slope of a Line and C = Intercept
To Find the slope of a line
slope = (y2 - y1) / (x2 - x1)
To Find the intercept of the line, intercept = y1 - (slope) * x1
which would be same as, intercept = y2 - (slope) * x2

Input/Output:

Program to find the equation of a line given two end points
Enter X1: 2
Enter Y1: 3
Enter X2: 5
Enter Y2: 7
Equation of the line with end points (2, 3 and (5, 7) : Y = 1.33333X +0.333333