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

Computer Programs

The document contains C code for 20 programs that perform various numeric and string operations: 1) Summing digits of a number, reversing digits, finding largest digit, summing odd/even positioned digits 2) Checking for prime numbers between 50-100 3) Counting occurrences of a digit in a number 4) Counting vowels in a word 5) Counting words in a string 6) Calculating number of days between dates 7) Computing area of a circle 8) Checking for a leap year 9) Computing row and column sums of a matrix 10) Checking if a number is even or odd 11) Computing square root of a number 12) Swapping two variables

Uploaded by

AmitKumar
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)
100 views

Computer Programs

The document contains C code for 20 programs that perform various numeric and string operations: 1) Summing digits of a number, reversing digits, finding largest digit, summing odd/even positioned digits 2) Checking for prime numbers between 50-100 3) Counting occurrences of a digit in a number 4) Counting vowels in a word 5) Counting words in a string 6) Calculating number of days between dates 7) Computing area of a circle 8) Checking for a leap year 9) Computing row and column sums of a matrix 10) Checking if a number is even or odd 11) Computing square root of a number 12) Swapping two variables

Uploaded by

AmitKumar
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/ 17

//Sum of the digits of a number

#include<stdio.h>
#include<stdlib.h>

int main()
{
int num, copy_of_num, sum=0;
system("cls");
printf("Enter any number: ");
scanf("%d", &num);
copy_of_num = num;
while(num != 0)
{
sum += num%10;
num /= 10;
}
printf("The sum of the digits of the number %d is %d\n", copy_of_num, sum);
system("pause");
return 0;
}

//Reversing the digits of a number
#include<stdio.h>
#include<stdlib.h>

int main()
{
int num, copy_of_num, reverse=0;
system("cls");
printf("Enter any number: ");
scanf("%d", &num);
copy_of_num = num;
while(num != 0)
{
reverse = reverse*10 + num%10;
num /= 10;
}
printf("\n\nThe reverse of %d is %d\n", copy_of_num, reverse);
system("pause");
return 0;
}

//Largest digit of a Number
#include<stdio.h>
#include<stdlib.h>

int main()
{
int num, copy_of_num, big, digit;
system("cls");
printf("Enter any number: ");
scanf("%d", &num);
copy_of_num = num;
digit = num%10;
big = digit;
while(num != 0)
{
digit = num%10;
if(big < digit)
big = digit;
num /= 10;
}
printf("\n\nThe largest digit in %d is %d\n", copy_of_num, big);
system("pause");
return 0;
}

//Sum of Odd and Even positioned digits in a Number
#include<stdio.h>
#include<stdlib.h>

int main()
{
int num, copy_of_num, digit, Odd_Sum=0, Even_Sum=0, count=0;
system("cls");
printf("Enter any number: ");
scanf("%d", &num);
copy_of_num = num;
while(num != 0)
{
count++;
digit = num % 10;
if(count%2 != 0)
Odd_Sum += digit;
else
Even_Sum += digit;
num /= 10;
}
if(count%2 == 0)
{
Odd_Sum = Odd_Sum + Even_Sum;
Even_Sum = Odd_Sum - Even_Sum;
Odd_Sum = Odd_Sum - Even_Sum;
}
printf("\n\nThe sum of odd positioned digits in %d is %d", copy_of_num, Odd_
Sum);
printf("\nThe sum of even positioned digits in %d is %d\n\n", copy_of_num, E
ven_Sum);
system("pause");
return 0;
}

//Prime Numbers Between 50 and 100
#include<stdio.h>
#include<stdlib.h>

int main()
{
int i, j, flag;
system("cls");
printf("Prime Numbers between 50 and 100: \n");
for(i=50; i<=100; ++i)
{
flag = 0;
for(j=2; j<=(i/2); ++j)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
printf("%d\t", i);
}
system("pause");
return 0;
}

//Number of times a digit is present in a number
#include<stdio.h>
#include<stdlib.h>

int main()
{
long int num, copy_of_num;
int count=0, digit;
system("cls");
printf("Enter any number: ");
scanf("%ld", &num);
copy_of_num = num;
printf("Enter the digit whose occurance is to be searched for: ");
scanf("%d", &digit);
while(num != 0)
{
if(num%10 == digit)
count++;
num /= 10;
}
printf("The number of times %d occurs in %ld is %d\n", digit, copy_of_num, c
ount);
system("pause");
return 0;
}

//Number of Vowels in a Word
#include<stdio.h>
#include<stdlib.h>

int main()
{
char str[200];
int i, count=0;
system("cls");
printf("Enter a string: ");
gets(str);
for(i=0; str[i] != '\0'; ++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')
count++;
printf("Number of vowels in the string: %d\n", count);
system("pause");
return 0;
}

//Number of Words in a String
#include<stdio.h>
#include<stdlib.h>

int main()
{
char str[200];
int i, count=1;
system("cls");
printf("Enter a string: ");
gets(str);
for(i=0; str[i] != '\0'; ++i)
{
if(str[i] == ' ')
{
count++;
while(str[i] == ' ')
i++;
}
}
printf("Number of words in the string: %d\n\n", count);
system("pause");
return 0;
}







//Number of Days Between Two Dates
#include <stdio.h>
#include <stdlib.h>

char *month[13] = {"None", "Jan", "Feb", "Mar",
"Apr", "May", "June", "July",
"Aug", "Sept", "Oct",
"Nov", "Dec"};

/*daysPerMonth[0] = non leap year
daysPerMonth[1] = leap year */
int daysPerMonth[2][13] = { {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{-
1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };

typedef struct _d
{
int day; // 1 to 31
int month; // 1 to 12
int year; // any
}dt;

void print_dt(dt d)
{
printf("%d %s %d \n", d.day, month[d.month], d.year);
return;
}

int leap(int year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0) ? 1 : 0;
}

int minus(dt d1, dt d2)
{
//int d1_l = leap(d1.year), d2_l = leap(d2.year);
int y, m;
int total_days = 0;
for (y = d1.year; y >= d2.year ; y--)
{
if (y == d1.year)
{
for (m = d1.month ; m >= 1 ; m--)
{
if (m == d1.month)
total_days += d1.day;
else
total_days += daysPerMonth[leap(y)][m];

// printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], to
tal_days);
}
}
else if (y == d2.year)
{
for (m = 12 ; m >= d2.month ; m--)
{
if (m == d2.month)

total_days += daysPerMonth[leap(y)][m] - d2.day;
else
total_days += daysPerMonth[leap(y)][m];

// printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], to
tal_days);
}
}
else
{
for (m = 12 ; m >= 1 ; m--)
{
total_days += daysPerMonth[leap(y)][m];

// printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], to
tal_days);
}
}
}
return total_days;
}

int main()
{
// 28 Oct 2018
dt d2 = {28, 10, 2018};
// 30 June 2006
dt d1 = {30, 6, 2006};
int days;
int d1_pt = 0, d2_pt = 0;
system("cls");
if (d1.year > d2.year)
d1_pt += 100;
else
d2_pt += 100;
if (d1.month > d2.month)
d1_pt += 10;
else
d2_pt += 10;
if (d1.day > d2.day)
d1_pt += 1;
else
d2_pt += 1;
days = (d1_pt > d2_pt) ? minus(d1, d2) : minus(d2, d1);
print_dt(d1);
print_dt(d2);
printf("number of days: %d \n", days);
system("pause");
return 0;
}

//Area of a Circle
#include<stdio.h>
#include<stdlib.h>
#define PI 3.141592

int main()
{
float r, area;
system("cls");
printf("Enter the radius: ");
scanf("%f", &r);
area = PI * r * r;
printf("Area of the circle is %f\n", area);
system("pause");
return 0;
}

//Leap Year Check
#include<stdio.h>
#include<stdlib.h>

int main()
{
unsigned int year;
system("cls");
printf("Enter the year: ");
scanf("%u", &year);
if(year%400 == 0)
printf("%u is a leap year!!\n\n", year);
else if(year%100 !=0 && year%4 == 0)
printf("%u is a leap year!!\n\n", year);
else
printf("%u is not a leap year.\n\n", year);
system("pause");
return 0;
}

//Row Sum and Column Sum of a Matrix
#include<stdio.h>
#include<stdlib.h>

int main()
{
int A[10][10], row_sum[10], column_sum[10], i, j, n;
system("cls");
printf("Enter the order of the square matrix: ");
scanf("%d", &n);
printf("Enter the matrix: \n");
for(i=0; i<n; ++i)
for(j=0; j<n; ++j)
scanf("%d", &A[i][j]);
for(i=0; i<n; ++i)
{
row_sum[i] = column_sum[i] = 0;
for(j=0; j<n; ++j)
{
row_sum[i] += A[i][j];
column_sum[i] += A[j][i];
}
}
printf("\n\nThe matrix with the row sum and the column sum is: \n");
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
printf("%d\t", A[i][j]);
printf("%d\n", row_sum[i]);
}
for(i=0; i<n; ++i)
printf("%d\t", column_sum[i]);
printf("\n\n");
system("pause");
return 0;
}

//Even or Odd Number Check
#include<stdio.h>
#include<stdlib.h>

int main()
{
int num;
system("cls");
printf("Enter the number: ");
scanf("%d", &num);
if(num%2 == 0)
printf("%d is an even number\n\n", num);
else
printf("%d is an odd number\n\n", num);
system("pause");
return 0;
}

//Square Root of a Number
#include<stdio.h>
#include<stdlib.h>

int main()
{
register float i, j;
float num;
j=0.00001; //Increase the number of Zero's to beef up the accuracy
system("cls");
printf("Enter any number: ");
scanf("%f",&num);
for(i=0;i<=num;i=i+j)
{
if((i*i)>num)
{
i=i-j;
break;
}
}
printf("\nsqrt(%.4f) = %.4f\n\n",num, i);
system("pause");
return 0;
}

//Swapping two variables
#include<stdio.h>
#include<stdlib.h>

void Swap(int*, int*);

int main()
{
int a, b;
system("cls");
printf("Enter the first number (say a): ");
scanf("%d", &a);
printf("Enter the second number (say b): ");
scanf("%d", &b);
printf("\n\nBefore swapping values of a and b are %d and %d respectively", a
, b);
Swap(&a, &b);
printf("\n\nAfter swapping values of a and b are %d and %d respectively\n\n"
, a, b);
system("pause");
return 0;
}

void Swap(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}

//Transpose of a Matrix
#include<stdio.h>
#include<stdlib.h>

int main()
{
int A[10][10], T[10][10], i, j, r, c;
system("cls");
printf("Enter the order of the matrix: ");
scanf("%d%d", &r, &c);
printf("Enter the matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
scanf("%d", &A[i][j]);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
T[j][i] = A[i][j];
printf("The transpose of the matrix entered is:\n");
for(i=0; i<c; ++i)
{
for(j=0; j<r; ++j)
printf("%d\t", T[i][j]);
printf("\n");
}
printf("\n");
system("pause");
return 0;
}

//Pattern Printing (Question Number 17)
#include<stdio.h>
#include<stdlib.h>

int main()
{
int i, j, k, n;
system("cls");
printf("Enter the number of rows in the pattern: ");
scanf("%d", &n);
for(i=1; i<=(2*n); i+=2)
{
for(j=1; j<=((2*n)-i); ++j)
printf(" ");
for(k=1; k<=i; ++k)
printf(" %d", k);
printf("\n");
}
system("pause");
return 0;
}

//Pattern Printing (Question Number 18)
#include<stdio.h>
#include<stdlib.h>

int main()
{
int i, j, k, n;
system("cls");
printf("Enter the number of rows in the pattern: ");
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
for(j=1; j<=(n-i); ++j)
printf(" ");
for(k=1; k<=i; ++k)
printf(" 5");
printf("\n");
}
system("pause");
return 0;
}

//Sum of Series: -x + (x^3/3!) - (x^5/5!) + ...
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

float fact(int);

int main()
{
int i, n;
float x, sum=0.0f, term, count=1;
system("cls");
printf("This program computes the sum of the following series:\n");
printf("-x + (x^3/3!) - (x^5/5!) + ...\n\n");
printf("Enter values for x and n: ");
scanf("%f%d", &x, &n);
for(i=1; i<=(2*n); i+=2)
{
term = pow(x, i)/fact(i);
sum += pow(-1, count)*term;
count++;
}
printf("The sum of the series upto %d terms is %f\n\n", n, sum);
system("pause");
return 0;
}

float fact(int n)
{
if(n == 1)
return 1;
else
return n*fact(n-1);
}

//Sum of Series: 1 - (x^2/2!) + (x^4/4!) - (x^6/6!) + ...
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

float fact(int);

int main()
{
int i, n;
float x, sum=0.0f, term, count=0;
system("cls");
printf("This program computes the sum of the following series:\n");
printf("1 - (x^2/2!) + (x^4/4!) - (x^6/6!) + ...\n\n");
printf("Enter values for x and n: ");
scanf("%f%d", &x, &n);
for(i=0; i<(2*n); i+=2)
{
term = pow(x, i)/fact(i);
sum += pow(-1, count)*term;
count++;
}
printf("The sum of the series upto %d terms is %f\n\n", n, sum);
system("pause");
return 0;
}

float fact(int n)
{
if(n==0 || n==1)
return 1;
else
return n*fact(n-1);
}
















//Inverse of a 3x3 Matrix
#include<stdio.h>
#include<stdlib.h>

#define order 3

int main()
{
float matrix[order][order], aug_matrix[order][order*2];
float identity[order][order] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
float c, sub, det;
int i, j, k, row, col;
system("cls");
printf("Enter the elements of 3 x 3 matrix:\n");
for(i=0; i<order; ++i)
for(j=0; j<order; ++j)
scanf("%f", &matrix[i][j]);
det = matrix[0][0] * (matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2])
-
matrix[0][1] * (matrix[1][0]*matrix[2][2] - matrix[2][0]*matrix[1][2])
+
matrix[0][2] * (matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1])
;
if(det != 0)
{
for(i=0; i<order; ++i)
{
for(j=0; j<order; ++j)
{
aug_matrix[i][j] = matrix[i][j];
aug_matrix[i][j+3] = identity[i][j];
}
}
for(i=0; i<order; ++i)
{
for(j=0; j<order; ++j)
{
if(i==j)
{
c = aug_matrix[i][j];
for(k=0; k<order*2; ++k)

aug_matrix[i][k] = aug_matrix[i][k]/c;
for(row=0; row<order; ++row)
{
sub = aug_matrix[row][j];
for(col=0; col<order*2; ++col)
{
if(row != i)

aug_matrix[row][col] -= sub*aug_matrix[i][col];
}
}
}
}
}
printf("Inverse of the matrix is:\n");
for(i=0; i<order; ++i)
{
for(j=0; j<order; ++j)
printf("%5.2f\t", aug_matrix[i][j+3]);
printf("\n");
}
}
else
printf("\nInverse does not exist!!");
system("pause");
return 0;
}

//Program to Search for a given element from a set of values
#include<stdio.h>
#include<stdlib.h>

int Search(int[], int, int);

int main()
{
int AR[100], n, item, i, pos;
system("cls");
printf("This program searches for a given element in an array and returns\nt
he position of its first occurance.\n");
printf("Enter the number of elements(max 100): ");
scanf("%d", &n);
printf("Enter the elements:\n");
for(i=0; i<n; ++i)
scanf("%d", &AR[i]);
printf("\nEnter the element to be serched for: ");
scanf("%d", &item);
pos = Search(AR, n, item);
if(pos == -1)
printf("Sorry, %d could not be found.\n\n", item);
else
printf("%d was found at position: %d.\n\n", item, pos);
system("pause");
return 0;
}

int Search(int AR[], int n, int item)
{
int i;
for(i=0; i<n; ++i)
{
if(AR[i] == item)
return i+1;
}
return -1;
}

//Program to accept a string in any case and convert it to another
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main()
{
char str[250];
int i;
system("cls");
printf("Enter a string: ");
gets(str);
for(i=0; str[i] != '\0'; ++i)
{
if(isupper(str[i]))
str[i] = tolower(str[i]);
else if(islower(str[i]))
str[i] = toupper(str[i]);
}
printf("The string after changing its case is ");
puts(str);
system("pause");
return 0;
}

//Pascal's Triangle
#include<stdio.h>
#include<stdlib.h>

int fact(int);

int main()
{
int i, j, k, n;
system("cls");
printf("Enter the number of rows in the pattern: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
for(j=0; j<=(n-i-2); ++j)
printf(" ");
for(k=0; k<=i; ++k)
printf(" %d", (fact(i)/(fact(k)*fact(i-k))));
printf("\n");
}
system("pause");
return 0;
}

int fact(int n)
{
int i, fact = 1;
for(i=1; i<=n; ++i)
fact *= i;
return fact;
}

//Prime Number or not
#include<stdio.h>
#include<stdlib.h>

int main()
{
int num, i, flag=0;
system("cls");
printf("Enter the number: ");
scanf("%d", &num);
if(num==0 || num==1)
printf("%d is neither prime nor composite.", num);
else
{
if(num != 2)
{
for(i=2; i<=(num/2); ++i)
{
if(num%i == 0)
{
flag = 1;
break;
}
}
}
if(flag == 0)
printf("%d is a prime number!!!", num);
else
printf("%d is not a prime number.", num);
}
printf("\n\n");
system("pause");
return 0;
}

//Roots of a quadratic equation
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
float a, b, c, r1, r2, d;
system("cls");
printf("Enter the values for A, B and C in the quadratic equation: ");
scanf("%f%f%f", &a, &b, &c);
d = b*b - 4*a*c;
if(d>=0)
{
r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);
printf("The roots are %f and %f", r1, r2);
}
else
{
d = -d;
printf("The roots are imaginary and thay are:\n");
printf("%f + %fi and ",(-b/(2*a)), (sqrt(d)/(2*a)));
printf("%f - %fi",(-b/(2*a)), (sqrt(d)/(2*a)));
}
printf("\n\n");
system("pause");
return 0;
}

//All Possible Combinations of a four digit number (1234 in this case)
#include<stdio.h>
#include<stdlib.h>

int main()
{
int a, b, c, d;
system("cls");
for(a=1; a<5; ++a)
for(b=1; b<5; ++b)
for(c=1; c<5; ++c)
for(d=1; d<5; ++d)

if(!(a==b || a==c || a==d ||b==c ||b==d||c==d))
printf("%d%d%d%d\t",a, b, c, d);
printf("\n\n");
system("pause");
return 0;
}

You might also like