[go: up one dir, main page]

0% found this document useful (0 votes)
274 views12 pages

CP Codes

The document contains C code snippets for various basic programs including Hello World, sum and average of numbers, salary calculation, area of shapes, simple interest calculation, greater or lesser of two numbers, even or odd, marks percentage calculation, loops, functions, pointers, arrays, structures, strings and more. All programs demonstrate fundamental C programming concepts.

Uploaded by

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

CP Codes

The document contains C code snippets for various basic programs including Hello World, sum and average of numbers, salary calculation, area of shapes, simple interest calculation, greater or lesser of two numbers, even or odd, marks percentage calculation, loops, functions, pointers, arrays, structures, strings and more. All programs demonstrate fundamental C programming concepts.

Uploaded by

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

//hello world

#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}

//sum and average


#include<stdio.h>
int main()
{
int n1,n2,n3,s;
float a;
printf("enter any three numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
s=n1+n2+n3;
a=s/3;
printf("the sum and average of %d %d %d are %d and %f
respectively.",n1,n2,n3,s,a);
return 0;
}

//salary
#include<stdio.h>
int main()
{
float bp,da,hra,gs;
printf("enter your base package:");
scanf("%f",&bp);
da=0.4*bp;
hra=0.2*bp;
gs=bp+da+hra;
printf("the DA,HRA and GS of %f are %f and %f and %f
respectively.",bp,da,hra,gs);
return 0;
}

//area
#include<stdio.h>
int main()
{
float r,s,l,b,a1,a2,a3;
printf("enter the radius of circle, side of square, length and breadth of
rectangle:");
scanf("%f%f%f%f",&r,&s,&l,&b);
a1=3.14*r*r;
a2=s*s;
a3=l*b;

printf("the areas of circle , square and rectangle are %f and %f and %f


respectively.",a1,a2,a3);
return 0;
}

//simple interest
#include<stdio.h>
int main()
{
float p,r,t,s;
printf("enter the principle amount,rate of interest and time :");
scanf("%f%f%f",&p,&r,&t);
s=(p*r*t)/100;
printf("the simple interest is Rs.%f",s);
return 0;
}

//greater or lesser and even or off


#include <stdio.h>

int main()
{
int a,b;

printf("Enter two numbers = ");


scanf("%d%d",&a,&b);
if(a>b)
printf("%d is greater.",a);
else if(b>a)
printf("%d is greater.",b);
else
printf("both %d %d are equal",a,b);

if(a%2==0)
printf("%d is even.",a);
else
printf("%d is odd",a);

if(b%2==0)
printf("%d is even.",b);
else
printf("%d is odd",b);
return 0;
}

//marks
#include<stdio.h>
int main()
{
int a,b,c,d;
float p;
printf("enter marks out of 100 in each of 4 subjects:");
scanf("%d%d%d%d",&a,&b,&c,&d);
p=(a+b+c+d)*100/400;
printf("\n the percentage is %f \n.",p);

if(p>=90)
{printf("1st class");}

else if((p<90) && (p>=80))


{printf("2nd class");}

else if((p<80) && (p>=60))


{printf("2nd class");}

else if((p<60) && (p>=50))


{printf("3rd class");}

else if((p<50) && (p>=35))


{printf("pass");}

else
{printf("fail");}

return 0;
}

//hello loop
#include<stdio.h>
int main()
{ int n,i;
printf("enter a number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Hello World\n");
}
return 0;
}
//factorial loop
#include<stdio.h>
int main()
{ long int n,i,f=1;
printf("enter a number\n");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n its factorial is %ld\n",f);
return 0;
}

//while series of natural no.


#include<stdio.h>
int main()
{ int i;
i=1;
while(i<=10)

{
printf("\t %d",i);
i++;
}

return 0;
}

//do while table of 5


#include<stdio.h>
int main()
{ int i;
i=5;
do
{if(i%5==0)
{printf("\t%d",i);}
i++;
}
while(i<=100);

return 0;
}

//sum with return


#include<stdio.h>
int sum (int x,int y);
int main()
{
int a,b,s;
printf("\n Enter any two numbers to find their sum.");
scanf("%d",&a);
scanf("%d",&b);
s=sum(a,b);
return 0;
}
int sum (int x,int y)
{
int s;
s=x+y;
printf("\n The sum is=%d.",s);
return s;
}

//sum without return


#include<stdio.h>
int sum (int x,int y);
int main()
{
int a,b,s;
printf("\n Enter any two numbers to find their sum.");
scanf("%d",&a);
scanf("%d",&b);
s=sum(a,b);
printf("\n The sum is=%d.",s);
return 0;
}
int sum (int x,int y)
{
int s;
s=x+y;

//factorial recursion

#include<stdio.h>
int f(int n);
int main()
{
int no,fc;
printf("\n Enter any number to find its factorial.");
scanf("%d",&no);

fc=f(no);
printf("\n The factorial is=%d.",fc);
return 0;
}
int f(int n)
{

if(n==0 || n==1)
return 1;
else

return n*f(n-1);
}

//fibonacci series

#include<stdio.h>
int f(int n);
int main()
{
int no,i;
printf("\n Enter any number to find its factorial.");
scanf("%d",&no);

for(i=0;i<=no;i++)
printf("\n %d.",f(i));
return 0;
}
int f(int n)
{

if(n==0)
return 0;
else if(n==1)
return 1;
else

return (f(n-1)+f(n-2));
}

//pointers
#include<stdio.h>
int main()
{
int i=3;
int *j;
j=&i;
printf("\n address of i=%u",&i);
printf("\n address of i=%u",j);
printf("\n value of i=%d",i);
printf("\n value of i=%d",*j);
printf("\n value of i=%d",*(&i));
printf("\n address of j=%u",&j);
printf("\n value of j=%u",j);
printf("\n value of j=%u",&i);

return 0;
}

//swap by address
#include<stdio.h>
int s(int *a, int *b);
int main()
{
int a,b;

printf("Enter two numbers = ");


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

return 0;

int s(int *a, int *b)


{

int t;
t=*a;
*a=*b;
*b=t;
printf("on swapping %d %d",*a,*b);
return 0;
}

//swap by value
#include<stdio.h>
int s(int a, int b);
int main()
{
int a,b;
printf("Enter two numbers = ");
scanf("%d%d",&a,&b);
s(a,b);

return 0;

int s(int a, int b)


{
int t;
t=a;
a=b;
b=t;
printf("on swapping %d %d",a,b);
return 0;
}

//matrix sum
#include<stdio.h>
int main()
{
int r,c,a[100][100],b[100][100],s[100][100]i,j;
printf("enter rows and columns.\n");
scanf("%d%d",&r,&c);
printf("enter a matrix elements \n");
for(i=0;i<=r;i++)
dfor(j=0;j<=c;j++)
{printf("element a[i][j]=");
scanf("%d",&a[i][j]);}

printf("enter b matrix elements \n");


for(i=0;i<=r;i++)
for(j=0;j<=c;j++)
{printf("element b[i][j]=");
scanf("%d",&b[i][j]);}

for(i=0;i<=r;i++)
for(j=0;j<=c;j++)
{s[i][j]=a[i][j]+b[i][j];}

for(i=0;i<=r;i++)
{ for(j=0;j<=c;j++)
{printf("\t%d",s[i][j])}
printf("\n");
}
return 0;
}

//structure
#include<stdio.h>

struct student
{
char name[20];
int roll_no;
int p,c,m,t;
};

int main()
{

struct student s[100],temp;


int n,i,j;
printf("enter no of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter students name ,roll no ,marks in physics ,chemistry and
maths.");

scanf("%s%d%d%d%d",s[i].name,&s[i].roll_no,&s[i].p,&s[i].c,&s[i].m);
s[i].t=s[i].p+s[i].c+s[i].m;
}

for(i=0;i<n;i++)
{ for(j=0;j<(n-1);j++)
{if (s[j].t>s[j+1].t)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
printf("name \t roll no \t physics \t chemistry \t maths\t total \n");
printf("--------------------------------------------------------------\n");
for(i=0;i<n;i++)
{printf("%s\t%d\t%d\t%d\t%d\t%d\n",s[i].name,s[i].roll_no,s[i].p,s[i].c,s[i].
m,s[i].t);}
return 0;
}
//structure copilot
#include<stdio.h>

struct student
{
char name[20];
int roll_no;
int p,c,m,t;
};

int main()
{
struct student s[100],temp;
int n,i,j;
printf("enter no of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter students name, roll no, marks in physics, chemistry and
maths.");
scanf("%s%d%d%d%d",s[i].name,&s[i].roll_no,&s[i].p,&s[i].c,&s[i].m);
s[i].t=s[i].p+s[i].c+s[i].m;
}

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


{
for(j=0; j<(n-i-1); j++)
{
if(s[j].t < s[j+1].t)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

printf("name \t roll no \t physics \t chemistry \t maths\t total \n");


printf("--------------------------------------------------------------
\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\n",s[i].name,s[i].roll_no,s[i].p,s[i].c
,s[i].m,s[i].t);
}
return 0;
}
//max min array
#include<stdio.h>

int main()
{int a[50],n,i,min,max;
printf("enter size of array=");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
max=a[0];
for(i=1;i<n;i++)
{if(max<a[i])
max=a[i];
}
printf("max is %d",max);
min=a[0];
for(i=1;i<n;i++)
{if(min>a[i])
min=a[i];
}
printf("min is %d",min);
return 0;
}

//ascending order
#include<stdio.h>

int main()
{int a[50],n,i,j,min,temp,max;
printf("enter size of array=");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}

for(i=0;i<n-1;i++)
{
for(j=0;j<(n-i-1);j++)

{if(a[j]>a[j+1])
{temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

}
printf("array in ascending order is:");
for(i=0;i<n;i++)
{printf("\t %d",a[i]);}
return 0;

//palindrome
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
int i,len,flag=0;
printf("enter a string");
fgets(a, sizeof(a), stdin);
len=strlen(a);
for(i=0;i<len;i++)
{if (a[i]==a[len-i-1])
flag=1+flag;}

if(flag !=len)
{printf("palingdrome");}

else
{printf("not palingdrome");}
return 0;
}

You might also like