0% found this document useful (0 votes)
13 views46 pages

C Codings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views46 pages

C Codings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Acumulation

Hello world program


#include<stdio.h>

main()

printf("hello CMC Madhyamgram!");

Sum and Sub of Two number

#include<stdio.h>

main()

int a,b,c,d;

printf("1st no:");

scanf("%d",&a);

printf("2nd no:");

scanf("%d",&b);

c=a+b;

d=a-b;

printf("a=%d",a);

printf("\nb=%d",b);

printf("\n sum=%d", c);

printf("\n sub=%d", d);

//Sum, sub, multi, div, remainder and average of two numbers

#include<stdio.h>

main()

int a,b,c,d,e,f,g,h=0;
printf("1st no");

scanf("%d",&a);

printf("2nd no");

scanf("%d",&b);

c=a+b;

d=a-b;

e=a*b;

f=a%b;

g=a/b;

h=(a+b)/2;

printf("The sum is %d\n",c);

printf("The substract is %d\n",d);

printf("The multiplication is %d\n",e);

printf("The modules is %d\n",f);

printf("The division is %d",g);

printf("The average is %d",h);

//Power and Sqare root function

#include<stdio.h>

#include<math.h>

main()

int i=10;

int j=pow(i,2);

printf("i=%d",i);

printf("\nj=%d",j);

int k=sqrt(j);
printf("\n k=%d",k);

//Sum of square and cube using Pow() function

#include<stdio.h>

#include<math.h>

main()

int a,b,c,sum,d=0;

printf("enter 1st no:");

scanf("%d",&a);

printf("enter 2nd no:");

scanf("%d",&b);

c=pow(a,2);

d=pow(b,3);

sum=c+d;

printf("sum is %d",sum);

If Statement

//odd , even checking

#include<stdio.h>

int main()

int n;

printf("Enter a number: ");


scanf("%d",&n);

if(n%2==0)

printf("%d is an even number.",n);

else

printf("%d is an odd number.",n);

//input marks of 3 subjects display pass or fail and grade

#include<stdio.h>

main()

int sub1,sub2,sub3,total;

float avg;

printf("Enter marks of 3 subjects:\n");

scanf("%d%d%d",&sub1,&sub2,&sub3);

total=sub1+sub2+sub3;

printf("Total=%d",total);

avg=(float)total/3;

printf("\nAverage=%f",avg);

if(avg>=40)

printf("\nPass");

else

printf("\nFail");

if(avg>=75)
{

printf("\nGrade A");

else if(avg>=60)

printf("\nGrade B");

else if(avg>=50)

printf("\nGrade C");

else if(avg>=40)

printf("\nGrade D");

else

printf("\nGrade F");

Ternary Operator

//odd , even checking using ternary Operator

#include<stdio.h>

int main()

int n;

printf("Enter a number: ");


scanf("%d",&n);

n%2==0?printf("%d is an even number.",n):printf("%d is an odd number.",n);

//positive , negative checking using ternary operator

#include<stdio.h>

int main()

int n;

printf("Enter a number: ");

scanf("%d",&n);

n%2==0?printf("%d is an even number.",n):printf("%d is an odd number.",n);

//Positive , negative, nutral using nested ternary operator

#include<stdio.h>

int main()

int n;

printf("Enter a number: ");

scanf("%d",&n);

n>0?printf("%d is a positive number.",n):

n<0?printf("%d is a negative number.",n):

printf("%d is a nutral number.",n);

/*

discount list

=================

20000 and above 20%


15000 and above 15%

10000 and above 10%

5000 and above 5%

otherwise NIL

*/

//Input pur. amt. and calculate dis. amt and net amt.

#include<stdio.h>

int main()

float p,d,n;

printf("Enter a purchase amount: ");

scanf("%f",&p);

d=(p>=20000)?p*0.2:(p>=15000)?p*0.15:(p>=10000)?p*0.1:(p>=5000)?p*0.05:0;

printf("Discount=%.2f",d);

n=p-d;

printf("\nNet Amount=%.2f",n);

Switch

//Simple calculator program (Sum,Sub,Multi and Div) using Switch

#include<stdio.h>

main()

int n,m,ch,r;

float d;

printf("Enter two no.:\n ");

scanf("%d%d",&n,&m);

printf("Menu\n");
printf("=====\n");

printf("1. Add \n2. Sub \n3. Multi \n4. Div \nEnter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1: r=n+m;

printf("Sum=%d",r);

break;

case 2: r=n-m;

printf("Sub=%d",r);

break;

case 3: r=n*m;

printf("Multi=%d",r);

break;

case 4: d=(float)n/m; //type casting -- convert one type to another explicitly or


forcely

printf("Div=%f",d);

break;

default: printf("Invalid choice.");

break;

//Simple calculator program (Sum,Sub,Multi and Div) using Switch character

#include<stdio.h>

main()

int n,m,r;

float d;
char ch;

printf("Enter two no.:\n ");

scanf("%d%d",&n,&m);

printf("Menu\n");

printf("=====\n");

printf("+. Add \n-. Sub \n*. Multi \n/. Div \nEnter your choice: ");

fflush(stdin); //to clear input buffer

scanf("%c",&ch);

switch(ch)

case '+': r=n+m;

printf("Sum=%d",r);

break;

case '-': r=n-m;

printf("Sub=%d",r);

break;

case '*': r=n*m;

printf("Multi=%d",r);

break;

case '/': d=(float)n/m; //type casting -- convert one type to another explicitly or
forcely

printf("Div=%f",d);

break;

default: printf("Invalid choice.");

break;

For
//wap to display integer no. upto n.

#include<stdio.h>

main()

int n,i,s=0;

printf("Enter last no.: ");

scanf("%d",&n);

for(i=1;i<=n;i++)//i++ increament by 1

printf("\n%d",i);

s=s+i;

printf("\nSum=%d",s);

//wap to display odd no. upto n.

#include<stdio.h>

main()

int n,i,s=0;

printf("Enter last no.: ");

scanf("%d",&n);

for(i=1;i<=n;i+=2)//i++ increament by 2 full form i=i+2

printf("\n%d",i);

s=s+i;

printf("\nSum=%d",s);
}

//wap to display even no. upto n.

#include<stdio.h>

main()

int n,i,s=0;

printf("Enter last no.: ");

scanf("%d",&n);

for(i=2;i<=n;i+=2)//i++ increament by 2 full form i=i+2

printf("\n%d",i);

s=s+i;

printf("\nSum=%d",s);

//wap to display integer no. upto n in reverse order

#include<stdio.h>

main()

int n,i,s=0;

printf("Enter last no.: ");

scanf("%d",&n);

for(i=n;i>=1;i--)//i-- decreament by 1 full form i=i-1

printf("\n%d",i);

s=s+i;

}
printf("\nSum=%d",s);

//wap to display even no. upto n in reverse order

#include<stdio.h>

main()

int n,i,s=0;

printf("Enter last no.: ");

scanf("%d",&n);

for(i=n;i>=1;i-=2)//i-=2 decreament by 2 full form i=i-2

printf("\n%d",i);

s=s+i;

printf("\nSum=%d",s);

//wap to display odd no. upto n in reverse order

#include<stdio.h>

main()

int n,i,s=0;

printf("Enter last no.: ");

scanf("%d",&n);

n=n-1; //n--

for(i=n;i>=1;i-=2)//i-=2 decreament by 2 full form i=i-2

printf("\n%d",i);
s=s+i;

printf("\nSum=%d",s);

//wap to display multiple table of n.

#include<stdio.h>

main()

int n,i;

printf("Enter no.: ");

scanf("%d",&n);

for(i=1;i<=10;i++)

printf("\n%dx%d=%d",n,i,n*i);

//wap to display square series upto n.

#include<stdio.h>

main()

int n,i,p;

printf("Enter no.: ");

scanf("%d",&n);
for(i=1;i<=n;i++)

p=pow(i,2); // pow(base,power);

printf("\n%d^2=%d",i,p);

//wap to display cube series upto n.

#include<stdio.h>

main()

int n,i,p;

printf("Enter no.: ");

scanf("%d",&n);

for(i=1;i<=n;i++)

p=pow(i,3); // pow(base,power);

printf("\n%d^3=%d",i,p);

//wap to display finonaccii series upto nth term.

#include<stdio.h>
main()

int n,i,a=0,b=1,c,s=0;

printf("Enter term: ");

scanf("%d",&n);

for(i=1;i<=n;i++)

a=a+b;

c=a;

a=b;

b=c;

printf("\n%d",a);

s=s+a;

printf("\nSum=%d",s);

//wap to display finonaccii series upto nth term.

#include<stdio.h>

main()

int n,i,a=1,b=0,c,s=0;

printf("Enter term: ");

scanf("%d",&n);

for(i=1;i<=n;i++)
{

a=a+b;

c=a;

a=b;

b=c;

printf("\n%d",a);

s=s+a;

printf("\nSum=%d",s);

//Diamond pattern

#include <stdio.h>

int main()

int n, c, k, space = 1;

printf("Enter number of rows\n");

scanf("%d", &n);

space = n - 1;

for (k = 1; k <= n; k++)

for (c = 1; c <= space; c++)

printf(" ");

space--;
for (c = 1; c <= 2*k-1; c++)

printf("*");

printf("\n");

space = 1;

for (k = 1; k <= n - 1; k++)

for (c = 1; c <= space; c++)

printf(" ");

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)

printf("*");

printf("\n");

return 0;

//M Pattern

#include<stdio.h>

main()
{

int i,j,n,k,bk;

printf("Enter rows:");

scanf("%d",&n);

bk=0;

for(i=n;i>=1;i--)

for(j=1;j<=i;j++)

printf("%d",j);

for(k=1;k<=2*bk-1;k++)

printf(" ");

for(j=i;j>=1;j--)

if(j!=n)

printf("%d",j);

bk++;

printf("\n");

While

//display integer no. upto n with sum

#include<stdio.h>

main()

int n,i,s=0;

printf("enter last no.: ");

scanf("%d",&n);
i=1;//initialization

while(i<=n)

printf("\n%d",i);

s=s+i;

i++;//increament

printf("\nSum=%d",s);

//display odd no. upto n with sum

#include<stdio.h>

main()

int n,i,s=0;

printf("enter last no.: ");

scanf("%d",&n);

i=1;//initialization

while(i<=n)

printf("\n%d",i);

s=s+i;

i+=2;//increament

printf("\nSum=%d",s);

//display even no. upto n with sum

#include<stdio.h>
main()

int n,i,s=0;

printf("enter last no.: ");

scanf("%d",&n);

i=2;//initialization

while(i<=n)

printf("\n%d",i);

s=s+i;

i+=2;//increament i=i+2;

printf("\nSum=%d",s);

//display integer no. in reverse upto n with sum

#include<stdio.h>

main()

int n,i,s=0;

printf("enter last no.: ");

scanf("%d",&n);

i=n;//initialization

while(i>=1)

printf("\n%d",i);

s=s+i;

i--;//decrement
}

printf("\nSum=%d",s);

//display odd no. in reverse upto n with sum

#include<stdio.h>

main()

int n,i,s=0;

printf("enter last no.: ");

scanf("%d",&n);

i=n-1;//initialization

while(i>=1)

printf("\n%d",i);

s=s+i;

i-=2;

printf("\nSum=%d",s);

//input a no. display digits,no. digits and sum of digits

#include<stdio.h>

main()

int n,d,s=0,c=0;

printf("Enter a no.: ");

scanf("%d",&n);

while(n>0)
{

d=n%10;

printf("\nDigit=%d",d);

s=s+d;

n=n/10;

c++;

printf("\nSum of digits=%d",s);

printf("\nNo. of digits=%d",c);

//input a no. display reverse no.

#include<stdio.h>

main()

int n,d,r=0;

printf("Enter a no.: ");

scanf("%d",&n);

while(n>0)

d=n%10;

r=r*10+d;

n=n/10;

printf("\nReverse no.=%d",r);

#include<stdio.h>
main()

int n,m,d,r=0;

printf("Enter a no.:");

scanf("%d",&n);

m=n;

while(m>0)

d=m%10;

r=r*10+d;

m=m/10;

printf("\nReverse no.=%d",r);

if(n==r)

printf("\npalindrome number");

else

printf("\npalindrome not number");

Array

//heightest and lowest no. of array

#include<stdio.h>

main()

int a[10],n,i,h,l,p1,p2;

//input

printf("Enter no. of elements: ");

scanf("%d",&n);
printf("Enter elements: \n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

p1=p2=0;

h=l=a[0];

for(i=1;i<n;i++)

if(a[i]>h)

h=a[i];

p1=i;

if(a[i]<l)

l=a[i];

p2=i;

printf("\nHeightest no =%d at %d",h,p1+1);

printf("\nLowest no =%d at %d",l,p2+1);

//Linear of array

#include<stdio.h>

main()

{
int a[10],n,i,x,f=0;

//input

printf("Enter no. of elements: ");

scanf("%d",&n);

printf("Enter elements: \n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter no. to search: ");

scanf("%d",&x);

//logic

for(i=0;i<n;i++)

if(a[i]==x)

printf("%d found at %d",x,i+1);

f++;

break;

if(f==0)

printf("%d not found",x);

//Binary Search

#include<stdio.h>

int main()
{

int c,first,last,middle,n,search,array[1000];

printf("enter number of elements\n");

scanf("%d",&n);

printf("enter %d integers \n",n);

for(c=0;c<n;c++)

scanf("%d",&array[c]);

printf("enter value to find\n");

scanf("%d",&search);

first=0;

last=n-1;

middle=(first+last)/2;

while(first<=last)

if (array[middle]<search)

first=middle+1;

else if(array[middle]==search)

printf("%d found at location %d\n",search,middle+1);

break;

else

last=middle-1;

middle=(first+last)/2;
}

if(first>last)

printf("not found!%d is not present in the list\n ",search);

// return 0;

Matrix

Matrix Multiplication [2x2 matrix]

Matrix Multiplication [3x3 matrix]

#include<stdio.h>

main()

int i,j,k,l;

int matrix[3][3],matri[3][3],matr[3][3];

printf("enter 1st matrix!\n");

for(i=0;i<=3;i++)

printf("\n enter #%d row:",(i+1));

for(j=0;j<3;j++)

scanf("%d",&matrix[i][j]);

printf("\n\n");

printf("enter 2nd matrix!");

for(i=0;i<3;i++)

printf("\n enter #%d row:",(i+1));

for(j=0;j<3;j++)
scanf("%d",&matri[i][j]);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

matr[i][j]=0;

for(j=0;j<3;j++)

for(i=0;i<3;i++)

for(l=0;l<3;l++)

matr[i][j]=matr[i][j]+(matrix[i][l]*matri[l][j]);

printf("matrix is:\n\n");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

printf("%4d",matr[i][j]);

printf("\n\n");

}
Factorial

#include<stdio.h>

main()

int n,i,fact=1;

printf("Please enter a number:\n");

scanf("%d",&n);

for(i=n;i>=1;i--)

fact=fact*i;

printf("The factorial of the number is: %d\n",fact);

Pointer

//DISPLAY VALUE AND ADDRESS OF A VARIABLE

#include<stdio.h>

main()

int a;

printf("Enter a value:");

scanf("%d",&a);

printf("value: %d \n",a);

printf("Address:%u",&a);
}

//DISPLAY VALUE AND ADDRESS OF A VARIABLE USING POINTER

#include<stdio.h>

main()

int a,*p;

p=&a;

printf("Enter a value:");

scanf("%d",&a);

printf("value of a: %d \n",a);

printf("Address of a:%u \n",&a);

printf("Value of p:%u \n",p);

printf("Address of p:%u \n",&p);

printf("Value of a through p:%d \n",*p);

//DISPLAY ARITHMETIC OPERATIONS USING POINTER

#include<stdio.h>

main()

int a,*p,s;

float d;

p=&a;

printf("Enter a value:");

scanf("%d",&a);

s=*p+5;

printf("SUM:%d \n",s);
s=*p-5;

printf("SUB:%d \n",s);

s=*p*5;

printf("MULTIPLICATION:%d \n",s);

d=(float)*p/5;

printf("DIVISION:%f \n",d);

//Find even no. from array using Pointer

#include<stdio.h>

main()

int numbers[40];

int*ptr;

int i=0;

int sum=0;

printf("Please enter numbers one by one!");

for(i=0;i<5;i++)

scanf("%d",&numbers[i]);

ptr=&numbers[0];

for(i=0;i<5;i++)

if(*ptr%2==0)
sum+=*ptr;

ptr++;

printf("Result=%d",sum);

String Function

//String Copy [strcpy]

#include<stdio.h>

#include<string.h>

main()

char fn[10],ln[10];

printf("first name :");

gets(fn);

printf("last name :");

gets(ln);

strcpy(fn,ln);

printf("last name=%s",fn);

//String Join/Concatination [strcat]

#include<stdio.h>

#include<string.h>

main()

char fn[20],ln[10];

printf("first name :");

gets(fn);
printf("last name :");

gets(ln);

strcat(fn," ");

strcat(fn,ln);

printf("full name=%s",fn);

//String Compare [strcmp]

#include<stdio.h>

#include<string.h>

main()

char fn[10],ln[10];

printf("first name :");

gets(fn);

printf("last name :");

gets(ln);

if(strcmp(fn,ln)==0)

printf("first name is same as last name");

else

printf("first name is not same as last name");

// Combination of strcpy and strcat

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

int main()

char first[100];

char last[100];

char full_name[200];

strcpy(first,"first");

strcpy(last,"last");

strcpy(full_name,first);

strcat(full_name," ");

strcat(full_name,last);

printf("the full name is%s\n",full_name);

return(0);

//Sorting using String function

#include<stdio.h>

#include<string.h>

main()

int i,j;

char name[5][20],temp[20];

for(i=0;i<5;i++)

printf("enter any name:");

scanf("%s",&name[i]);

for(i=0;i<4;i++)
{

for(j=i+1;j<5;j++)

if(strcmp(name[i],name[j]>0))

strcpy(temp,name[i]);

strcpy(name[i],name[j]);

strcpy(name[j],temp);

printf("Display after Sorting\n";

for(i=0;i<5;i++)

printf("%s\n",name[i]);

//Pallindrom in String Function

#include<stdio.h>

#include<string.h>

main()

char word[20],temp[20];

printf("enter any word:");

gets(word);

strcpy(temp,word);

strrev(temp);
if(strcmp(word,temp)==0)

printf("%s is a pallindromic\n",word);

else

printf("%s is not pallindromic\n",word);

UDF

//Sum of Two numbers using UDF

#include<stdio.h>

main()

int a=150;

int b=150;

int ret;

ret=sum(a,b);

printf("Sum of the value is: %d\n",ret);

return 0;

int sum(int x,int y)

int s=x+y;

return s;

}
Average

#include<stdio.h>

main()

int a=150;

int b=50;

int av=(x+y)/2;

printf("Average of the value is: %d\n",av);

return 0;

Average using UDF

#include<stdio.h>

main()

int a=150;

int b=50;

int r;

r=average(a,b);

printf("Average of the value is: %d\n",r);

return 0;

int average(int x,int y)

int av=(x+y)/2;

return av;
}

Factorial using UDF

#include<stdio.h>

main()

int n,f;

printf("Please enter a number:\n");

scanf("%d",&n) ;

f=factorial(n);

printf("\nThe factorial of the number is: %d\n",f);

int factorial(int x)

int fact=1,i;

for(i=x;i>=1;i--)

fact=fact*i;

return fact;

HCF & LCM using UDF

#include<stdio.h>

main()

int n,m,h;
printf("Enter two number:");

scanf("%d%d",&n,&m);

h=hcf(n,m); //calling

printf("HCF=%d",h);

printf("\nLCM=%d",lcm(n,m));

int hcf(int x,int y)//definition

int small,i,hc;

if(x>y)

small=y;

else

small=x;

for(i=1;i<=small;i++)

if(x%i==0 && y%i==0)

hc=i;

return hc;
}

int lcm(int x,int y)

int p,h,l;

h=hcf(x,y);//calling

p=x*y;

l=p/h;

return l;

//call by address

#include<stdio.h>

void swap(int *,int *);

main()

int a,b;

printf("Enter 2 no.s: ");

scanf("%d%d",&a,&b);

swap(&a,&b);

printf("a=%d\nb=%d",a,b);

void swap(int *p,int *q)

int t;

t=*p;

*p=*q;

*q=t;
}

//Macro Define

//Sum, Sub, Multi, div, remainder and average using macro define function

#include<stdio.h>

#define add(x1,x2) x1+x2

#define sub(x1,x2) x1-x2

#define div(x1,x2) x1/x2

#define mult(x1,x2) x1*x2

#define mod(x1,x2) x1%x2

#define avg(x1,x2) (x1+x2)/2

main()

int c,d,e,f,g,h,z=0;

int a=20;

int b=10;

z=add(a,b);

c=sub(a,b);

d=div(a,b);

e=mult(a,b);

f=mod(a,b);

g=avg(a,b);

printf("sum is %d\n",z);

printf("sub is %d\n",c);

printf("div is %d\n",d);

printf("mult is %d\n",e);

printf("mod is %d\n",f);

printf("avg is %d\n",g);
}

//Cube using macro define function

#define CUBE(x) x*x*x

#include<stdio.h>

main()

int k=5;

int j=0;

j=CUBE(k);

printf("value of j is %d\n",j);

//File Handling Append Mode

#include<stdio.h>

main()

int a,b,c=0;

printf("1st no.");

scanf("%d",&a);

printf("2nd no.");

scanf("%d",&b);

c=a+b;

printf("sum is \t%d",c);

FILE*fp;

fp=fopen("D:\\abc2.txt","a");

fprintf(fp,"1st no %d\n 2nd no. %d\n sum %d",a,b,c);

fclose(fp);

printf("file created!");
}

File handling Write mode with structure

#include<stdio.h>

struct student

int roll;

char name[20];

int total;

};

main()

int i;

struct student s1[5];

FILE *fp;

for(i=0;i<5;i++)

printf("Enter roll,name and total: ");

scanf("%d",&s1[i].roll);

fflush(stdin);

gets(s1[i].name);

scanf("%d",&s1[i].total);

fp=fopen("student.txt","w");

if(fp==NULL)

printf("File not created or open...");


exit(0);

for(i=0;i<5;i++)

fprintf(fp,"\n%-3d%-20s%-3d",s1[i].roll,s1[i].name,s1[i].total) ;

printf("5 records inserted into file...");

fclose(fp);

File handling Append mode with structure

#include<stdio.h>

struct student

int roll;

char name[20];

int total;

};

main()

int i;

struct student s1[5];

FILE *fp;

for(i=0;i<5;i++)

printf("Enter roll,name and total: ");


scanf("%d",&s1[i].roll);

fflush(stdin);

gets(s1[i].name);

scanf("%d",&s1[i].total);

fp=fopen("student.txt","a");

if(fp==NULL)

printf("File not created or open...");

exit(0);

for(i=0;i<5;i++)

fprintf(fp,"\n%-3d%-20s%-3d",s1[i].roll,s1[i].name,s1[i].total) ;

printf("5 records inserted into file...");

fclose(fp);

//File Handling Read Mode

#include<stdio.h>

main()

char x;

FILE *fp;

fp=fopen("student.txt","r");
x=fgetc(fp);

while(x!=EOF)

printf("%c",x);

x=fgetc(fp);

fclose(fp);

Advanced Prog.

//Line command and File command

#include<stdio.h>

main()

printf("A\n");

printf("C FILE %s LINE %d\n",__FILE__,__LINE__);

#line 100

printf("B\n");

printf("C FILE %s LINE %d\n",__FILE__,__LINE__);

#line 200

printf("D\n");

printf("C FILE %s LINE %d\n",__FILE__,__LINE__);

printf("E\n");

printf("C FILE %s LINE %d\n",__FILE__,__LINE__);

You might also like