C Programming 70 Programs
C Programming 70 Programs
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sal,tax;/*sal=salary*/
float total;
printf("Enter salary per year Rs:",sal);
scanf("%d",&sal);
if (sal<=100000)
printf("Tax is 0 percent,so your Salary after paying tax is Rs
%d \n\n ",sal);
else if (sal>100000 && sal<=150000)
{
total=sal-(0.15*sal);
printf("Tax is 15 percent,so your Salary after paying tax is
Rs %f \n",total);
tax=sal-total;
printf("Tax amount is Rs %d\n",tax);
}
else if(sal>150000)
{
total=sal-(0.25*sal);
printf("Tax is 25 percent,so your Salary after paying tax is Rs
%f \n ",total);
tax=sal-total;
printf("Tax amount is Rs %d\n",tax);
}
system("pause");
return(0);
}
scanf("%Lf",&R12);
printf("Enter the value of R23(in ohm)=");
scanf("%Lf",&R23);
printf("Enter the value of R13(in ohm)=");
scanf("%Lf",&R13);
R1=(R12*R13)/(R12+R23+R13);
printf("Resistance in R1(in ohm) is %Lf\n",R1);
R2=(R12*R23)/(R12+R23+R13);
printf("Resistance in R2(in ohm) is %Lf\n",R2);
R3=(R13*R23)/(R12+R23+R13);
printf("Resistance in R3(in ohm) is %Lf\n",R3);
scanf("%lf",&R3);
return(1);
}
9)/* This program computes the size of variable using size of operator.*/
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of char: %d byte\n",sizeof(d));
system("pause");
return (0);
}
else if(determinant>0.0)
{
printf("Roots are real and unequal");
r1=(-b+sqrt(determinant))/(2*a);
r2=(-b-sqrt(determinant))/(2*a);
printf("\nfirst root:%f",r1);
printf("\nsecond root:%f",r2);
}
else if(determinant==0)
{
r1 =r2 =-b/(2*a);
printf("Roots are real and equal\n");
printf("Roots are :%f and %f\n",r1,r2);
}
else
{
real=-b/(2*a);
imag =sqrt(-determinant)/(2*a);
printf("Roots are: %f+%fi and %f-%fi \n",real,imag,real,imag);
}
system("pause");
return (0);
}
if (n<0)
printf("Error!!! Factorial of negative number doesn't
exist\n");
else
{
for(count=1;count<=n;++count) /*for loop terminates if
count>n*/
{
factorial*=count; /*factorial=factorial*count*/
}
printf("Factorial = %lu",factorial);
}
system("pause");
return 0;
}
int n,reverse=0,rem,temp;
printf("Enter an integer: ");
scanf("%d",&n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10; } /* Checking if number entered by user and it's reverse
number is equal. */
if(reverse==n)
printf("%d is a palindrome\n",n);
else
printf("%d is not a palindrome\n",n);
system("pause");
return 0;
}
printf("%f / %f = %f\n",num1,num2,num1/num2);
break;
default: /*If operator is other than +, -, * or /, error message
is shown */
printf("Error! operator is not correct");
break;
}
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter year:");
scanf("%d",&n);
if((n%4==0 && n%100!=0)||n%400==0)
printf("\nThis is leap year");
else
printf("\nThis is not leap year ");
scanf("%d",&n);
return(1);
}
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row
of second So there will be no multiplication\n\n");
exit(1);
}
printf("\n\n");
}
system("pause");
return 0;
}
}
system("pause");
return 0;
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",trans[i][j]);
if(j==r-1)
printf("\n\n");
}
system("pause");
return 0;
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d",sum[i][j]);
if(j==c-1)
printf("\n\n");
}
system("pause");
return 0;
}
printf("%c is an alphabet\n",c);
else
printf("%c is not an alphabet\n",c);
system("pause");
return 0;
}
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
printf("Each digits of given number are: \n");
while(factor>1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}
system("pause");
return 0;
}
29)Write a c program to find out the sum of series 1^3 + 2^3 + …. + n^3
Write a c program or code to find out the sum of series 1^3 + 2^3 + ….
+ n^3 that is sum of cube of n natural numbers.
#include<stdio.h>
#include<math.h>
int main()
{
int n,i;
int sum=0;
for(i =1;i<=n;i++){
if (i != n)
printf("%d^3 + ",i);
else
printf("%d^3 = %d ",i,sum);
}
system("pause");
return 0;
}
#include<stdio.h>
int main(){
int n,i;
int sum=0;
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1)) / 2;
printf("Sum of the series: \n");
for(i =1;i <= n;i++)
{
if (i!=n)
printf("%d + ",i);
else
printf("%d = %d \n",i,sum);
}
system("pause");
return 0;
}
{
positive_sum =positive_sum + array[i];
}
else if(array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers=
%d\n",negative_sum);
printf("Sum of all positive numbers= %d\n",positive_sum);
printf("\n Average of all input numbers= %.2f\n", average);
system("pause");
return(0);
}
if(i%j==0)
{
c++;
}
}
if (c==2)
{
printf("\n %d \n",i);
}
c=0;
}
system("pause");
return 0;
}
++count;
printf(" %d ",display);
}
printf("\n\n");
system("pause");
return 0;
}
system("pause");
return(0);
}
//bring it in range of 0 to 6
fmonth = fmonth % 7;
return fmonth;
}
int day_of_week(int date, int month, int year) {
int dayOfWeek;
int YY = year % 100;
int century = year / 100;
(century % 4);
//remainder on division by 7
dayOfWeek =dayOfWeek%7;
switch (dayOfWeek) {
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
default:
printf("Incorrect data");
}
return 0;
}
int main() {
int date, month, year;
return 0;
}
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(c
h=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
{
printf("The given Character '%c' is a Vowel\n\n ",ch);
}
else
{
printf("The given Character '%c' is a Consonant\n\n ",ch);
}
system("pause");
return 0;
}
int main()
{
int first, second, *p, *q, sum;
p = &first;
q = &second;
sum = *p + *q;
48)Linear search in C.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array=");
scanf("%d",&n);
printf("Enter %d integer(s)=\n",n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
system("pause");
return 0;
}
}
50)C program to find frequency of characters in a string
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0};
printf("Enter a string\n");
gets(string);
while (string[c] != '\0')
{
if (string[c] >= 'a' && string[c] <= 'z')
count[string[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (count[c] != 0)
printf("%c occurs %d times in the entered
string.\n",c+'a',count[c]);
}
system("pause");
return 0;
}
void main() {
char s[100][50],t[1000],n;
int i,j;
printf("Enter total number of names = ");
scanf("%d",&n);
printf("\nEnter strings:\n");
for (i =0;i<n;i++)
scanf("%s", s[i]);
for (i =1;i <n; i++) {
for (j =1; j<n; j++) {
if (strcmp(s[j - 1], s[j]) > 0) {
strcpy(t, s[j - 1]);
strcpy(s[j - 1], s[j]);
strcpy(s[j], t);
}
}
}
printf("\nStrings in order are : ");
for (i =0; i<n; i++)
printf("\n%s",s[i]);
getch();
}
for(i=0;i<len;i++){
count=1;
for(j=i+1;j<=len-1;j++){
if(arr[i]==arr[j] && arr[i]!='\0'){
count++;
arr[j]='\0';
}
}
if(arr[i]!='\0'){
printf("%d is repeated %d times.\n",arr[i],count);
}
}
getch();
return 0;
}
55)To find the average and numbers greater than average and less
than average between two input numbers in an array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
long int ar[90000],n,i;
float avg,sum=0;
printf("Enter the size of the array=");
scanf("%ld",&n);
printf("\n");
printf("Enter the numbers in the array to find their
average=\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
sum=sum+ar[i];
}
avg=sum/n;
printf("\n");
printf("Average is %f\n",avg);
printf("\n");
printf("The numbers greater than the average are: \n");
for(i=1;i<n;i++)
{
if(ar[i]>avg)
{
printf("%d\n",ar[i]);
}
}
printf("\n");
printf("\n");
printf("The numbers smaller than the average are: \n");
for(i=1;i<n;i++)
{
if(ar[i]<avg)
{
printf("%d\n",ar[i]);
}
}
printf("\n");
system("pause");
return(1);
}
56)To calculate sum of money according to number and value of notes.
#include <stdio.h>
int main()
{
double X1,X2,X3,X4,X5,X6,X7,X8,X9,sum;
printf("This calculates total amount you entered according to
number and value of notes");
printf("\n");
printf("Enter number of Rs 1000,X1= ");
scanf("%Lf",&X1);
X1=X1*1000;
printf("\nEnter number of Rs 500,X2= ");
scanf("%Lf",&X2);
X2=X2*500;
printf("\nEnter number of Rs 100,X3= ");
scanf("%Lf",&X3);
X3=X3*100;
printf("\nEnter number of Rs 50,X4= ");
scanf("%Lf",&X4);
X4=X4*50;
printf("\nEnter number of Rs 25,X5= ");
scanf("%Lf",&X5);
X5=X5*25;
printf("\nEnter number of Rs 10,X6= ");
scanf("%Lf",&X6);
X6=X6*10;
printf("\nEnter number of Rs 5,X7= ");
scanf("%Lf",&X7);
X7=X7*5;
printf("\nEnter number of Rs 2,X8= ");
scanf("%Lf",&X8);
X7=X7*2;
printf("\nEnter number of Rs 1,X9= ");
scanf("%Lf",&X9);
X7=X7*9;
sum=X1+X2+X3+X4+X5+X6+X7,X8,X9;
printf("\nTotal amount in rupees that Jaikishan owns is Rs
%Lf\n",sum);
scanf("%Lf",&sum);
return(1);
}
return 0;
}
61)Number Pattern
#include <stdio.h>
int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n\n");
}
system("pause");
return 0;
}
62)Number Pattern
#include <stdio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n\n");
}
system("pause");
return 0;
}
63)Number Pattern
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=j-2;j>=1;j--)
printf("%d ",j);
printf("\n");
}
System(“pause”);
return 0;
}
64)Number pattern:
int main()
{
int i,j,k;
for(i=5;i>=1;i--)
{
if(i%2==1) k=1;
else k=i;
for(j=1;j<=i;j++)
{
printf("%d",k);
if(i%2==1) k++;
else k--;
}
printf("\n\n");
}
system("pause");
return 0;
}
65)
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>=1;j--)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
printf("\n\n");
}
system("pause");
return 0;
}
66)Alphabet Patterns:
#include <stdio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
printf("%c", 'A'-1 + j);
}
printf("\n\n");
}
system("pause");
return 0;
}
67)#include <stdio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A'-1 + i);
}
printf("\n\n");
}
system("pause");
return 0;
}
68)
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A' + j-1);
}
printf("\n\n");
}
system("pause");
return 0;
}
printf("Marks: %f\n",s.marks);
system("pause");
return 0;
}
70)Using structure:
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct details
{
char name[30];
int age;
float salary;
};
int main()
{
struct details detail;
printf("\nEnter name: ");
gets(detail.name);
printf("\nEnter age:");
scanf("%d",&detail.age);
printf("\nEnter Salary:");
scanf("%f",&detail.salary);
printf("\n\n\n");
printf("Name of the Employee : %s \n",detail.name);
printf("Age of the Employee : %d \n",detail.age);
printf("Salary of the Employee : %f \n",detail.salary);
getch();