Thursday, July 4, 2019

print the pascal triangle in c language


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

long factorial(int);
long comb(int,int);

int main()
{
    int i,j,k;
    printf ("Enter number of rows for pascal triangles\n");
    scanf ("%d", &k) ;
    for(i=0;i<k;i++)
    {
        for(j=0;j<=i;j++)
        printf("%5ld",comb(i,j));
        printf("\n") ;
    }
}
long comb(int n,int r)
{

    long c;
    c=factorial(n)/(factorial(r)*factorial(n-r));
    return c;
}
long factorial (int k)
{
    long fact=1;
    while(k>0)
    {
        fact*=k;
        k--;
    }
    return fact;
}


output:
Enter number of rows for pascal triangles
5
    1
    1    1
    1    2    1
    1    3    3    1
    1    4    6    4    1

convert binary octal to decimal in c language

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

int main()
{
    int num,base,result;
    char choice;
    printf("enter b for binary and o for octal\n");\
    scanf("%c",&choice);
    printf("enter the number\n");
    scanf("%d",&num);

  if(choice=='b')
  {
    base=2;

  }
  else
    base=8;
 //result=fun(num,base);
 printf("Decimal number is %d\n",fun(num,base));
    return 0;
}

fun(int n,int base)
{
    int rem,d,j=1,dec=0;
    while(n>0)
    {
        rem=n%10; /* taking last digit */
        d=rem*j;
        dec+=d;
        j*=base;
        n/=10; /* skipping last digit */
    }

return dec;
}

float power Calculation

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


float power (float a, int n);
void main()
{
    float a, p;
    int n;
    printf("Enter a and n\n") ;
    scanf("%f%d",&a,&n) ;
    p=power(a,n) ;
    printf("%f raised. to power %d is %f\n",a,n,p);
}
float power(float a, int n)
{

 if(n==0)
    return(1);
 else
    return(a*power(a,n-1));
}

output::
Enter a and n
5
2

5.000000 raised. to power 2 is 25.000000

static variable in c language

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

void func(void);
void main()
{
    func();
    func();
    func();
}


void func(void){
int a=10;
static int b=10;
printf("a=%d b=%d\n",a,b);
a++;
b++;

}

output::
a=10 b=10
a=10 b=11
a=10 b=12

global variables in c language



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


void funcl(void) ;
void func2(void) ;
int a,b=6;

void main()
{
    printf("Inside main()a=%d b=%d\n",a,b);
funcl();
func2();
}

void funcl(void)
{
   printf("\nInside funcl() a=%d,b=%d\n",a,b);
}

void func2 (void)
{
    int a=8;
    printf("\nInside func2() a=%d, b=%d\n",a,b);

}


output::
Inside main()a=0 b=6

Inside funcl() a=0,b=6

Inside func2() a=8, b=6

write a program to raise floating point number to an integer power in c language


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

void main()
{
    float a;
    int n;
    float power (float a, int n);
    printf("Enter base");
    scanf("%f",&a);
    printf("Enter exponent");
    scanf("%d",&n);
    printf("%f raised to power::%d is %f\n",a,n,power(a,n));
}
float power (float a, int n)
{
    int i;
    float p=1;
    if(n==0)
        return 1;
    else
        for(i=1;i<=abs(n) ;i++)
        p=p*a;
    if (n>0)
        return p;
    else
    return 1/p;
}


output::
Enter base12
Enter exponent2
12.000000 raised to power::2 is 144.000000

palindrome or not....in c language

#include<stdio.h>
int reverse(int n);
main()
{
    int num;
    printf("Enter a number\n");
    scanf("%d",&num) ;
    printf("Reverse of::%d is ::%d\n", num, reverse(num));
    if(num==reverse(num))
        printf("number is palindrome\n");
    else
        printf("Number is not a palindrome\n");
}
int reverse(int n)
{
int rem, rev=0;
    while(n>0)
    {
        rem=n%10;
        rev=rev*10 +rem;
        n/=10;
    }
return rev;
}

no arguments and no return values in c language


#include<stdio.h>

void dispmenu(void);

void main()
{
    int choice;
    dispmenu();
    printf("Enter your choice :");
    scanf("%d",&choice);
}
void dispmenu (void)
{
    printf (" 1. Create database\n");
    printf("2.Insert new record\n");
    printf("3.Modifya record\n");
    //Functions
    printf("4.Delete a record\n");
    printf("5.Display all records\n");
    printf("6.Exit\n");
}

number of characters occarance in a given string in c language

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

int main()
{
   char string[100];
   int c = 0, count[26] = {0}, x;

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

   while (string[c] != '\0') {
   /** Considering characters from 'a' to 'z' only and ignoring others. */

      if (string[c] >= 'a' && string[c] <= 'z') {
         x = string[c] - 'a';
         count[x]++;
      }

      c++;
   }

   for (c = 0; c < 26; c++)
         printf("%c occurs %d times in the string.\n", c + 'a', count[c]);

   return 0;
}

python class topic video