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

C Program List

It's the list of the programing list of c

Uploaded by

shab52434
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)
81 views

C Program List

It's the list of the programing list of c

Uploaded by

shab52434
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/ 22

1) C program to Find the sum of digits and reverse of a number.

Algorithm

Step 1 : start
Step 2 : read the number ,num
Step 3 : rev=0,sum=0
Step 4 : while num>0
Step 5 : d=num%10;
Step 6 : num=num/10;
Step 7 : sum=sum+d;
Step 8 : rev=rev*10+d;
Step 9 : end of while
Step 10: print sum,rev
Step 11: stop

program

#include<stdio.h>
#include<conio.h>
void main( )
{
int num,sum=0,rev=0,d;
clrscr( );
printf("Enter the number: ");
scanf("%d",&num);

while(num)
{
d=num%10;
num=num/10;
sum=sum+d;
rev=rev*10+d;
}
printf("Sumof digits = %d",sum);
printf("\nReverse of the number = %d",rev);
getch( );
}
Output :
Enter the number :123
sum of digits = 6
Reverse of the number = 321
2 ) C program to Find first n Fibonacci numbers

Algorithm

Step 1 : start
Step 2 : enter howmany numbers ,num
Step 3 : fib1=0,fib2=1,count=2
Step 4 : print fib1,fib2
Step 5 : while count<num
Step 6 : fib3 = fib1 + fib2
Step 7 : count++
Step 8 : print fib3
Step 9 : fib1 = fib2
Step 10 : fib2 = fib3
Step 11 : end of while
Step 12 : stop

Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int fib1 = 0, fib2 = 1, fib3, num, count = 0;
clrscr();
printf("Enter the value of num \n");
scanf("%d", &num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2;
while (count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
Output:
Enter the value of num:7
First 7 FIBONACCI numbers are ...
0 1 1 2 3 5 8
3) C program to Create a pyramid using ‘*’.
Program :
#include<stdio.h>
#include<conio.h>

void main()
{
int num , space,i,j,k;
clrscr();
printf("Enter a value : ");
scanf("%d",&num);

space=num-1;
for(i=1; i<=num; i++)
{
for(space=1; space<=(num-i); space++)
{
printf(" ");
}

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


{
printf("*");
}

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


{
printf("*");
}
printf("\n");
}
getch();
}

Output
4 ) C program to Find the number of words in a sentence.

Program

#include<stdio.h>
#include<conio.h>

void main()
{
char str[30];
int wc=1,i;
clrscr();
printf("\n enter a sentace\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
wc++;
}
printf("\n no of word=%d",wc);
getch();
}

output

Enter a sentance :

hai how are you

No of words=4
5) C program to Check whether a number is prime or not

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,f=0;
clrscr();
printf("\n enter the number");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if((n%i)==0)
{
f=1;
break;
}
}
if(f==1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
getch();
}

output

enter the number :5

5 is a prime number
6) C program to Perform matrix transpose

Program :

#include <stdio.h>
void main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
scanf("%d", &a[i][j]);
}

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


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

printf("\nTranspose of the matrix:\n");


for (i = 0; i < c; ++i)
{
for (j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);

}
printf("\n");
}
}
output

Enter rows and column: 2 3


Enter matrix elements: 4 5 7 2 1 6
Entered matrix:
4 5 7
2 1 6
Transpose of the matrix:
4 2
5 1
7 6
7) C program to Display the short form of a string.

Program

#include<stdio.h>
#include<string.h>
int main()
{
char str[100],*ptr,i,l;
clrscr();
printf("Enter any string\n");
gets(str);
l=strlen(str);
ptr=str;
printf("%c",*(ptr+0));
printf("The short form of %s is",str);
for(i=1;i<l;i++)
{
if(*(ptr+i-1)==' ')
printf(" %c ",*(ptr+i));
}
getch();
}

output:

Enter any string :Computer Science


The short form of Computer Science is CS
8) C program to Find the Armstrong numbers within a given range.

Program

#include <stdio.h>
#include <math.h>
#include<conio.h>

int main()
{
int low, high, i, temp1,r, result = 0;
clrscr();
printf("Enter two range");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d an %d are: ", low,
high);

for(i = low + 1; i < high; ++i)


{

temp1 = i;
while (temp1 != 0)
{
r = temp1 % 10;
result = result+(r*r*r);
temp1 = temp1/10;
}

if (result == i)
{
printf("%d ", i);
}
result = 0;

}
getch();
}

output

Enter two range : 100 500


Armstrong numbers between 100 an 500 are:153 370 371 407
9) C program to Check for leap year

Program

#include<stdio.h>
#include<conio.h>

void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch();
}

output:

Enter a year:2000
2000 is a leap year
10) Find the currency denomination of a given amount.

Program :

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c,d,f,g,h,n,k;
clrscr();
printf("enter the amount of money\n");
scanf("%d",&n);
f=n/2000;
if(f!=0)
printf("number of 2000 rupees notes: %d\n",f);
n=n%2000;
a=n/500;
if(a!=0)
printf("number of 500 rupees notes: %d\n",a);
n=n%500;
k=n/200;
if(k!=0)
printf("number of 200 rupees notes: %d\n",k);
n=n%200;
b=n/100;
if(b!=0)
printf("number of 100 rupees notes: %d\n",b);
n=n%100;
c=n/50;
if(c!=0)
printf("number of 50 rupees notes: %d\n",c);
n=n%50;
d=n/20;
if(d!=0)
printf("number of 20 rupees notes: %d\n",d);
n=n%20;
f=n/10;
if(f!=0)
printf("number of 10 rupees notes: %d\n",f);
n=n%10;
g=n/5;
if(g!=0)
printf("number of 5 rupees notes: %d\n",g);
n=n%5;
h=n/2;
if(h!=0)
printf("number of 2 rupees notes: %d\n",h);
n=n%2;
if(n!=0)
printf("number of 1 Rupee notes: %d\n",n);
getch();
}

OUTPUT:
11. C program to Write odd and even numbers into separate files

Program

#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp,*fp1,*fp2;
int c,i;
clrscr();
fp=fopen("data.txt","w");
printf("enter the numbers");
for(i=0;i<10;i++)
{
scanf("%d",&c);
putw(c,fp);
}
fclose(fp);
fp=fopen("data.txt","r");
fp1=fopen("even.txt","w");
fp2=fopen("odd.txt","w");
while((c=getw(fp))!=EOF)
{
if(c%2==0)
putw(c,fp1);
else
putw(c,fp2);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
fp1=fopen("even.txt","r");
printf("\neven numbers are :");
while((c=getw(fp1))!=EOF)
printf("%4d",c);
printf("\n\n");
fp2=fopen("odd.txt","r");
printf("\nodd numbers are :");
while((c=getw(fp2))!=EOF)
printf("%4d",c);
getch();
fcloseall();
}
OUTPUT
12. C program to Merge two numeric arrays in sorted order

Program
#include <stdio.h>
#include <conio.h>

void merge(int [], int, int [], int, int []);


void main()
{
int a[100], b[100], m, n, c, sorted[200];
clrscr();
printf("Input number of elements in first
array\n");
scanf("%d", &m);
printf("Enter the array elements");
for (c = 0; c < m; c++)
scanf("%d", &a[c]);
printf("Input number of elements in second
array\n");
scanf("%d", &n);

printf("Enter the array elements");


for (c = 0; c < n; c++)
scanf("%d", &b[c]);

merge(a, m, b, n, sorted);
printf("Sorted array:\n");

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


printf("%d\n", sorted[c]);
getch();
}
void merge(int a[], int m, int b[], int n, int sorted[])
{
int i, j, k;

j = k = 0;

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


{
if (j < m && k < n)
{
if (a[j] < b[k])
{
sorted[i] = a[j];
j++;
}
else
{
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m)
{
for (; i < m + n;)
{
sorted[i] = b[k];
k++;
i++;
}
}
else
{
for (; i < m + n;)
{
sorted[i] = a[j];
j++;
i++;
}
}
}
}

output:
13. C program to Fill upper triangle with 1, lower triangle with -1
and diagonal elements with 0

Program

#include <stdio.h>
#include <conio.h>
int main()
{
int a[10][10], i, j, m, n;
printf("Enter the order : ");
scanf("%d%d", &m, &n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (i < j)
a[i][j] = 1;
else if (i > j)
a[i][j] = -1;
else
a[i][j] = 0;
}
}
printf("\nThe matrix :\n");
for (i = 0; i < m; i++)
{
printf("\n");
for (j = 0; j < n; j++)
printf("%4d", a[i][j]);
}
getch();
}

OUTPUT:
14) C program to Find the factorial of a number using recursion

Program :

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int x,n;
clrscr();
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);

x=fact(n);
printf(" Factorial of %d is %d",n,x);

getch();
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

OUTPUT

15. C program to Check for palindrome string.


Program

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

void main()
{
char string1[20];
int i, length;
int f = 0;
clrscr();

printf("Enter a string:");
scanf("%s", string1);

length = strlen(string1);

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


{
if(string1[i] != string1[length-i-1])
{
f = 1;
break;
}
}
if (f)
printf("%s is not a palindrome", string1);
else
printf("%s is a palindrome", string1);
getch();
}

OUTPUT

16. C program to find slandered deviation of n numbers


Program :
#include<stdio.h>
#include<math.h>
#include<conio.h>

int main ()
{
float Price[50];
int i, Number;
float Mean, Variance, SD, Sum = 0, Differ, Varsum=0;
clrscr();
printf ("Please Enter the N Value = ");
scanf ("%d", &Number);
printf ("\nPlease Enter %d real numbers\n",Number);
for (i = 0; i < Number; i++)
{
scanf ("%f", &Price[i]);
}
for (i = 0; i < Number; i++)
{
Sum = Sum + Price[i];
}
Mean = Sum / (float) Number;
for (i = 0; i < Number; i++)
{
Differ = Price[i] - Mean;
Varsum = Varsum + pow (Differ, 2);
}
Variance = Varsum / (float) Number;
SD = sqrt (Variance);
printf ("Standard deviation = %f\n", SD);
getch();
}

Output:

Please Enter the N Value = 5


Please Enter 5 real numbers = 11 22 33 44 55
Standard deviation = 15.56

17. Convert a decimal to new base


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i;
clrscr();
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
getch();
}

Output:

Enter the number to convert: 25


Binary of Given Number is : 11001

18.Number of vowels in a string


Program :

#include <stdio.h>
#include <conio.h>

void main()
{

char str[100];
int i, vowels = 0;
clrscr();
printf("Enter the string: ");
scanf("%s",&str);
for(i = 0; str[i]; i++)
{

if(str[i]=='a'|| str[i]=='e'||str[i]=='i'||
str[i]=='o'|| str[i]=='u'||str[i]=='A'||
str[i]=='E'||str[i]=='I'||str[i]=='O' ||str[i]=='U')
{
vowels++;
}
}

printf("Total number of vowels: = %d\n",vowels);

getch();
}

Output:

Enter the string: Malayalam


Total number of vowels: 4

You might also like