0% found this document useful (0 votes)
61 views

Cprog

The document contains code snippets for various C programming concepts like arrays, loops, functions, conditional statements etc. Some key programs included are: 1. Matrix operations like addition, multiplication and diagonal traversal. 2. Searching and sorting algorithms like binary search and bubble sort. 3. Pattern printing programs to create shapes like pyramids, diamonds, triangles. 4. Mathematical programs to calculate factorials, Fibonacci series, sum of digits. 5. Programs to check for Armstrong, palindrome and leap years. 6. Conversion between temperature scales and units of measurement.

Uploaded by

Rahul Jain
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Cprog

The document contains code snippets for various C programming concepts like arrays, loops, functions, conditional statements etc. Some key programs included are: 1. Matrix operations like addition, multiplication and diagonal traversal. 2. Searching and sorting algorithms like binary search and bubble sort. 3. Pattern printing programs to create shapes like pyramids, diamonds, triangles. 4. Mathematical programs to calculate factorials, Fibonacci series, sum of digits. 5. Programs to check for Armstrong, palindrome and leap years. 6. Conversion between temperature scales and units of measurement.

Uploaded by

Rahul Jain
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ANTI DIAGONAL

#include<conio.h>
#include<stdio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(enter which number you want;);
scanf(%d,&c);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i+j= =3)
a[i]]j]=c;
else
a[i][j]=0
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(%d,a[i][j]);
printf(\n);
}
getch( );
}
ARMSTRONG
# include <stdio.h>
# include <conio.h>
main( )
{
int n, a, b, c, d;
clrscr( );
printf ( Enter a Three Digit Number: );
scanf (%d, &n);
a=n/100;
b=((n/10)%10);
c=n%10;
d=a*a*a*+b*b*b +c*c*c;
if (n= =d)
printf (The Given Number is Armstrong number);
else
printf (The Given Number is Not Armstrong number);
getch( );
}
BINARY SEARCH
# include <stdio.h>
# include <conio.h>
main( )
{
int a[100],i,n,x, mid, top, bot,c;
clrscr();

printf(enter the array size;);


scanf(%d,&n);
printf(enter the array elements);
for(i=1;i<=n;i++)
scanf(%d,&a[i]);
top=1;
bot=n;
c=0;
printf(enter the element to searched);
scanf(%d,&x);
while((top <=bot)&&(c==0))
{
mid=(top+bot)/2;
if(a[mid]<x)
top=mid+1;
else
if(a[mid]>x)
bot=mid-1;
else
c=1;
}
if(c==1)
printf(elements is at position;%d,mid);
else
printf(elements is not in list);
getch( );
}
BUBBLE SORT
# include <stdio.h>
# include <conio.h>
main( )
{
int a[100],i,j,n,t;
clrscr( );
printf(enter the array size);
scanf(%d,&n);
for(i=1;i<n;i++)
scanf(%d,&a[i]);
for(i=1;i<=n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i]
a[i]=a[j];
a[j]=t;
}
printf(the sorted elements are );
for(i=1;i<=n;i++)
print(%d,a[i]);
getch( );
}

CHARPYAMID2
# include <stdio.h>
# include <conio.h>
main( )
{
char i,j,n,r;
int s,sp=40;
clrscr( );
for(i=65;i<=75;i+=2)
{
for(s=1;s<=sp;s++)
printf( );
for(j=65;j<i;j++)
printf(%c,j);
printf(\n);
sp=sp-2;
}
sp=sp+4;
for(n=73;n>=65;n-=2)
{
for(s=1;s<=sp;s++)
printf( );
for(r=65;r<=n;r++)
printf(%c,r);
sp=sp+2;
}
getch( );
}
CHARPYRAMID
include <stdio.h>
# include <conio.h>
main( )
{
char i,j;
clrscr();
for(i=65;i<=70;i++)
{
for(j=65;j<=i;j++)
printf(%c,j);
printf(\n);
}getch( );
}
COUNT CHARACTER
# include <stdio.h>
# include <conio.h>
main( )
{
char ch;
int c=0,s=0,s1=0;

clrscr( );
printf(enter a string :);
while(( ch=getchar( ))!=\n)
{
if(ch>=A&& ch>=Z)
c=c+1;
else
if(ch>=a&& ch>=z)
s=s+1;
else
s1=s1+1;
}
printf( no of capital letters are %d,c);
printf( no of smal1 letters are %d,s);
printf( no of special characters are %d,s1);
getch( );
}
FACTORIAL
# include <stdio.h>
# include <conio.h>
main( )
{
int n,f;
clrscr( );
printf(enter a number:)
scanf(%d,&n);
f= fact(n);
printf(factorial value is %d,f);
getch();
}
int fact(int n)
{
int i, fa=1;
for(i=n;i>=1;i--)
fa=fa*i;
return fa;
}
FIBANNOCI
# include <stdio.h>
# include <conio.h>
main( )
{
int a=0,b=1,c=0,i;
clrscr( );
printf(%d,a);
printf(\n%d,b);
for (i=1; i<=10; i++)
{
c=a+b;
printf(\n%d,c);

a=b;
b=c;
}
getch( );
}
FLOYDS TRIANGLE
# include <stdio.h>
# include <conio.h>
main( )
{
int i,n,s,r k=1;
clrscr( );
printf(enter a number of rows);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
for(s=1;s<=40-i;s++)
printf( );
for(j=1;j<=i;j++)
printf(%3d,k++);
printf(\n);
}
getch( );
}
FREQUENT DISTRIBUTION
#include<stdio.h>
#include<conio.h>
main( )
{
int a[20],i,n1=0,n2=0,n3=0,n4=0,n5=0;
clrscr();
printf(enter the any 20 no of range(1-25));
for(i=1;i<=20;i++)
scanf(%d,&a[i]);
for(i=1;i<=20;i++)
if((a[i]>=1)&&(a[i]<6))
n1++;
else
if((a[i]>5)&&(a[i]<11))
n2++;
else
if((a[i]>10)&&(a[i]<16))
n3++;
else
if((a[i]>15)&&(a[i]<21))
n4++;
else
if((a[i]>20)&&(a[i]<26))
n5++;
printf(class interval frequency);

printf(\n
printf(\n
printf(\n
printf(\n
printf(\n
getch();
}

1-5 %d,n1);
6-10 %d,n2);
11-15 %d,n3);
16-20 %d,n4);
21-25 %d,n5);

FAREN HEAT TO CELSIUS


# include <stdio.h>
# include <conio.h>
# include <string.h>
void main()
{
float tc,tf;
clrscr();
printf("Enter the temperature in Fahrenheit:");
scanf("%f",&tf);
tc=((tf-32)*5)/9;
printf("The calculated temperature in Celsius is: %.2f",tc);
getch();
}
SUM OF INDIVIDUAL DIGITS- 3 NUMBER
# include <stdio.h>
# include <conio.h>
main( )
{
int a,b,c,n, sum;
clrscr( );
printf ( Enter a Three Digit Number:);
scanf (%d,&n);
a=n/100;
b=( (n%100)/10);
c=n%10;
sum=a+b+c;
printf ( Sum of Individual Digits of Given Numbers is %d, Sum);
getch( );
}
SUM OF INDIVIDUAL DIGITS- 2 NUMBER
# include <stdio.h>
# include <conio.h>
main( )
{int a,b,c,d;
clrscr( );
printf ( Enter a two digit number :);
scanf ( %d, &a);
b=a/10;
c=a%10;
d=b+c;
printf (sum of individual digits of given numbers id %, d);

getch( );
}
LARGEST OF 3 NUMBER
# include <stdio.h>
# include <conio.h>
main( )
{
int a,b,c,big=0;
clrscr( );
printf(ENTER VALUE FOR A:);
scanf(%d,&a);
printf(ENTER VALUE FOR B:);
scanf(%d,&b);
print(ENTER VALUE FOR C:);
scanf(%d,&c);
if (a>big)
big=a ;
if(b>big)
big=b;
if (c>big)
big=c;
printf (BIGGEST OF ABOVE GIVEN THREE NUMBER IS %d,big)
getch( );
}
LEAP YEAR
# include <stdio.h>
# include <conio.h>
main( )
{
int y;
clrscr( );
printf(enter a year:);
scanf(%d,&y);
if(y%4==0& &y%100!=0|| y%400==0);
printf(the above given year IS a leap year);
else
printf(the above given year IS not a leap year);
getch();
}
MATRIX ADDITION
# include <stdio.h>
# include <conio.h>
main( )
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr( );
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{

printf(enter the two values for a[%d][%d] & b[%d][%d], i,j,i,j);


scanf(%d%d,&a[i][j],&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(%d,c[i][j]);
}
printf(\n);
}
getch( );
}
MATRIX DIAGONAL
include<conio.h>
#include<stdio.h>
main()
{
int a[4][4],i,j;
clrscr( );
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i==j)
c[i][j]=7;
else
a[i][j]=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(%d,a[i][j]);
printf(\n);
}
getch();
}
MATRIX MULTIPLICATION
# include <stdio.h>
# include <conio.h>
main( )
{
int a[10][10],b[10][10],c[10],[10],i,j,m,n,p,q,k;
clrscr( );
printf(enter the size of first matrices);
scanf(%d%d,&m,&n);
printf(enter the size of second matrix);
scanf(%d%d,&p,&q);
if(n==p)
{
printf(enter first matrices elements);
for(i=1;i<m;i++)

for(j=1;j<n;j++)
scanf(%d,&a[i][j]);
printf(enter second matrix elements);
for(i=1;i<p;i++)
for(j=1;j<q;j++)
scanf(%d,&b[i][j]);
for(i=1;i<m;i++)
for(j=1;j<n;j++)
{
c[i][j]=0;
for(k=1;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf(the multiplication matrix is);
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
print(%2d,c[i][j]);
printf(\n);
}
}
else
printf(multiplication is not possible);
getch( );
}
MATRIX SUBTRACTION
# include <stdio.h>
# include <conio.h>
main( )
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr( );
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf(enter the two values for a[%d][%d] & b[%d][%d], i,j,i,j);
scanf(%d%d,&a[i][j],&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf(%d,c[i][j]);
}
printf(\n);
}
getch( );
}

INCHES TO CENTIMETERS
include <stdio.h>
# include <conio.h>
void main()
{
float i,c;
clrscr();
printf("Enter the no. of Inches:");
scanf("%f",&i);
c=2.54*i;
printf("%.2f inches =%.2fCms",i,c);
getch();
}
CHANGE CASE
# include <stdio.h>
# include <conio.h>
main( )
{
char ch,c1;
clrscr( );
printf(enter a char in anycase);
ch=getchar();
if(ch>=65 && ch<=90)
c1=ch+32;
else
if(ch>=97 && ch<=122)
c1=ch-32;
printf(the given char in anothercase IS);
putchar(c1);
getch();
}
MAXIUM AND MINIMUM
#include<stdio.h>
#include<conio.h>
main( )
{
int i,n,a[10],min,max;
clrscr( );
printf( enter how many number);
scanf(%d,&n);
printf(enter the elements);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
min=a[0];
for(i=0;i<n;i++)
if(min>a[i])
min=a[i];
printf(minimum=%d,min);
max=0;
for(i=0;i<n;i++)

if(max<a[i]);
max=a[i];
printf(\n maximum=%d,max);
getch( );
}
MULTI TABLE
# include <stdio.h>
# include <conio.h>
main( )
{
int i,t;
clrscr( );
printf(which table u want:);
scanf(%d,&t);
for (i=1; i<=10; i++)
printf(\n%d*%d=%d,t,i,i*t);
getch( );
}
REVERSING OF NATURAL NUMBERS
# include <stdio.h>
# include <conio.h>
main( )
{
int i;
clrscr( );
for (i=10; i>=1; i--)
printf(%d\n,i);
getch( );
}
NUMERIC DIAMOND
include <stdio.h>
# include <conio.h>
main( )
{
int i,j,l,n,s,k=40;
clrscr( );
for(i=1;i<=9;i+=2)
{
for(l=1;l<=k;l++)
printf( );
for(j=1;j<=i;j++)
printf(\n);
k=k-2;
}
k=k+4;
for(n=7;n>=1;n-=2)
{
for(i=1;i<=k;i++)

printf( );
for(s=1;s<n;s++)
printf(%d,s);
printf(\n);
k=k+2;
}
getch( );
}
NUMERIC PYRAMID 1
# include <stdio.h>
# include <conio.h>
main()
{
int i,j;
clrscr( );
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf(%d,j);
printf(\n);
}
getch();
}
NUMERIC PYRAMID

# include <stdio.h>
# include <conio.h>
main( )
{
int i,j ,l,k=40;
clrscr( );
for(i=1;i<=9;i+=2)
{
for(l=1;l<=k;l++)
printf( );
for(j=1;j<=i;j++);
printf(%d,j);
printf(\n);
k=k-2;
}
getch( );}
ODD NUMBERS FROM 1 TO 10
# include <stdio.h>
# include <conio.h>
main( )
{
int i;
clrscr( );
for (i=1; i<=10; i+=2)
printf(%d\n,i);
getch( );
}

ODD OR EVEN
# include <stdio.h>
# include <conio.h>
main()
{
int n,r;
clrscr();
printf(ENTER A NUMBER ;);
scanf(%d, &n);
r=n%2;
if(r= = 0)
printf(the above given number is even number);
else
printf(the above given number is odd number);
getch();
}
PASCAL TRIANGLE
#include<stdio.h>
#include<conio.h>
main()
{
int n,p=1,q,num,sp;
clrscr( );
printf(enter the number of rows);
scanf(%d,&n);
for(p=0;p<=n;p++)
{
for(sp=1;sp<=40-(3*p);sp++)
printf( );
for(q=0;q<n;q++)
{
if((q==q)||(q==0))
num=1;
else
num=num*((q-q)+1)/q;
printf(%2d,num);
printf(\n);
}}
getch( );
}
SWAPPING USING POINTERS
# include <stdio.h>
# include <conio.h>
void interchange(int *x,int *y);
main( )
{i
nt a,b;
clrscr( );
printf(enter values of a and b);
scanf(%d%d,&a,&b);

interchange(&a,&b);
}
void interchange(x,y)
int *x,*y;
{
int t;
t=*x;
*x=*y;
*y=t;
printf(%d=x, %d=y,*x,*y);
getch( );
}
PRIME NUMBER OR NOT
# include <stdio.h>
# include <conio.h>
main( )
{
int n, i, check;
clrscr();
for(i=1;i<=100;i++)
{
check=1;
for(n=2;n<=i/2;n++)
if(i%n= =0)
{
check=0;
break;
}
if(check= =1)
printf(\n %d is a prime,i);
else
printf(\n %d is not a prime,i);
}
getch( );
}
REVERSE OF A GIVEN NUMBER
# include <stdio.h>
# include <conio.h>
main( )
{int a,b,n;
clrscr( );
printf(enter a number:)
scanf(%d,&n);
a=rev(n);
printf(REVERSE OF A GIVEN NUMBER IS %d,a);
b=add(n,a);
printf(\n sum of a given and reverse number is %d,b);
getch( );
}
int rev( int n)

{
int r,rev=0,s;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
return rev;
}
int add(int n, int a)
{
return n+a;
}
/Program to accept 10 numbers and print first five numbers in original
order and print last five
numbers in reverse order.//
# include <stdio.h>
# include <conio.h>
main( )
{
int i,a[10];
for(i=0;i<10;i++)
{
printf(enter value for a[%d],i);
scanf(%d,&a[i]);
}
for(i=0;i<=4;i++)
printf(\nA[%d]=%d,i,a[i]);
for(i=9;i>=5;i--)
printf(\nA[%d]=%d,i,a[i]);
getch( );
}
SELECTION SORT
#include<stdio.h>
#include<conio.h>
main( )
{
int a[100],i,n,j,t,min,pos;
clrscr();
printf(enter the array size);
scanf(%d,&n);
printf(enter the elements);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
for(i=0;i<n;i++)
{
min=a[i];
pos=i;
for(j=0;j<n-1;j++)

if(min>a[j])
{
min=j;
pos=j;
}
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
printf(the sorted elements are);
for(i=0;i<n;i++)
printf(%2d,a[i]);
getch( );
}
Program To Find Whether The Given Matrix Is Skew Symmetric or Symmetric
# include <stdio.h>
# include <conio.h>
main( )
{
int a[10][10],i,j,m,n,c=0,c1=0;
clrscr( );
printf(enter the array size);
scanf(%d,&n);
printf(enter the elements);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf(%d,&a[i][j]);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(a[i][j]==a[j][i])
c=1;
else
if(a[i][j]==a[j][i])
c1=1;
}
printf(the given matrix is \n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf(%4d,a[i][j]);
printf(\n);
}
if(c==0)
printf(the given matrix is symmetric);
else
if(c1==0)
printf(the matrix is skew symmetric);
else
printf(none of two);
}
getch( );}

STRING COMPARE
# include <stdio.h>
# include <conio.h>
int getline (char line[ ], int lim );
int strc(char str1[ ], char str2[] );
main( )
{
char str1[80],str2[80];
int comp;
clrscr( );
printf(enter first string:);
getline(str1,80);
printf(enter second string:);
getline(str2,80);
comp=strc(str1,str2);
if(comp>0)
printf(first string is bigger);
else
if(comp==0)
printf(both the strings are equal);
getch( );
}
int getline(char str[], int lin)
{
int i;
for(i=0;i<lin&&((str[i]=getchar())!=\n);i++);
if(str[i]=\0)
return i;
}
int strc(char str1[],char str2[])
{
int i;
for(i=0;str1[i];i++)
if(str1[i]!=str2[i])
return str1[i]-str2[i];
return str1[i]-str2[i];
}
Program To Find Which String Is Bigger
#include<stdio.h>
#include<conio.h>
int getline(char line[],int lim);
main( )
{
char str1[80],str2[80];
int len1,len2;
clrscr( );
printf(enter first string);
len1=getline(str1,80);
printf(enter second string);
len2=getline(str1,80);
if(len1 >len2)

printf(first string bigger than second string);


else
if(len1<len2)
printf(second string bigger than first string);
else
printf(both strings are equal);
getch( );
}
int getline(char line[],int lim)
{
int i;
for(Ii0;i<lim && ((line[i]=getchar( ))!=\n);i++)
if(line[i]==\n)
line[i]=\0;
return i;
}
STRING PLAINDROME
# include <stdio.h>
# include <conio.h>
main( )
{
int i,lim,c,check=1;
char word[80];
clrscr( );
printf( enter a string);
for(i=0;i<80 && ((word [i]= getchar())!=\n);i++);
lim=i-1;
c=lim/2;
for(i=0;i<=0;i++,lim--)
if(word[i]!= word[lim])
{
check=0;
break;
} if(check= = 1)
printf(the given string is palindrome );
else
printf( not palindrome);
getch( );
}
STRING REVERSE
# include <stdio.h>
# include <conio.h>
main( )
{
int i,j;
char name[80];
clrscr( );
printf( enter a string);
gets(name);
for(i=0;i<80 && ((name [i]= getchar())!=\n);i++);

if(name[i]= =\n)
name[i]=\0;
for(j=i;j>=0;j--)
putchar(name[j]);
printf(is the reverse of given string);
getch( );
}
STUDENT DETAILS USING STRUCTURES
# include <stdio.h>
# include <conio.h>
main( )
{
struct student
{
char name[20];
int m1,m2,m3, tot;
char result[10];
}stud[10];
int i,n;
clrscr( );
printf(enter no of students \n);
scanf(%d,&n);
for(i=0;i<n;i++)
{
printf(enter %d student deatails \n,i);
printf(enter name\n);
scanf(%s, stud[i].name);
printf(enter marks of 3 subjects \n);
scanf(%d%d%d, &stud[i].m1,&stud[i].m2,&stud[i].m3);
stud[i].tot=stud[i].m1+stud[i].m2+stud[i].m3;
if((stud[i].m1>35)&&(stud[i].m2>35)&&(stud[i].m3>35))
strcpy(stud[i].result,pass);
else
strtcpy(stud[i].result,fail);
}clrscr( );
printf(name total result \n);
for(i=0;i<n;i++)
{
printf(%s %d %s \n, stud[i].name,stud[i].tot,stud[i].result);
}
getch( );
}
SUM USING FUNCTIONS
# include <stdio.h>
# include <conio.h>
main( )
{int a,b,c;
clrscr();
printf(enter the value for a:)

scanf(%d,&a);
printf(enter the value for b:)
scanf(%d,&b);
c=add(a,b);
printf(sum of two numbers is %d,c);
getch( );
}
int add(int x, int y)
{
int z;
z=x+y;
return z;
}
SUM OF NATURAL NUMBERS
# include <stdio.h>
# include <conio.h>
main( )
{
int n,sum=0,i;
clrscr( );
for (i=1; i<=10; i++)
sum=sum+i;
printf(sum of natural numbers from 1 to 10 is %d\n,sum);
getch( );
}
AREA OF TRIANGLE
# include <stdio.h>
# include <conio.h>
main( )
{
int a,b,c;
float s, area;
clrscr( );
printf(enter there sides of the triangle);
scanf(%d%d%d,&a,&b,&c);
if((a+b)<c||(b+c)<a||(a+c)<b)
printf(finding area is not possible);
else
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(area=%.2f,area);
getch( );
}

You might also like