Monday, July 28, 2014

c assignment 4

↓↑ Program to count Number of 1's in binary number. 
#include<stdio.h>
void bin(int);
static int c=0;
int main()
{
    int n;
    printf("enter decimal number :");
    scanf("%d",&n);
    printf("\nbinary number is:");
    bin(n);
    printf("\nno of one=%d\n",c);

return 0;
}
void bin(int n)
{
    int r;
    if(n!=0)
    {
     r=n%2;
 if(r)c++;
        n=n/2;
 bin(n);
 printf("%d",r);
    }
    else 
        printf(" ");
}
↓↑ Program to Find evaluation order of function parameters.
#include<stdio.h>
void test(int,int,int);
int main()
{
    int i=6; 
    test(i++,i++,i++);   // Although behaviour undefined but most compilers follow right-to-left evaluation order.
    printf("\nAfter post:\n");
    test(++i,++i,++i);   
return 0;
}

void test(int a,int b,int c)
{
    printf("value:a=%d\tb=%d\tc=%d\n",a,b,c);
    printf("address:a=%u\tb=%u\tc=%u\n",&a,&b,&c);
}



↓↑ Program to calculate factorial of number correctly till 1...20.
#include<stdio.h>
long long int fact(int n);
int main()
{
    int n;
    long long int factN;
    printf("enter number:");
    scanf("%d",&n);
    factN=fact(n);
    printf("value of factorial %d is:%lld\n",n,factN);
return 0;
}

long long int fact(int n)
{
    if(n==0)
        return 1;
    else
        return n*fact(n-1);
}
↓↑ Program to calculate factorial of very large number(with math lib).
#include <stdio.h>
#include <math.h>
#include <string.h>

void multd(char * s, size_t len, unsigned n)
{
    unsigned values[len];
    size_t i;
    memset(values, 0, sizeof(unsigned) * len);
    for(i = len; i--; )
    {
        unsigned x = values[i] + (s[i] - '0') * n;
        s[i] = '0' + x % 10;
        if(i) values[i - 1] += x / 10;
    }
}

void factd(char * s, size_t len, unsigned n)
{
    memset(s, '0', len - 1);
    s[len - 1] = '1';
    for(; n > 1; --n) multd(s, len, n);
}

int main(void)
{
    unsigned num = 5;
    size_t len = ceill(log10l(tgammal(num + 1)));
    char dstr[len + 1];
    dstr[len] = 0;
    factd(dstr, len, num);
    puts(dstr);
}
↓↑ Program to calculate factorial of very large number(with gmp lib).
// There is no practical limit to the precision except the ones implied 
// by the available memory in the machine GMP runs on.
#include <stdio.h>
#include <gmp.h>

void fact(mpz_t r,int n){
    unsigned int i;
    mpz_t temp;
    mpz_init(temp);
    mpz_set_ui(r,1);
    for(i=1;i<=n;i++){
        mpz_set_ui(temp,i);
        mpz_mul(r,r,temp);
    }
    mpz_clear(temp);
}
int main(void) {
    mpz_t r;
    mpz_init(r);
//    fact(r,188315);
    fact(r,100); 
    gmp_printf("%Zd\n",r);
    mpz_clear(r);
return(0);
}
//https://gmplib.org/
//sudo apt-get install  libgmp3-dev or sudo yum install  libgmp3-dev
//gcc factlargernumber.c -lgmp
↓↑ Program to print fibonacci series upto given length.
#include<stdio.h>
void fib(int,int,int);
int main()
{
    int f0=0,f1=1,len;
    printf("Enter fibonaci series length: ");
    scanf("%d",&len);  
    fib(f0,f1,len);
    printf("\n");
return 0;
}
void fib(int current,int old,int l)
{
    static int i=0;
    int new;
    if(i<l)
    {
        new=current+old;
        printf("%d ",new);
        old=current;
        current=new;
        i++;
        fib(current,old,l);
    }
    else
        return;  
}
↓↑ Program to demonstrate function pointer.
#include<stdio.h>
int test(int,int,int (*fp)(int,int));
int (*fp)(int,int);
int sum(int,int);

int main()
{
    int a=3,b=4,sm;
    fp=sum;
    sm=test(a,b,sum);
    printf("sum=%d\n",sm);
return 0;
}

int sum(int a,int b)
{
    return (a+b);
}

int test(int a,int b,int (*fp)(int a,int b))
{
    return fp(a,b);
}
↓↑ Menu based Program using function pointer.
#include<stdio.h>
int test(int,int,int (*fp)(int,int));
int sum(int,int);
int prod(int,int);
int div(int,int);
int sub(int,int);

int main()
{
    int a=4,b=2,choice,result,(*fp[4])(int,int);
    fp[0]=sum;
    fp[1]=sub;
    fp[2]=prod;
    fp[3]=div;
    printf("enter your choice.\n0.addition\n1.substraction\n2.multiplication\n3.division\n");
    scanf("%d",&choice);
    result=test(a,b,fp[choice]);
    printf("%d\n",result);
return 0;
}

int sum(int a,int b)
{
    printf("\nsum=");
    return (a+b);
}

int prod(int a,int b)
{
    printf("\nproduct=");
    return(a*b);
}

int sub(int a,int b)
{
    printf("\nsubstraction=");
    return (a-b);
}

int div(int a,int b)
{
    printf("\ndivision=");
    return(a/b);
}
int test(int a,int b,int (*fp)(int a,int b))
{
    return fp(a,b);
}
↓↑ Program to calculate GCD of two number using recursion.
#include<stdio.h>
int gcd(int,int);
int main()
{
 int x,y,gcd1;
 printf("enter two no.");
 scanf("%d%d",&x,&y);
 gcd1=gcd(x,y);
 printf("gcd of two no is:%d\n",gcd1);
return 0;
}
int gcd(int x,int y)
{
 if(x==y)
  return x;
 else if(x>y)
  return gcd(x-y,y);
 else
  return gcd(x,y-x); 
}       
       
↓↑ Make file demonstration
//copy all parts in different files and keep in same directory
// then simply type ( make ) on terminal

makefile ->

all.out:mainp.o sump.o squarep.o
 gcc main16.o sum16.o square16.o -o all.out
mainp.o:mainp.c
 gcc main16.c -c
sump.o:sump.c
 gcc sum16.c -c
squarep.o:squarep.c 
 gcc square16.c -c

mainp.c ->

#include<stdio.h>
#include<math.h>
int sum(int,int);
int square(int);
int main()
{
int a=3,b=8,sm,sqr;
sm=sum(a,b);
printf("sum=%d\n",sm);
sqr=square(a);
printf("sqr=%d\n",sqr);
return 0;
}

sump.c -> 

int sum(int x,int y)
{
return x+y;
}

squarep.c ->

int square(int x)
{
return x*x;
}
↓↑ Program to pass 1 D array as function argument.
#include<stdio.h>
int sumall(int*,int);
int min(int*,int);
int max(int*,int);
int main()
{
    int a[7]={10,12,19,14,9,18,11},s,mn,mx;
    s=sumall(a,7);
    mn=min(a,7);
    mx=max(a,7);
    printf("\nsum of 1-D array element is=%d\n",s);
    printf("\nmin of 1-D array element is=%d\n",mn);
    printf("\nmax of 1-D array element is=%d\n",mx);
return 0;
}
int sumall(int*p,int n)
{
    int i,sm=0;
    for(i=0;i<n;i++)
    {
        sm+=*(p+i);
        printf("%d ",*(p+i));
    }
return sm;
}
int min(int *p,int n)
{
    int a=12323,i;
    for(i=0;i<n;i++)
    {
        if(*(p+i)<a)
        {
            a=*(p+i);
        }
    }
return a;
}

int max(int *p,int n)
{
    int a=-32452,i;
    for(i=0;i<n;i++)
    {
        if(*(p+i)>a)
        {
            a=*(p+i);
        }
    }   
return a;
}
↓↑ Program to pass 2 D array as function argument.
#include<stdio.h>
int summatrix(int nr,int nc,int (*p)[7]);
int main()
{
    int a[2][7]={10,12,13,23,34,32,33,26,13,19,14,9,18,11},s;
    s=sumall(2,7,a);
    printf("\nsum of 2-D array element is=%d\n",s);
return 0;
}
int sumall(int nr,int nc,int (*p)[7])
{
    int i,j,sm=0;
    for(i=0;i<nr;i++)
    {
         printf("\n");
        for(j=0;j<nc;j++)
        {
            sm+=*(*(p+i)+j);
//            printf("%d ",*(*(p+i)+j));
        }
    }
return sm;
}

↓↑ Program to return array from function.
#include<stdio.h>
int* fun(int[],int);
int main()
{
int c[5]={4,7,8,9,6},i,*p;
p=fun(c,5);
for(i=0;i<5;i++)
printf("%d\t",*(p+i));
printf("\n");
return 0;
}

int* fun(int x[],int n)
{
 int i;
 for(i=0;i<n;i++)
 {
  x[i]=x[i]+1;
 // printf("%d\t",x[i]);
 }
 printf("\n");
return x;

}


↓↑ Program to calculate sum of N natural number usung recursion.
#include<stdio.h>
int sum(int);
int main()
{
int n,n1;
printf("enter no.");
scanf("%d",&n);
n1=sum(n);
printf("sum of %d numbers is:%d\n1",n,n1);
return 0;
}


int sum(int n)
{
if(n==0)
return 0;
else
return n+sum(n-1);
}
↓↑ Program to demonstrate typedef for function pointer.
#include<stdio.h>
typedef int (*pftype)(); 
int sum(int,int);
int main()
{
    int a=4,b=7;
    pftype pf1;
    pf1=sum; 
    pf1(a,b);
return 0;
}

int sum(int x,int y)
{
    printf("sum=%d\n",x+y);
}

Tuesday, July 22, 2014

ASCII Codes Table

ASCII
Hex

Symbol

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
TAB
LF
VT
FF
CR
SO
SI

ASCII

Hex

Symbol

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US

ASCII

Hex

Symbol

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
(space)
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/

ASCII

Hex

Symbol

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
30
31
32
33
34
35
36
37
38
39
3A
3B
3C
3D
3E
3F
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?

ASCII

Hex

Symbol

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O

ASCII

Hex

Symbol

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
50
51
52
53
54
55
56
57
58
59
5A
5B
5C
5D
5E
5F
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_

ASCII

Hex

Symbol

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
60
61
62
63
64
65
66
67
68
69
6A
6B
6C
6D
6E
6F
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o

ASCII

Hex

Symbol

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
70
71
72
73
74
75
76
77
78
79
7A
7B
7C
7D
7E
7F
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~


Sunday, July 13, 2014

c assignment 3

↓↑ Program to find second maximum element in array.
#include<stdio.h>
int main()
{
//    int arr[]={15,50,45,30,50};// here max1=50 max2=50
    int arr[]={15,50,45,30,5}; //here max1=50 max2=45
    int i,max1,max2,len=sizeof(arr)/sizeof(arr[0]);
    if(arr[0]>arr[1]){
        max1=arr[0];
        max2=arr[1];
    }else{
        max1=arr[1];
        max2=arr[0];
    }
    for(i=2;i<len;i++)
    {
        if(max1<arr[i]){
            max2=max1;
            max1=arr[i];
        }else if(max2<arr[i] /*&& max1>arr[i]*/){
                max2=arr[i];
        }
        else{}
    }
    printf("max1=%d max2=%d \n",max1,max2);
return 0;
}
↓↑ Program for number system(Binary,octal,hexadecimal,decimal)conversion.
#include<stdio.h>
int main()
{
    int a[32],b[32],i,j,choice,n,count=0;
    char ch=' ';
do{
    printf("************************************\n");
    printf("1.Decimal to binary.\n");
    printf("2.decimal to octal.\n");
    printf("3.decimal to hexadecimal.\n");
    printf("4.binary to decimal.\n");
    printf("5.binary to octal\n");
    printf("6.binary to hexadecimal.\n");
    printf("7.EXIT\n****************************\n");
 
    printf("Enter your choice:");
    scanf("%d",&choice);
    long int d,rem,n1;
    if(choice<1 && choice>7)
    {
     choice=0;
    }

    switch(choice)
    {
    case 1:
    printf("\nEnter decimal No. :");
    scanf("%d",&n);
    i=0;
    while(n)
    {
        a[i]=n%2;
        n=n/2;
        i++;
        count++;     
        }
        for(j=0,i=count-1;j<count,i>=0;j++,i--)
        {
            b[j]=a[i];
        } 
        printf("Binary no is:");
        for(i=0;i<count;i++){
        printf("%d",b[i]);}
        printf("\n");
    break;
    case 2:
    printf("\nEnter decimal No. :");
    scanf("%d",&n);
    i=0;
    while(n)
    {
        a[i]=n%8;
        n=n/8;
        i++;
        count++;     
    }
    for(j=0,i=count-1;j<count,i>=0;j++,i--)
    {
        b[j]=a[i];
    } 
    printf("octal no is:");
    for(i=0;i<count;i++){
        printf("%d",b[i]);
    }
    printf("\n");
    break;
    case 3:
    printf("\nEnter decimal No. :");
    scanf("%d",&n);
    i=0;
    while(n)
    {
        a[i]=n%16;
        n=n/16;
        i++;
        count++;     
    }
    for(j=0,i=count-1;j<count,i>=0;j++,i--)
    {
        b[j]=a[i];
    } 
    printf("hexadcimal no is:");
    for(i=0;i<count;i++)
    {
        if(b[i]==10||b[i]==11||b[i]==12||b[i]==13||b[i]==14||b[i]==15)
            printf("%c",b[i]+55);
        else 
            printf("%d",b[i]);
    }
     printf("\n");
   
    break;
    case 4:
    j=1;d=0;
    printf("Enter any binary number: ");      
    scanf("%ld",&n1);
    while(n1!=0){
        rem=n1%10;
        d=d+rem*j;
        j=j*2;
        n1=n1/10;
    }
    printf("Equivalent decimal value: %ld\n",d);
    break;
    case 5:
    j=1;d=0;
    printf("Enter any binary number: ");      
    scanf("%ld",&n1);
    while(n1!=0){
        rem=n1%10;
        d=d+rem*j;
        j=j*2;
        n1=n1/10;
    }
    printf("Equivalent octal value: %lo\n",d);
    break;
    case 6:
    j=1;d=0;
    printf("Enter any binary number: ");      
    scanf("%ld",&n1);
    while(n1!=0){
        rem=n1%10;
        d=d+rem*j;
        j=j*2;
        n1=n1/10;
    }
        printf("Equivalent hexadecimal value: %lx\n",d);
    break;
    case 7:
  
    break;
    default:
            printf("Wrong choice !!!  enter choice any of(1 to 7)");
    }
    fflush(stdin);
    printf("want to  continue enter y/n :");
    scanf(" %c",&ch);
}while(ch=='y'||ch=='Y');    
return 0;
}
↓↑ Program to implement cramer's rule to compute two unknown.
#include<stdio.h>
float det(float[2][2],int);
int main()
{

    int n=2,a1,b1,c1,a2,b2,c2,a3,b3,c3;
    printf("Enter the coefficient a1,b1,c1 (a1x+b1y=c1)of first equation: \n");
    scanf("%d%d%d",&a1,&b1,&c1);
    printf("Enter the coefficient a2,b2,c2 (a2x+b2y=c2)of second equation: \n");
    scanf("%d%d%d",&a2,&b2,&c2);
    float m[2][2]={a1,b1,a2,b2},x1[2][2]={c1,b1,c2,b2},y1[2][2]={a1,c1,a2,c2},x,y;
    x=(det(x1,n))/(det(m,n));
    y=(det(y1,n))/(det(m,n));
    printf("x=%f\ty=%f\n",x,y);
return 0;
}

float det(float matrix[2][2],int n)
{
    int i, j, k;
    float det,ratio;
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -=ratio * matrix[i][k];
                }
            }    
        }
    }
    det = 1;                                            //storage for determinant
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
    //printf("The determinant of matrix is: %.2f\n\n", det);
return det;
}
/*output:
 * 4x-3y=11
 * 6x+5y=7
 Enter the coefficient a1,b1,c1 (a1x+b1y=c1)of first equation: 
 4
 -3
 11
 Enter the coefficient a2,b2,c2 (a2x+b2y=c2)of second equation: 
 6
 5
 7
 x=2.000000 y=-1.000000
 */
↓↑ Program to implement cramer's rule to compute three unknown.
#include<stdio.h>
float det(float[3][3],int);
int main()
{

    int n=3,a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3;
    printf("Enter the coefficient a1,b1,c1,d1 (a1x+b1y+c1z=d1) of first equation: \n");
    scanf("%d%d%d%d",&a1,&b1,&c1,&d1);
    printf("Enter the coefficient a2,b2,c2,d2 (a2x+b2y+c2z=d2)of second equation: \n");
    scanf("%d%d%d%d",&a2,&b2,&c2,&d2);
    printf("Enter the coefficient a2,b2,c2,d2 (a3x+b3y+c3z=d3)of second equation: \n");
    scanf("%d%d%d%d",&a3,&b3,&c3,&d3);
    float m[3][3]={a1,b1,c1,a2,b2,c2,a3,b3,c3},x1[3][3]={d1,b1,c1,d2,b2,c2,d3,b3,c3},y1[3][3]={a1,d1,c1,a2,d2,c2,a3,d3,c3},z1[3][3]={a1,b1,d1,a2,b2,d2,a3,b3,d3},x,y,z;
     x=(det(x1,n))/(det(m,n));
     y=(det(y1,n))/(det(m,n));
     z=(det(z1,n))/(det(m,n));
     printf("x=%.2f\ty=%.2f\tz=%.2f\n",x,y,z);
return 0;
}

float det(float matrix[3][3],int n)
{
    int i, j, k;
    float det,ratio;
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -=ratio * matrix[i][k];
                }
            }
        }
    }
    det = 1;
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
return det;
}
/*Output:
 * x+2y+3z=-5
 * 3x+y-3z=4
 * -3x+4y+7z=-7
 Enter the coefficient a1,b1,c1,d1 (a1x+b1y+c1z=d1) of first equation: 
 1
 2
 3
 -5
 Enter the coefficient a2,b2,c2,d2 (a2x+b2y+c2z=d2)of second equation: 
 3
 1
 -3
 4
 Enter the coefficient a2,b2,c2,d2 (a3x+b3y+c3z=d3)of second equation: 
 -3
 4
 7
 -7
 x=-1.00    y=1.00  z=-2.00
 */
↓↑ Program to calculate determent of matrix connverting to upper triangular matrix.
#include<stdio.h>
int main()
{
    float  matrix[10][10], ratio, det;
    int i, j, k, n;
    clrscr();
    printf("Enter order of matrix: ");
    scanf("%d", &n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%f", &matrix[i][j]);
        }
    }
                                                       //  Conversion of matrix to upper triangular
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -=ratio * matrix[i][k];
                }
            }
        }
    }
    det = 1;                                            //storage for determinant
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
    printf("The determinant of matrix is: %.2f\n\n", det);
return 0;
}
↓↑ Program to generate Identity matrix.
#include<stdio.h>
int main()
{
    int a[5][5],i,j,r,c;
    printf("enter rows and columns of matrix:");
    scanf("%d %d",&r,&c);
    if(r==c)
    {
        printf("\nenter element of %dX%d matrix:",r,c);
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
    printf("\nentered matrix is ");
    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            printf("%d\t",a[i][j]);
        } 
    } 
    printf("\nidentity matrix corresponding to given matrix is:\n");
    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            if(i==j)
            {
                a[i][j]=1;
                printf("%d\t",a[i][j]);
            }
            else
           {
               a[i][j]=0;
               printf("%d\t",a[i][j]);
           }
        }
    } 
        printf("\n"); 
    }
    else
    {
        printf("to generate identity matrix rows and columns should be equal\n");
    }
return 0;
}
↓↑ Program to find minimum and maximum element in an array.
#include<stdio.h>
int main()
{
    int i,a[7],max=0,min=32432;
    printf("enter elements of array:");
    for(i=0;i<7;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<7;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
        }
        if(a[i]<min)
        {
            min=a[i];
        }
    }
    printf("\nmaximum value is:%d\nminimum value is:%d\n",max,min);
return 0;
}
↓↑ Program to Implement matrix multiplication.
#include<stdio.h>
int main()
{
    int a[3][3],b[3][3],c[3][3],i,j,k,sum=0,m,n,o,p;
    printf("\nEnter the row and column of first matrix:");
    scanf("%d %d",&m,&n);
    printf("\nEnter the row and column of second matrix:");
    scanf("%d %d",&o,&p);
    if(n!=o)
    {
        printf("Matrix mutiplication is not possible");
        printf("\nColumn of first matrix must be same as row of second matrix");
    }
    else
    {
        printf("\nEnter the First matrix->");
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                scanf("%d",&a[i][j]);      
        printf("\nEnter the Second matrix->");
        for(i=0;i<o;i++)
            for(j=0;j<p;j++)
                scanf("%d",&b[i][j]);
     
        printf("\nThe First matrix is\n");
        for(i=0;i<m;i++)
        {
            printf("\n");
            for(j=0;j<n;j++)
            {
                printf("%d\t",a[i][j]);
            }
        }         
        printf("\nThe Second matrix is\n");
        for(i=0;i<o;i++)
        {
            printf("\n");
            for(j=0;j<p;j++)
            {
                printf("%d\t",b[i][j]);
             }
        }
        for(i=0;i<m;i++)
            for(j=0;j<p;j++)
                c[i][j]=0;
        for(i=0;i<m;i++)
        {                                  //row of first matrix
            for(j=0;j<p;j++)
            {                                 //column of second matrix
                sum=0;
                for(k=0;k<n;k++)
                sum=sum+a[i][k]*b[k][j];
                c[i][j]=sum;
            }
        }
    }
    printf("\nThe multiplication of two matrix is\n");
    for(i=0;i<m;i++)
    {
        printf("\n");
        for(j=0;j<p;j++)
        {
            printf("%d\t",c[i][j]);
        }
    }
printf("\n");
return 0;
}
↓↑ Program to generate null matrix.
#include<stdio.h>
int main()
{
    int a[5][5],b[5][5],i,j,r,c;
    printf("enter rows and columns of matrix:");
    scanf("%d%d",&r,&c);
    printf("\nEnter the element of a matrix");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
         }
    }
    printf("\nEntered matrix is:\n");
    for(i=0;i<r;i++)
    { 
        for(j=0;j<c;j++)
        {
            printf("%d\t",a[i][j]);
        }
        printf("\n");
    }
    printf("\nNull matrix corresponding to given is:");
    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            b[i][j]=a[i][j]-a[i][j];
        }
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%d\t",b[i][j]);
        }  
        printf("\n");
    }
    printf("\n");
return 0;
}
↓↑ Program for pointer arithmetic.
#include<stdio.h>
int main()
{
    int x=3,*p,*p1,*p2;
    printf("intiger pointer\n");
    p=&x;
    printf("p=%u\n",p);
    p1=p+5;
    printf("p1=%u\n",p1);
    p2=p-5;
    printf("p2=%u\n",p2);
    printf("\np1++=%u \np2--=%u\np1-p2=%u\n",p1++,p2--,p1-p2);
    printf("\nfloat pointer\n");
    float y=3.2,*q,*q1,*q2;
    q=&y;
    printf("q=%u\n",q);
    q1=q+5;
    printf("q1=%u\n",q1);
    q2=q-5;
    printf("q2=%u\n",q2);
    printf("\nq1++=%u \nq2--=%u\nq1-q2=%u\n",q1++,q2--,q1-q2);
    printf("\ncharacter pointer\n");
    char r='3',*c,*c1,*c2;
    c=&r;
    printf("c=%u\n",c);
    c1=c+5;
    printf("c1=%u\n",c1);
    c2=c-5;
    printf("c2=%u\n",c2);
    printf("c1++=%u \nc2--=%u\nc1-c2=%u\n",c1++,c2--,c1-c2);
return 0;
}
/* //output 
ntiger pointer
p=106569296
p1=106569316
p2=106569276
p1++=106569316 
p2--=106569276
p1-p2=10
float pointer
q=106569300
q1=106569320
q2=106569280
q1++=106569320 
q2--=106569280
q1-q2=10
character pointer
c=106569295
c1=106569300
c2=106569290
c1++=106569300 
c2--=106569290
c1-c2=10
*/
↓↑ Program to reverse element of array .
#include<stdio.h>
#include<math.h>
int main()
{
    int i,a[]={12,10,11,33,23,13},len=sizeof(a)/sizeof(a[0]),temp;
    double alen =ceil((double)len/2);
        printf("\nArray before reverse: \n");
  
    for(i=0;i<len;i++)
    {
        printf("%d ",a[i]);
    }
    for(i=0;i<(int)alen;i++)
    {
        temp       = a[i];
        a[i]       = a[len-i-1];
        a[len-i-1] =temp;
    }
    printf("\nArray after reverse: ");
    for(i=0;i<len;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
return 0;
}
// compile with math library $gcc test.c -lm
↓↑ Program to print sum and average of array elements.
#include<stdio.h>
int main()
{
    int sum=0,avg,i,a[7];
    printf("enter elements of array:");
    for(i=0;i<7;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\nthe array is:"); 
    for(i=0;i<7;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\nsum of array element is :");
    for(i=0;i<7;i++)
    {
        sum+=a[i];
    }
    printf("%d",sum);
    printf("\naverage of array element is:%d\n",sum/7);
return 0;
}
↓↑ Program to find trace of matrix.
#include<stdio.h>
int main()
{
    int a[5][5],i,j,sum=0,m,n;
    printf("\nEnter the row and column of matrix: ");
    scanf("%d %d",&m,&n);
    printf("\nEnter the elements of matrix: ");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    printf("\nThe matrix is\n");
    for(i=0;i<m;i++)
    {
        printf("\n");
        for(j=0;j<m;j++)
        {
            printf("%d\t",a[i][j]);
        }
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i==j)
                sum=sum+a[i][j];
        }
    }
    printf("\n\ntrace of a matrix is: %d",sum);
    printf("\n");
return 0;
}
↓↑ Program to find transpose of matrix.
#include<stdio.h>
int main()
{
    int a[6][6],b[6][6],i,j,k=0,m,n;
    printf("\nEnter the row and column of matrix:");
    scanf("%d %d",&m,&n);
    printf("\nEnter the First matrix->");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);

    printf("\nThe matrix is\n");
    for(i=0;i<m;i++)
    {
        printf("\n");
        for(j=0;j<m;j++)
        {
             printf("%d\t",a[i][j]);
        }
    }
    for(i=0;i<m;i++)
       for(j=0;j<n;j++)
           b[i][j]=0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            b[i][j]=a[j][i];
        }
    }
    printf("\n\nTraspose of a matrix is -> ");
    for(i=0;i<m;i++)
    {
        printf("\n");
        for(j=0;j<m;j++)
        {
            printf("%d\t",b[i][j]);
        }
    }
    printf("\n");
return 0;
}
↓↑ Program to check that a given matrix is identity matrix or not.
#include<stdio.h>
int main()
{
    int a[5][5],b[5][5],i,j,r,c,count=0,count1=0;
    printf("enter rows and columns of matrix:");
    scanf("%d%d",&r,&c);
    printf("\nEnter the element of a matrix");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("\nEntered matrix is:\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%d\t",a[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            if(i==j && (a[i][j]==1))
            {
                ++count;
            }
            else if(i!=j && a[i][j]==0)
            {
                ++count1;
            }
        }
    }
    if(count==r && count1==(r*c-r))
    {
        printf("\nmatrix is identity");
    }
    else
        printf("\nmatrix is not identity");
    printf("\n");
return 0;
}
↓↑ Program to check null matrix.
#include<stdio.h>
int main()
{
    int a[5][5],b[5][5],i,j,r,c,count=0;
    printf("enter rows and columns of matrix:");
    scanf("%d%d",&r,&c);
    printf("\nEnter the element of a matrix");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("\nEntered matrix is:\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%d\t",a[i][j]);
        }
    printf("\n");
    }
    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            if(a[i][j]==0)
            {
                ++count;
            }
        }
    }
    if(count==r*c)
    {
        printf("\nmatrix is null");
    }
    else
        printf("\nmatrix is not null");
    printf("\n");
return 0;
}

Saturday, July 12, 2014

c assignment 2

↓↑ Print range of various data types using constants declared in limits.h .
#include<stdio.h>
#include<limits.h>
int main()
{
    printf("range of char is %ld to %ld\n ",SCHAR_MIN,SCHAR_MAX);
    printf("range of int is %ld to %ld\n ",INT_MIN,INT_MAX);
    printf("range of longint is %ld to %ld\n ",LONG_MIN,LONG_MAX);
    printf("range of shortint is %d to %d\n ",SHRT_MIN,SHRT_MAX);

return 0;
}
↓↑ prgram to display int float using various format specifiers.
#include<stdio.h>
void showbit(int n);
int main()
{
    int i=65;
    float j=1.25f;
    printf("Intiger to decimal=%d ,octal=%o hexadecimal=%x ,scientific=%e character=%c and in binary= ",i,i,i,i,i,i);
    showbit(i);
    printf("\n");
    printf("float in scientific=%e\n",j); 
return 0;
}
void showbit(int n)
{
    int i,k,shift;
    for(i=15;i>=0;i-- )
    {
        shift=1<<i;
        k=n&shift;
        k==0?printf("0"):printf("1");
    }
}
↓↑ Programm to convert ip address to 32 bit unsigned integer.
#include<stdio.h>
#include<math.h>
int main()
{
    int a,b,c,d;
    unsigned int ip;
    printf("\nEnter IP Address...\n");
    scanf(" %d%d%d%d",&a,&b,&c,&d);
    if(a>255&&a<0 && b>255&&b<0 && c>255&&c<0 && d>255&&d<0)
    {
        printf("\nInvalid Ip...\n");
    }
    ip=a<<24|b<<16|c<<8|d;
    printf("\n\n%u",ip);
return 0;
}
↓↑ Program to converts a 32 bit unsigned integer to ip address.
#include<stdio.h>
#include<math.h>
int main()
{
    unsigned int num;
    int a,b,c,d;
    printf("\nEnter 32 bit unsigned number");
    scanf("%d",&num);
    a=num>>24; 
    b=(num<<8)>>24;
    c=(num<<16)>>24;
    d=(num<<24)>>24;
    printf("\n%d.%d.%d.%d",a,b,c,d);
return 0;
}
↓↑ Program to convert binary to decimal.
#include<stdio.h>
#include<math.h>
int main()
{
    int n,a[8],b[8],i,j;
    printf("enter binary no:");
    for(i=0;i<=7;i++)
    {
        scanf("%d",&a[i]);
    } 
    for(i=7,j=0;i>=0,j<=7;i--,j++)
    { 
        n+=a[j]*pow(2,i); 
    }
    printf("no in decimal is:%d\n",n);
return 0;
}
↓↑ Program to Evaluate various expressions with bitwise operatiors.
#include<stdio.h>
int main()
{
    int a=22,b=4;
    printf("result of bitwise or  is:%d\n",a|b);
    printf("result of bitwise and is:%d\n",a&b);
    printf("result of bitwise xor is:%d\n",a^b);
    printf("result of bitwise left shift is:%d\n",a<<b);
    printf("result of bitwise right shift is:%d\n",a>>b);
    printf("result of bitwise not is:%d\n",~a);
return 0;
}
output:
result of bitwise or  is:22
result of bitwise and is:4
result of bitwise xor is:18
result of bitwise left shift is:352
result of bitwise right shift is:1
result of bitwise not is:-23
↓↑ Program to Try various format specifiers in printf, scanf .
#include<stdio.h>
int main()
{
   int i=10;
   float j=20.56333;
   printf("%5d,%-5d,%05d,%8.2f,%-8.2f,%08.2f,%.2f,%.1f\n",i,i,i,j,j,j,j,j);
return 0;
}
output:
   10,10   ,00010,   20.56,20.56   ,00020.56,20.56,20.6
↓↑ expression using conditional operator to find biggest of 3 no.s.
biggest = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
↓↑ Program In a given bit pattern(8,16 or 32 bit) write expressions for
a. set Kth bit
b. Reset Kth bit
c. Flip Kth bit
d. Query Kth bit1.
#include<stdio.h>
int main()
{
    int n,i=0,j=0,bin[32],b[32],count=0,choice,bit;
    printf("enter number:");
    scanf("%d",&n);
    while(n>=1)
    {
        bin[i]=n%2;
        n=n/2;
        i++;
        count++;
    }
    while(i>=0)
    {
        b[j]=bin[i-1];
        i--;
        j++;
    }
    printf("\nbinary no is:");
    for(i=0;i<count;i++ )
    {
        printf("%d ",b[i]);
    }
    printf("\nthere are : %d :bit in binary number\n",count);
    printf("**********************************************************\n");
    printf("Press 1,2,3,4,5 .............\n");
    printf("1.set Kth bit\n");
    printf("2.Reset Kth bit\n");
    printf("3.Flip Kth bit\n");
    printf("4.Query Kth bit\n");
    printf("5.Exit\n");
    printf("**********************************************************\n");
    printf("enter your choice:");
    scanf("%d",&choice);



    switch(choice)
    {
        case 1:
            printf("\nEnter which bit is to be set:");
            scanf("%d",&bit);
            if(b[count-bit]==1);
            else
                b[count-bit]=1;

            printf("\nBinary no after setting the bit:");
            for(i=0;i<count;i++ )
            {
                printf("%d ",b[i]);
            }
            printf("\n");
        break;
        case 2:
            printf("Enter which bit is to be Reset:\n");
            scanf("%d",&bit);
            (b[count-bit]==1)?(b[count-bit]=0):(b[count-bit]=0);

             printf("\nBinary no after setting the bit:");
             for(i=0;i<count;i++ )
             {
                 printf("%d ",b[i]);
             }
             printf("\n");

        break;
        case 3:
            printf("which bit is to be flip:\n");
            scanf("%d",&bit);
            if(b[count-bit]==0)
                b[count-bit]=1;
            else
                b[count-bit]=0;
            printf("\nBinary no after Flipping the bit:");
            for(i=0;i<count;i++ )
            {
                printf("%d ",b[i]);
            }
            printf("\n");
        break;
        case 4:
            printf("which bit is to be searched:\n");
            scanf("%d",&bit);
            printf(" %d bit is :%d\n",bit,b[count-bit]);
        break;
        case 5:


        break;
        default:
            printf("\ninvalid choice\n");
    }
return 0;
}

Saturday, July 5, 2014

c assignment1

↓↑ Different ways to swap two number.
/*swap two no. using xor operator */
#include<stdio.h>
int main()
{
        int num1=12,num2=15;
        printf("No. Before swapping a=%d,b=%d\n",num1,num2);
/*      num1=num1^num2;
        num2=num1^num2;                       
        num1=num1^num2;
*/
//or in one line
        num1 ^= num2 ^= num1 ^= num2;
        printf("no after swapping a=%d,b=%d\n",num1,num2);
return 0;
}

/*swap two no. without using third variable */
#include<stdio.h>
int main()
{
        int num1=12,num2=15;
        printf("No. Before swapping a=%d,b=%d\n",num1,num2);
/*      num1=num1+num2;
        num2=num1-num2;
        num1=num1-num2;
*/
//or in one line
        num2 = num1 + num2 - (num1 = num2);
        printf("no after swapping a=%d,b=%d\n",num1,num2);
return 0;
}

/* swap two number using pointer */
#include<stdio.h>

void swap( int *num1, int *num2)
{
    int temp ;
    temp  = *num1 ;
    *num1 = *num2 ;
    *num2 = temp ;
}

int main( )
{
    int num1=12,num2=15;

    printf("beforee swap num1=%d num2=%d \n",num1,num2);
    swap(&num1,&num2) ;
    printf("after swap num1=%d num2=%d \n",num1,num2);

    return(0);
}

/* swap using 3 variable */
#include<stdio.h>
int main()
{
    int temp,num1=12,num2=15;
    printf("\nAfter Swapping, the numbers are:num1=%d num2=%d \n",num1,num2);
    temp = num1;
    num1 = num2;
    num2 = temp;
    printf("\nAfter Swapping, the numbers are:num1=%d num2=%d \n",num1,num2);
return 0;
}

/* swap using 2 Non-Zero variable*/
#include<stdio.h>
int main()
{
 int num1=12,num2=15;//both number should be non zero
       if(num1 != 0 && num2 != 0)
       {
          printf("\nBefore Swapping, the numbers are: num1=%d num2=%d \n",num1,num2);
          num1 = num1 * num2;
          num2 = num1/num2;
          num1 = num1/num2; 
//      or
/*        num2 = num1 * num2;
          num1 = num2/num1;
          num2 = num2/num1;   
*/        printf("\nAfter Swapping, the numbers are: num1=%d num2=%d \n",num1,num2);
       }
       else
       {
          printf("\n Both the numbers should be Non-Zero!");
       }

return 0;
}
↓↑ Program to reverse 4 digit number.
#include<stdio.h>
int main()
{
    int rev=0,rem,num;
    printf("\nenter four digit no.");
    scanf("%d",&amp;num);
    while(num&gt;0){
        rem=num%10;              //last digit is being extracted
        rev=rev*10+rem;          //reverse no. is being generated by extracted no.
        num=num/10;              //extracted no is being removed from original no.
    }
    printf("reverse of four digit no=%d\n",rev);
return 0;
}
↓↑ Program to check a year is Leap Year or not
#include<stdio.h>
int main()
{
    int year;
    printf("Enter year :");
    scanf("%d",&year);
    if(((year%4==0)&&(year%100!=0))||(year%400==0))      //condition for leap year
        printf("%d is a leap year\n",year);
    else
        printf("%d is not a leap year\n",year);
return 0;
}
↓↑ Program to draw shape ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
#include<stdio.h>
int main()
{
    int row,i,j,k,space=1,limitl=71,limitr=65,count=0,temp;
    for(row=0;row<7;row++){  
         for(i=65;i<=limitl;i++){ //  first time PRINT ABCDEFG
             printf("%c",i); 
         }
         limitl--;

        if(row>0)
        {
             for(k=1;k<=space;k++) //FIRST TIME PRINT 0 SPACE 
             {
                 printf(" "); 
             }
         space=space+2;
        }

        temp=limitl;

        if(count==1)temp+=1;  // USED TO PRINT FEDCBA SECOND TIME AND ONE CHARACTER LESS ONWARDS
 
        for(j=temp;j>=limitr;j--){ // FIRST TIME PRINT FEDCBA
             printf("%c",j);
        }
        count=1;
        printf("\n");
    } 
return 0;
}
↓↑ Program to check greatest of three numbers.
#include<stdio.h>
int main()
{
    int num1,num2,num3;
    printf("Enter 3 Number :");
    scanf("%d\n%d\n%d",&num1,&num2,&num3);
    if(num1==num2 && num1==num3)                 //compare all number to each other number
        printf("all numbers are equal\n");
    else if(num1>num2 && num1>num3)              //compare first number to other numbers
        printf("num1=%d is greater\n",num1);
    else if(num2>num1 && num2>num3)              //compare second number to other numbers
        printf("num2=%d is greater\n",num2);
    else                                         //compare third number to other numbers
        printf("num3=%d is greater\n",num3);
return 0;
}
↓↑ Program to identify Quadrant of a point
#include<stdio.h>
int main()
{
    float x,y;
    printf("enter co-ordinate of a point :");
    scanf("%f\n%f",&x,&y);
    if(x==0 && y==0)                                           //condition if co-ordinate at origin
        printf("x=%f,y=%f is the origion\n",x,y);
    else if(x==0 && y>0 ||x==0 && y<0)                        //condition if co-ordinate at y axix
        printf("x=%f,y=%f point is on Y-axis\n",x,y);
    else if(x>0 && y==0 ||x<0 && y==0)                       //condition if co=ordinate at x axix
        printf("x=%f,y=%f point is on X-axis\n",x,y);
    else if(x>0 && y>0)                                     //condition if co-ordinate at first quardent
        printf("x=%f,y=%f are in 1st quadrant\n",x,y);
    else if(x<0 && y>0)                                     //condition if co-ordinate at second quardent
        printf("x=%f,y=%f are in 2nd quadrant\n",x,y);
    else if(x<0 && y<0)                                     //condition if co-ordinate at third quardent
        printf("x=%f,y=%f are in 3rd quadrant\n",x,y);
    else                                                    //condition if co-ordinate at fourth quardent
        printf("x=%f,y=%f are in 4th quadrant\n",x,y);
return 0;
}
↓↑ Recursive sum of digits in a no. Eg:- sumdigits(5689) = 28, sumdigits(28) = 10, sumdigits(10) = 1.
#include<stdio.h>
int sum(int n);
int main()
{
    int i,n,s;
    printf("enter no.");
    scanf("%d",&n);
    s=sum(n);
    printf("recursive sum is =%d of number %d\n",s,n);
return 0;
}
int sum(int n)
{
    int sum1=0,rem,rem1,sum2=0;
    while(n>0)
    {
        rem=n%10;                //last digit is being extracted                              
        sum1+=rem;               //sum of all digit is being extracted
        n=n/10;                  // exponent is being extracted
    }  
    while(sum1>0)
    {
        rem1=sum1%10;            //last digit is being extracted from sum
        sum2+=rem1;              //sum of all digit is being extracted
        sum1=sum1/10;            //exponent is being extracted
    }
return sum2;
}
↓↑ Series generation 1 + x + x^2/2! + x^3/3!+...x^n/n!
#include<stdio.h>
int main()
{
    int x,n,i;
    printf("Enter the integer value of X and N:\n");
    scanf("%d\n%d",&x,&n);
    for(i=0;i<=n;i++)
    { 
        if(i!=0)printf("+");
            printf("%d^%d/%d!",x,i,i);
    }
    printf("\n\n");
return 0;
}
↓↑ Program to calculate GCD of two no.s do-while loop
#include<stdio.h>
int main()
{
    int x,y,i;
    printf("enter two no.");
    scanf("%d%d",&x,&y);
    i=y;
    do{
        if(x%i==0 && y%i==0)
        {
            printf("gcd of two no is :%d\n",i);
            break;
        }
        i--;
    }while(i>=1);
return 0;
}
↓↑ Program to calculate GCD of two no.s while loop
#include<stdio.h>
int main()
{
    int n1,n2,g;
    printf("\nEnter two no:");
    scanf("%d\n%d",&n1,&n2);
    g=gcd(n1,n2);
    printf("gcd of %d and %d is %d\n",n1,n2,g);
return 0;
}

int gcd(int a,int b)
{
    int c;
    while(1)
    {
        c=a%b;
        if(c==0)
            return b;
        a = b;
        b = c;
     }
}
↓↑ Program to calculate LCM of two no.s
#include<stdio.h>
int main()
{
    int n1,n2,l;
    printf("\nEnter two number:");
    scanf("%d\n%d",&n1,&n2);
    l=lcm(n1,n2);
    printf("lcm of %d and %d is =%d\n",n1,n2,l);
return 0;
}
int lcm(int a,int b)
{
    int n;
    for(n=1;;n++)
    {
        if(n%a == 0 && n%b == 0)          //condition for lcm
            return n;
    }
}
↓↑ Program to calculate ncr
#include<stdio.h>
int main()
{
    int n,r,ncr;
    printf("Enter any two numbers->");
    scanf("%d %d",&n,&r);
    if(n>=r)
    {
        ncr=fact(n)/(fact(r)*fact(n-r));
        printf("The NCR factor of %d and %d is: %d\n",n,r,ncr);
    }
    else
        printf("Value of N should be equal to or greater than R\n\n");
return 0;
}
int fact(int n)
{
    int i=1;
    while(n!=0)
    {
        i=i*n;
        n--;
    }
return i;
}
↓↑ Program to Print range of prime number till given number
#include<stdio.h>
int main()
{
    int limit,i,j,flag=0;
    printf("Enter Limit for prime numbers:");
    scanf("%d",&limit);
    printf("Prime no's are 2,");
    for(i=3;i<=limit;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j==0)
                flag=1;
        }
        if(flag==0)
            printf("%d ",i);
        flag=0;
    }
    printf("\n");
return 0;
}
↓↑ Program to make triange shape1 with '*' .
#include<stdio.h>
int main()
{
    int line,i,j;
    printf("Enter the no. of lines: ");
    scanf("%d",&line);
    for(i=0;i<line;i++){
        for(j=0;j<line-i-1;j++)
            printf(" ");

        for(j=0;j<=i;j++)
            printf("* ");
        printf("\n");
    }
return 0;
}
↓↑ Program to make triange shape2 with '*'
#include<stdio.h>
int main()
{
    int line,i,j;
    printf("Enter the no. of lines: ");
    scanf("%d",&line);
    for(i=0;i<line;i++){
         for(j=0;j<line-i-1;j++)
             printf(" ");

         for(j=0;j<=i;j++)
             printf("*");
         printf("\n");
    }
return 0;
}
↓↑ Program to make triange shape3 with '*'
#include<stdio.h>
int main()
{
    int line,i,j;
    printf("enter no's of lines:");
    scanf("%d",&line);
    for(i=1;i<=line;i++)
    {
        for(j=1;j<=i;j++) 
        {
            printf("*");
        }
        printf("\n");
    }
return 0;
}
↓↑ Program to print Pascal triangle.
#include<stdio.h>
int fact(int n)
{
    int i=1;
    while(n!=0)
    {
        i=i*n;
        n--;
    }
return i;
}
int main()
{
    int line,i,j;
    printf("Enter the no. of lines: ");
    scanf("%d",&line);
    for(i=0;i<line;i++)
    {
        for(j=0;j<line-i-1;j++)
            printf(" ");

        for(j=0;j<=i;j++)
            printf("%d ",fact(i)/(fact(j)*fact(i-j)));
        printf("\n");
     }
return 0;
}
↓↑ Program to calculate Brute force multiplicative inverse
#include<stdio.h>
int modInverse(int a, int m) {
    a %= m;
    int x;
    for(x = 1; x < m; x++) {
        if((a*x) % m == 1) return x;
    }
}
int main()
{
    printf("\nmultiplicative inverse = %d\n",modInverse(42,2017));
return 0;
}
↓↑ Program for Extended Euclidean Algorithm to calculate multiplicative inverse
#include<stdio.h>
int mul_inv(int a, int b)
{
    int b0 = b, t, q;
    int x0 = 0, x1 = 1;
    if (b == 1) return 1;
    while (a > 1) {
        q = a / b;
        t = b, b = a % b, a = t;
        t = x0, x0 = x1 - q * x0, x1 = t;
    }
    if (x1 < 0) x1 += b0;
return x1;
}
int main(void) {
    printf("%d\n", mul_inv(42, 2017));
return 0;
}
↓↑ Electricity bill calculation as per following tariff upto 100 Units : Rs.1,100-199 Units : Rs.2 200 – 399 units : Rs.3,400 units and above – Rs.4
#include<stdio.h>
int main()
{
    int unit,rs;
    printf("enter unit:");
    scanf("%d",&unit);
    if(unit<=99)                                 //total bill if unit <=99
    {
        rs=unit;
    }
    else if(unit>=100 && unit<=199)              //total bill if unit<= 199
    {
        rs=99+(unit-99)*2;
    }
    else if(unit>=200 && unit<=399)            //total bill if unit<=399
    {
        rs=299+(unit-199)*3;
    }
    else                                        //total bill if unit>=399
    {
        rs=899+(unit-399)*4;
    }

    printf("electricity bill of unit %d is %d\n",unit,rs);
return 0;
}
↓↑ Student grade using switch case as per following criteria >= 70% - A grade, 60-69% - B grade 50 – 59%: C Grade, 30-49% - D grade <30% - Fail
#include<stdio.h>
int grade(int p)
{
    if(p>=70)                                   // condition for A grade 
        return 1;
    else if(p>=60 && p<=69)                     // condition for B grade
        return 2;
    else if(p>=50 && p<=59)                      //condition for C grade
        return 3;
    else if(p>=30 && p<=49)                     //condition for D grade
        return 4;
    else                                          //condition for FAIL
        return 5;
}
int main()
{
    int percentage,i;
    printf("enter percentage:");
    scanf("%d",&percentage);
    i=grade(percentage);
    switch(i)
    { 
        case 1:
            printf("congratuation you passed with A-GRADE\n");
        break;
        case 2:
            printf("congratuation you  passed with B-GRADE\n");
        break;
        case 3:
            printf("congratuation you  passed with C-GRADE\n");
        break;
        case 4:
            printf("you passed with D-GRADE \n");
         break;
        default:
            printf("Sorry you are FAIL\n");                          
    }
return 0;
}