0% found this document useful (0 votes)
11 views41 pages

CProgram

Some c programming

Uploaded by

Ravi Singh
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)
11 views41 pages

CProgram

Some c programming

Uploaded by

Ravi Singh
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/ 41

Some elementary C-Programs

____________________________________________________________________
_____________________
1. SI & CI
____________________________________________________________________
_____________________

#include<stdio.h>
int main()
{

float P,n,r,SI,CI;
printf("Enter Principal(Rs), Time(Yr), and Rate(R): ");
scanf("%f %f %f",&P,&n,&r);
SI= (P*n*r)/100;
CI=P*(pow(1+r/100,n)-1);
printf("SI= %.2f\n",SI);
printf("CI= %.2f\n",CI);
getchar();
return 0;

____________________________________________________________________
____________________
2. Leap Year
____________________________________________________________________
_____________________

#include<stdio.h>
int main()
{

int year;
printf("Enter year : ") ;
scanf("%d",&year);
if(year%4==0&&(year%100==0||year%400==0))
printf("%d is leap",year);
else
printf("%d is not a leap year",year);
return 0;
}

____________________________________________________________________
____________________
3. Result formated
____________________________________________________________________
_____________________
// Result 1

#include<stdio.h>

void main()
{
printf("\t\t*****************\n");
printf("\t\tResult Calculator\n");
printf("\t\t*****************\n\n");
int phy , chem , math , nep , eng;
float per;
printf("Please Enter the Marks\n");
printf("======================\n");
printf("\n");
printf("1. Physics : ");
scanf("%d",&phy);
printf("2. Chemistry : ");
scanf("%d",&chem);
printf("3. Mathematics : ");
scanf("%d",&math);
printf("4. Nepali : ");
scanf("%d",&nep);
printf("5. English : ");
scanf("%d",&eng);
printf("_______________________\n");
printf("Total %d",phy + chem + math + nep + eng);
per = (float)(phy + chem + math + nep + eng )/5;

if(phy > 100 || chem > 100 || math > 100 || nep > 100 || eng >
100)
printf("\n\aWarning: Incorrect Input");
else
{
printf("\n\nPercentage %.1f\n",per);
printf("\nDivision ");
if(phy >=35 && chem >=35 && math >=35 && nep >= 35 && eng >=
35)
{
if(per >= 80)
printf(" Distinction");
else
if(per >= 60)
printf(" First");
else
if(per >= 50)
printf(" Second");
else
if(per >= 35)
printf(" Third");
}
else
{
printf(" Fail\n\n");
printf("Failed in\n");
printf("------------");
if(phy < 35)
printf("\nPhysics");
if(chem < 35)
printf("\nChemistry");
if(math < 35)
printf("\nMathematics");
if(nep < 35)
printf("\nNepali");
if(eng < 35)
printf("\nEnglish");
}
}
printf("\n\n");
}

____________________________________________________________________
____________________
4. Result
____________________________________________________________________
_____________________

#include<stdio.h>
int main()
{
float percentage;
int E,M,P,C,total;
printf("Enter Marks: \n");
scanf("%d %d %d %d",&E,&M,&P,&C);

total = E + M + P + C;
printf("Total = %d\n",total);
percentage = (float)total/4;

if(E>=40 && M>=40 && P>=40 && C>=40 )


{
if(percentage>=80)
printf("Pass. Distinction %.2f% %\n",percentage);
else if(percentage>=60)
printf("Pass. First Division %.2f% %\n",percentage);
else if(percentage>=50)
printf("Pass. Second Division %.2f% %\n",percentage);
else
printf("Third Division %.2f% %\n",percentage);
}
else
printf("Fail in\n");
if(E<40)
printf("English\t");
if(M<40)
printf("Math\t");
if(P<40)
printf("Physics\t");
if(C<40)
printf("Chemistry\t");
printf("\n");
return 0;

____________________________________________________________________
____________________
5. Greatest of three using If
____________________________________________________________________
_____________________

// Greatest of three If

#include<stdio.h>

void main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d",&a, &b, &c);
if(a > b && a > c)
printf("The largest number is %d",a);
if(b > a && b > c)
printf("The largest number is %d",b);
if(c > a && c > b)
printf("The largest number is %d",c);
}

____________________________________________________________________
____________________
6. Greatest of three using
else...if
____________________________________________________________________
_____________________

// Greatestof3

#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three positive integers:");
scanf("%d %d %d",&a,&b,&c);
if (a>b&&a>c)
printf("%d is largest",a);
else if(b>c)
printf("%d is largest",b);
else
printf("%d is largest",c);
getchar();
return 0;

____________________________________________________________________
____________________
7. Greatest of three using
conditional
____________________________________________________________________
_____________________

// Greatestof3 using conditional

#include<stdio.h>
int main()
{
int x,a,b,c;
printf("Enter three positive integers:");
scanf("%d %d %d",&a,&b,&c);
x=a>b&&a>c?a:b>c?b:c;
printf("%d is largest",x);
getchar();
return 0;
}

____________________________________________________________________
____________________
8. Greatest of three using
nested if
____________________________________________________________________
_____________________

//greatestof3

#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three positive integers:");
scanf("%d %d %d",&a,&b,&c);
if (a>b)
{
if(a>c)
{

printf("%d is largest",a);
}
else
{
printf("%d is largest",c);
}
}
else if (b<c)

printf("%d is largest",c);
else
printf("%d is largest",b);

getchar();
return 0;

____________________________________________________________________
____________________
9. I/O
____________________________________________________________________
_____________________

// I/O

#include <stdio.h>
int main()
{

puts("Greetings, human.");
return 0;
}

____________________________________________________________________
____________________
10. I/O
____________________________________________________________________
_____________________

// I/O 1

#include <stdio.h>

main()
{
char answer;

printf("Would you like to know my name?\n");


printf("Type Y for YES and any other letter for NO: ");

answer = getchar(); /* .... Reading a character...*/

if(answer == 'Y' || answer == 'y')


printf("\n\nMy name is BUSY BEE\n\n");
else
printf("\n\nYou are good for nothing\n\n");
}

____________________________________________________________________
____________________
11. I/O
____________________________________________________________________
_____________________

// I/O 2

#include <stdio.h>
#include <ctype.h>

// Program: Alphabet or Number ? //


main()
{
char character;
printf("Press any key\n");
character = getchar();
if (isalpha(character) )
printf("The character is a letter.\n\n");
else
if (isdigit (character) )
printf("The character is a digit.\n\n");
else
printf("The character is not alphanumeric.\n\n");
}

____________________________________________________________________
____________________
12. I/O
____________________________________________________________________
_____________________

// I/O 3

#include <stdio.h>
#include <ctype.h>

//Program: Casechange for a character

main()
{
char alphabet;
printf("Enter an alphabet\n");

alphabet = getchar();
if (islower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));

putchar('\n');
}

____________________________________________________________________
____________________
13. I/O
____________________________________________________________________
_____________________

// I/O 5

#include<stdio.h>

void main()
{
char x;
x = getchar();
putchar(x);
putchar('\n');
putchar('P');
putchar('\n');
putchar('T');
putchar('h');
putchar('a');
putchar('n');
putchar('k');
}

____________________________________________________________________
____________________
14. Size of variables
____________________________________________________________________
_____________________

// Size of

#include<stdio.h>
#include<float.h>
#include<limits.h>

/* Program for Storage size and Range of Data Types */


int main()
{
printf("size of character %d byte(s)\n",sizeof(char));
printf("Range of signed Character %d to %d\n",SCHAR_MIN,SCHAR_MAX);
printf("Range of unsigned Character 0 to %d\n\n",UCHAR_MAX);

printf("size of short integer %d byte(s)\n",sizeof(short int));


printf("Range of signed Short integer %d to %d
\n",SHRT_MIN,SHRT_MAX);
printf("Range of unsigned short integer 0 to %d\n\n",USHRT_MAX);

printf("size of integer %d byte(s)\n",sizeof(int));


printf("size of unsigned integer %d byte(s)\n",sizeof(unsigned
int));
printf("Range of signed integer %d to %d\n",INT_MIN,INT_MAX);
printf("Range of unsigned integer 0 to %lu\n\n",UINT_MAX);

printf("size of long integer %d byte(s)\n",sizeof(long int));


printf("Range of signed long integer %ld to %ld
\n",LONG_MIN,LONG_MAX);
printf("Range of unsigned long integer 0 to %lu\n\n",ULONG_MAX);

printf("size of long long integer %d byte(s)\n",sizeof(long long


int));
printf("Range of signed long long integer %lld to %lld
\n",LONG_LONG_MIN,LONG_LONG_MAX);
printf("Range of unsigned long long integer 0 to %llu\n
\n",ULONG_LONG_MAX);

printf("size of float %d byte(s)\n",sizeof(float));


printf("%e to %e\n",FLT_MIN,FLT_MAX);
printf("Precision of float %d\n\n",FLT_DIG);

printf("size of double %d byte(s)\n",sizeof(double));


printf("Range of double %e to %e\n",DBL_MIN,DBL_MAX);
printf("Precision of double %d\n\n",DBL_DIG);

printf("size of long double %d byte(s)\n",sizeof(long double));


printf("Range of long double %e to %e\n",LDBL_MIN,LDBL_MAX);
printf("Precision of long double %d\n\n",LDBL_DIG);

return 0;
}

____________________________________________________________________
____________________
15. Natural Number Series
____________________________________________________________________
_____________________

// Natural Series
#include<stdio.h>

void main()
{
int i = 1, n;
int index;
long sum;
printf("Enter n: ");
scanf("%d",&n);
printf("Enter the index: ");
scanf("%d",&index);
while(i <= n)
{
sum = sum + pow(i,index);
i = i + 1;
}
printf("\n1^%d + 2^%d + ... + %d^%d = %ld
\n",index,index,n,index,sum);
}

____________________________________________________________________
____________________
16. Quadratic equation
____________________________________________________________________
_____________________

// Quadratic equation

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,D,r1,r2;
printf("Enter a,b,c in ax^2+bx+c \n");
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("Roots are %f , %f\n",r1,r2);
}
else
{
r1=(-b+sqrt(-D))/(2*a);
r2=(-b-sqrt(-D))/(2*a);
printf("Roots are %f +i %f and ",r1,r2);
printf(" %f -i %f\n",r1,r2);
}
return 0;
}
____________________________________________________________________
____________________
17. Reverse
____________________________________________________________________
_____________________

// Reverse

#include<stdio.h>
main()
{
int n,i,r=0;
printf("Enter Number\n");
scanf("%d",&n);

while(n!=0)
{
r = r * 10;
r = r + n % 10;
n = n / 10;

printf("Reverse = %d\n",r);

return 0;
}

____________________________________________________________________
____________________
18. Palindrome
____________________________________________________________________
_____________________

// Palindrome

#include<stdio.h>
int main()
{
int n,i,p,r=0;
printf("Enter Number\n");
scanf("%d",&n);
p=n;
while(n!=0)
{
r = r * 10;
r = r + n % 10;
n = n / 10;

}
printf("Reverse = %d\n",r);

if(r==p)
printf("%d is Palindrome\n",p);
else
printf("%d is not Palindrome\n",p);
return 0;
}

____________________________________________________________________
____________________
19. Digit Sum
____________________________________________________________________
_____________________

// Digit Sum

#include<stdio.h>
main()
{
int n,i,r,s=0;
printf("Enter Number\n");
scanf("%d",&n);

while(n!=0)
{
r = n % 10;
n = n / 10;
s = s + r;
}

printf("Digitsum = %d\n",s);

return 0;
}

____________________________________________________________________
____________________
20. Armstrong Number
____________________________________________________________________
_____________________

// Armstrong Number

#include<stdio.h>
int main()
{
int a,n,i,r,s=0;
printf("Enter Number\n");
scanf("%d",&n);
a=n;
while(n!=0)
{
r = n % 10;
n = n / 10;
s = s + r*r*r;
}

if(a==s)
printf("%d is Armstrong Number",a);
else
printf("%d is not Armstrong Number",a);
return 0;
}

____________________________________________________________________
____________________
21. Identify Perfect number
____________________________________________________________________
_____________________

// Identify Perfect number

#include<stdio.h>

void main()
{
int num, i = 1, sum = 0;
printf("Enter a number: ");
scanf("%d",&num);
while(i < num)
{
if(num % i == 0)
{
sum = sum + i;
}
i++;
}
if(sum == num)
printf("%d is a perfect number.", num);
else
printf("%d is not a perfect number",num);
}

____________________________________________________________________
____________________
22. Perfect number in Range
____________________________________________________________________
_____________________

// Perfect number in Range


#include<stdio.h>
int main()
{

int min, max,i,j,s=0;

printf("Perfect number between number to number : \n");


scanf("%d %d",&min,&max);

for(i=min;i<=max;i++)
{
j=1;
while(j<i)
{
if(i%j==0)
{
s=s+j;
}
j++;
}
if(s==i)
printf("%d\n",i);
}
return 0;
}

____________________________________________________________________
____________________
23. Perfect to Prime
____________________________________________________________________
_____________________

// Perfect to Prime

#include<stdio.h>

void main()
{
int num, i = 1, sum = 0;
printf("Enter a number: ");
scanf("%d",&num);
while(i <= num)
{
if(num % i == 0)
{
sum = sum + i;
}
i++;
}
if(sum == num + 1)
printf("%d is a prime number.", num);
else
printf("%d is not a prime number",num);
}

____________________________________________________________________
____________________
24. Identify Prime number
____________________________________________________________________
_____________________

// Identify Prime number

#include<stdio.h>
main()
{

int n,i;

printf("Number ? ");
scanf("%d",&n);

i=2;

while(i<n/2)
{

if(n%i==0)
{

printf("%d is not prime\n",n);

break;
}

i=i+1;
}

if(i==n/2)
printf("%d is prime\n",n);

return 0;

____________________________________________________________________
____________________
25. Prime numbers in a Range
____________________________________________________________________
_____________________
// Prime numbers in a Range

#include<stdio.h>
main()
{

int min, max,i,j,k=0;

printf("Prime between number to number ? ");


scanf("%d %d",&min,&max);

for(i=min;i<=max;i++)
{
for(j=2; j<i;j++)
{

if(i%j==0)
{
break;
}
}
if(i==j)
{

printf("%d \n",i);
k=k+1;
}

printf("number of primes %d\n",k);


return 0;

____________________________________________________________________
____________________
26. For Alphabet
____________________________________________________________________
_____________________

// For Alphabet

#include <stdio.h>
int main()
{
int alphabet;
for(alphabet='A';alphabet<='Z';alphabet=alphabet+1)
{
printf("%d\n",alphabet);
}
putchar('\n');
return(0);
}

____________________________________________________________________
____________________
27. For Alphabet
____________________________________________________________________
_____________________

// For Alpha2

#include<stdio.h>

int main()
{

int alpha,code;

for(alpha='A';alpha<='G';alpha=alpha+1)
{

for(code=1;code<=7;code=code+1)

printf("%c %d\t",alpha,code);
}
putchar('\n');
}
return(0);
}

____________________________________________________________________
____________________
28. Multiplicaion Table
____________________________________________________________________
_____________________

// Multiplicaion Table

#include<stdio.h>
//Multiplication Table
int main()
{

int i,j,r,s;

putchar('\n');
printf("Multiplication Table from what number to what number ?");
scanf("%d %d",&r,&s);
for(i=1;i<=10;i=i+1)
{

for(j=r;j<=s;j=j+1)

printf(" %d x %2d = %d\t",j,i,i*j);


}
putchar('\n');
}
return(0);
}

____________________________________________________________________
____________________
29. Sort
____________________________________________________________________
_____________________

// Sort

#include<stdio.h>
int main()
{
int i,n,j,t;
int a[100];

printf("How many \n");


scanf("%d",&n);
printf("Input numbers \n");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);

}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}

printf("The numbers in ascending order : \n");


for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("\nThe smallest number is %d\n",a[0]);
printf("The largest number is %d\n\n",a[n-1]);
return 0;
}

____________________________________________________________________
____________________
30. Sort
____________________________________________________________________
_____________________

// Sort

#include<stdio.h>
int main()
{
int i,j,t,k=0;
int a[100];

printf("Input numbers -1 to end\n");

for(i=0;i<100;i++)
{
scanf("%d",&a[i]);
k=k+1;
if(a[i]==-1)
break;
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(a[i]>a[j])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}

printf("The numbers in ascending order : \n");


for(i=0;i<k;i++)
{
printf("%d\n",a[i]);
}
printf("\nThe smallest number is %d\n",a[1]);
printf("The largest number is %d\n\n",a[k-1]);
return 0;
}

____________________________________________________________________
____________________
31. Switch
____________________________________________________________________
_____________________

// Switch

#include <stdio.h>

int main()
{
int code;
printf("Enter the error code (1-3): ");
scanf("%d",&code);
switch(code)
{
case 1:
puts("Drive Fault, not your fault.");
break;
case 2:
puts("Illegal format, call a lawyer.");
break;
case 3:
puts("Bad filename, spank it.");
break;
default:
puts("That's not 1, 2, or 3");
}
return(0);
}

____________________________________________________________________
____________________
32. Switch
____________________________________________________________________
_____________________

// Switch Traffic

#include<stdio.h>

void main()
{
char t;
printf("Press g, r or y\n");
scanf("%c",&t);
switch(t)
{
case 'g': printf("Green : You can go.\n");
break;
case 'G': printf("Green : You can go.\n");
break;
case 'r': printf("Red : Stop !\n");
break;
case 'R': printf("Red : Stop !\n");
break;
case 'y': printf("Blue : Get ready.\n");
break;
case 'Y': printf("Blue : Get ready.\n");
break;
default: printf("Press either g or r or y\n");
}
}

____________________________________________________________________
____________________
33. Swich Traffic
____________________________________________________________________
_____________________

// Switch Traffic

#include<stdio.h>

int main()
{
char t;
printf("Press g, r or y. Press N for exit\n");
scanf("%c",&t);

while(t!='N')
{
t=getchar();
switch(t)
{
case 'g': printf("Green : You can go.\n");
break;
case 'G': printf("Green : You can go.\n");
break;
case 'r': printf("Red : Stop !\n");
break;
case 'R': printf("Red : Stop !\n");
break;
case 'y': printf("Yellow : Get ready.\n");
break;
case 'Y': printf("Yellow : Get ready.\n");
break;
default: printf("Press either g or r or y. Press N for exit
\n");

if(t=='N')
printf("Goodbye!\n");
}
}
return 0;
}

____________________________________________________________________
____________________
34. Switch M/F
____________________________________________________________________
____________________

//Switch M/F

#include<stdio.h>

void main()
{
int i;
printf("Please enter 0 or 1 and 100 to exit\n");
while(i != 100)
{
scanf("%d",&i);
switch(i)
{
case 0: printf("Male\n");
break;
case 1: printf("Female\n");
break;
default: printf("Please enter 0 or 1.\n");
}
}

____________________________________________________________________
____________________
35. Simple Calculator using switch
____________________________________________________________________
_____________________

//Simple Calculator using switch

// Performs addition, subtraction, multiplication or division


depending the input from user

# include <stdio.h>

int main() {

char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,/): ");
scanf("%c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf",&firstNumber, &secondNumber);

switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf\n",firstNumber, secondNumber,
firstNumber + secondNumber);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf\n",firstNumber, secondNumber,
firstNumber - secondNumber);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf\n",firstNumber, secondNumber,
firstNumber * secondNumber);
break;

case '/':
printf("%.1lf / %.1lf = %.1lf\n",firstNumber, secondNumber,
firstNumber / secondNumber);
break;

// operator doesn't match any case constant (+, -, *, /)


default:
printf("Error! operator is not correct\n");
}

return 0;
}

____________________________________________________________________
____________________
36. Array string
____________________________________________________________________
_____________________

//Array string

#include<stdio.h>

void main()
{
char name[10], address[15];
printf("\nPlease enter your name:\n");
scanf("%s", name);
printf("Your name is: %s",name);
}

____________________________________________________________________
____________________
37. Array string gets & puts
____________________________________________________________________
_____________________

//Array string gets & puts

#include<stdio.h>

void main()
{
char name[10]; //creates 10 rooms in which the last value is
'\0'. for 10 lenght
char address[15];
printf("\nPlease enter your name:\n");
gets(name);
printf("Your name is: ");
puts(name);
printf("Address: ");
gets(address);
puts(address);
}

____________________________________________________________________
____________________
38. Sum of Dgits
____________________________________________________________________
_____________________

// Sum of Dgits

#include<stdio.h>
int main()
{
int i,n;
long s=0;
int a[100];
printf("How many \n");
scanf("%d",&n);
printf("Input numbers \n");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s= s + a[i];
}
printf("Sum = %ld \n",s);
return 0;

____________________________________________________________________
____________________
39. Matrix Sum
____________________________________________________________________
_____________________

// Matrix Sum

#include<stdio.h>

int main()
{
int i,j,m,n,a[10][10],b[10][10],c[10][10];
printf("\nEnter size m x n of matrix A\n");
scanf("%d %d",&m,&n);
printf("Enter rowwise elements of A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf(" \n A = ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d\t",a[i][j]);
}
printf("\n");

}
printf("\nEnter size m x n of matrix B\n");
scanf("%d %d",&m,&n);
printf("Enter rowwise elements of B\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf(" \n B = ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d\t",b[i][j]);
}
printf("\n");

printf(" \n A+B = ");


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

____________________________________________________________________
____________________
40. Metrix Product
____________________________________________________________________
_____________________

// matrix Product

#include <stdio.h>

int main()
{
int a[10][10], b[10][10], c[10][10], m, n, p, q, i, j, k;

printf("Enter rows and column for first matrix A: ");


scanf("%d %d", &m, &n);

printf("Enter rows and column for second matrix B: ");


scanf("%d %d",&p, &q);
while (n != p)
{
printf("Error! column of first matrix not equal to row of
second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &m, &n);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&p, &q);
}
printf("\nEnter elements of matrix A:\n");
for(i=0; i<m; ++i)
for(j=0; j<n; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf(" \n A = ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d\t",a[i][j]);
}
printf("\n");

// Storing elements of second matrix.


printf("\nEnter elements of matrix B:\n");
for(i=0; i<p; ++i)
for(j=0; j<q; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
printf(" \n B = ");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d\t",b[i][j]);
}
printf("\n");

}
// Initializing all elements of result matrix to 0
for(i=0; i<m; ++i)
for(j=0; j<q; ++j)
{
c[i][j] = 0;
}

// Multiplying matrices A and B and


// storing result in result matrix
for(i=0; i<m; ++i)
for(j=0; j<q; ++j)
for(k=0; k<n; ++k)
{
c[i][j]+=a[i][k]*b[k][j];
}

// Displaying the result


printf("\nOutput Matrix \nAB =");
for(i=0; i<m; ++i)
for(j=0; j<q; ++j)
{
printf("\t%d\t ", c[i][j]);
if(j == q-1)
printf("\n\n");
}
return 0;
}

____________________________________________________________________
____________________
41. Factorial
____________________________________________________________________
_____________________

// Factorial

#include<stdio.h>

void main()

{
printf("Factorial Calculator\n");
int i = 1, n;
float prod = 1;
printf("Enter n: ");
scanf("%d",&n);
if(n > 0)
{
while(i <= n)
{
prod = prod * i;
i = i + 1;

}
printf("%d! = %.0f", n, prod);
}
else
if(n == 0)
printf("%d! = 1",n);
else
if(n < 0)
printf("(%d)! is not defined.",n);

____________________________________________________________________
____________________

42. Factorial as Recursive Function


____________________________________________________________________
_____________________
//Factorial as Recursive Function

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

int fact(int);

int main() {
int factorial, num;

printf("Enter the value of num :");


scanf("%d", &num);

factorial = fact(num);
printf("Factorial is %d\n", factorial);

return 0;
}

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

____________________________________________________________________
____________________
43. Fibonacci as Recursive
Function
____________________________________________________________________
_____________________

// Fibonacci as Recursive Function

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

int size;
int fibonacci(int prev_number, int number);

int main() {
static int prev_number = 0, number = 1;
//clrscr();

printf("Enter the Size of Series (< 20) : ");


scanf("%d", &size);

printf("1 ");
fibonacci(prev_number, number);
//getch();
}

int fibonacci(int prev_number, int number) {


static int i = 1;
int next_num;

if (i == size)
return (0);
else {
next_num = prev_number + number;
prev_number = number;
number = next_num;
printf("%d ", next_num);

i++;
fibonacci(prev_number, number); //recursion
}
return (0);
}

____________________________________________________________________
____________________
44. Permutation and Combination
____________________________________________________________________
_____________________

// nPr & nCr

#include<stdio.h>
long factorial(int);
long ncr(int,int);
long npr(int,int);
int main()
{
int n,r;
long x,y;
printf("Enter n and r : \n");
scanf("%d %d",&n,&r);
x=ncr(n,r);
y=npr(n,r);
printf("C(%d,%d)=%ld\n",n,r,x);
printf("P(%d,%d)=%ld\n",n,r,y);
return 0;

}
long ncr(int n,int r)
{
long result;
result=factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long npr(int n,int r)
{
long result;
result=factorial(n)/factorial(n-r);
return result;
}
long factorial(int n)
{
int i;
long result=1;
for(i=1;i<=n;i++)

result=result*i;

return result;
}

____________________________________________________________________
____________________
45. Graph as Function
____________________________________________________________________
_____________________

// Graph as Function

#include <stdio.h>
void graph(int count);
int main()
{
int value;
value = 2;
while(value<=64)
{
graph(value);
printf("Value is %d\n",value);
value = value * 2;
}
return(0);
}
void graph(int count)
{
int x;
for(x=0;x<count;x=x+1)
putchar('*');
putchar('\n');
}

____________________________________________________________________
____________________
46. Variable Type
____________________________________________________________________
_____________________
Variable Type

//Variable Type 1

void f() {
int i;
i = 1; // OK: in scope
}
void g() {
i = 2; // Error: not in scope
}

//Variable Type 2

int i;
int f()
{
i = 1; // OK: in scope
printf("\n %d \n",i);
}
int g() {
i = 2; // OK: still in scope
printf("\n % d \n",i);
}
#include<stdio.h>
//int f();
//int g();
int main()
{
//int x, y;
f();
g();
//printf("%d \n%d\n",x,y);
printf("Hallo World !\n");
}

// Variable3

//Variable Type 3

#include<stdio.h>
int main()
{

for (int i = 0; i < 5; ++i)


{
static int n = 0;
printf("%d ", n++); // prints 0 1 2 3 4 - the previous value
persists
}
printf("\n");
for (int j = 0; j < 5; ++j)
{
int n = 0;
printf("%d ", ++n); // prints 1 1 1 1 1 - the previous value
is lost
}
printf("\n");
return 0;
}

____________________________________________________________________
____________________
47. Filehandling
____________________________________________________________________
_____________________

// Filehandling 1

#include<stdio.h>

main()
{
FILE *f1;
char c;
printf("Data Input\n\n");
/* Open the file INPUT */
f1 = fopen("INPUT", "w");

/* Get a character from keyboard */


while((c=getchar()) != EOF)

/* Write a character to INPUT */


putc(c,f1);
/* Close the file INPUT */
fclose(f1);
printf("\nData Output\n\n");
/* Reopen the file INPUT */
f1 = fopen("INPUT","r");

/* Read a character from INPUT*/


while((c=getc(f1)) != EOF)

/* Display a character on screen */


printf("%c",c);

/* Close the file INPUT */


fclose(f1);
}

____________________________________________________________________
____________________
48. Filehandling
____________________________________________________________________
_____________________

// Filehandling 2

#include <stdio.h>
main()
{
FILE *f1, *f2, *f3;
int number, i;

printf("Contents of DATA file\n\n");


f1 = fopen("DATA", "w"); /* Create DATA file */
for(i = 1; i <= 30; i++)
{
scanf("%d", &number);
if(number == -1) break;
putw(number,f1);
}
fclose(f1);

f1 = fopen("DATA", "r");
f2 = fopen("ODD", "w");
f3 = fopen("EVEN", "w");

/* Read from DATA file */


while((number = getw(f1)) != EOF)
{
if(number %2 == 0)
putw(number, f3); /* Write to EVEN file */
else
putw(number, f2); /* Write to ODD file */
}
fclose(f1);
fclose(f2);
fclose(f3);

f2 = fopen("ODD","r");
f3 = fopen("EVEN", "r");
printf("\n\nContents of ODD file\n\n");

while((number = getw(f2)) != EOF)


printf("%4d", number);
printf("\n\nContents of EVEN file\n\n");

while((number = getw(f3)) != EOF)


printf("%4d\n\n", number);

fclose(f2);
fclose(f3);
}

____________________________________________________________________
____________________
49. Filehandling
____________________________________________________________________
_____________________

// Filehandling 3

#include <stdio.h>

main()
{
FILE *fp;
int number, quantity, i;
float price, value;
char item[10], filename[10];

printf("Input file name\n");


scanf("%s", filename);
fp = fopen(filename, "w");
printf("Input inventory data\n\n");
printf("Item name Number Price Quantity\n");
for(i = 1; i <= 3; i++)
{
fscanf(stdin, "%s %d %f %d",
item, &number, &price, &quantity);
fprintf(fp, "%s %d %.2f %d",
item, number, price, quantity);
}
fclose(fp);
fprintf(stdout, "\n\n");

fp = fopen(filename, "r");

printf("Item name Number Price Quantity Value\n");


for(i = 1; i <= 3; i++)
{
fscanf(fp, "%s %d %f d",item,&number,&price,&quantity);
value = price * quantity;
fprintf(stdout, "%-8s %7d %8.2f %8d %11.2f\n",
item, number, price, quantity, value);
}
fclose(fp);
}
____________________________________________________________________
____________________
50. Filehandling
____________________________________________________________________
_____________________

// Filehandling 4

#include <stdio.h>

main()
{
char *filename;
FILE *fp1, *fp2;
int i, number;

fp1 = fopen("TEST", "w");


for(i = 10; i <= 100; i += 10)
putw(i, fp1);

fclose(fp1);

printf("\nInput filename\n");

open_file:
scanf("%s", filename);

if((fp2 = fopen(filename,"r")) == NULL)


{
printf("Cannot open the file.\n");
printf("Type filename again.\n\n");
goto open_file;
}
else

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


{ number = getw(fp2);
if(feof(fp2))
{
printf("\nRan out of data.\n");
break;
}
else
printf("%d\n", number);
}

fclose(fp2);
}

____________________________________________________________________
____________________
51. Accessing Structure Members
____________________________________________________________________
_____________________

// Structure 1

struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};
main()
{
struct personal person;
printf("Input Values\n");
scanf("%s %d %s %d %f",
person.name,
&person.day,
person.month,
&person.year,
&person.salary);
printf("%s %d %s %d %.2f\n",
person.name,
person.day,
person.month,
person.year,
person.salary);
}

____________________________________________________________________
____________________
52. Comparing Structure members
____________________________________________________________________
_____________________

// Structure 2.1

struct class
{
int number;
char name[20];
float marks;
};

main()
{
int x;
struct class student1 = {111,"Rao",72.50};
struct class student2 = {222,"Reddy", 77.00};
struct class student3;
student3 = student2;
printf("%d %s %f\n", student3.number,
student3.name,
student3.marks);

x = (student3.marks >= student1.marks) ? 1 : 0;

if(x == 1)
{
printf("\nstudent3 is better\n\n");

}
else
printf("\nstudent1 is better\n\n");

____________________________________________________________________
____________________
53. Structure of array
____________________________________________________________________
_____________________

// Structure 3

struct marks
{
int sub1;
int sub2;
int sub3;
int total;
};
main()
{
int i;
struct marks student[3] = {{45,67,81,0},
{75,53,69,0},
{57,36,71,0}};
struct marks total;
for(i = 0; i <= 2; i++)
{
student[i].total = student[i].sub1 +
student[i].sub2 +
student[i].sub3;
total.sub1 = total.sub1 + student[i].sub1;
total.sub2 = total.sub2 + student[i].sub2;
total.sub3 = total.sub3 + student[i].sub3;
total.total = total.total + student[i].total;
}
printf(" STUDENT TOTAL\n\n");
for(i = 0; i <= 2; i++)
printf("Student[%d] %d\n", i+1,student[i].total);
printf("\n SUBJECT TOTAL\n\n");
printf("%s %d\n%s %d\n%s %d\n",
"Subject 1 ", total.sub1,
"Subject 2 ", total.sub2,
"Subject 3 ", total.sub3);

printf("\nGrand Total = %d\n", total.total);


}

____________________________________________________________________
____________________
54. Structure of array and
within array
____________________________________________________________________
_____________________

// Structure 4

main()
{
struct marks
{
int sub[3];
int total;
};
struct marks student[3] =
{45,67,81,0,75,53,69,0,57,36,71,0};
struct marks total;
int i,j;

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


{
for(j = 0; j <= 2; j++)
{
student[i].total += student[i].sub[j];
total.sub[j] += student[i].sub[j];
}
total.total += student[i].total;
}
printf("STUDENT TOTAL\n\n");
for(i = 0; i <= 2; i++)
printf("Student[%d] %d\n", i+1, student[i].total);

printf("\nSUBJECT TOTAL\n\n");
for(j = 0; j <= 2; j++)
printf("Subject-%d %d\n", j+1, total.sub[j]);

printf("\nGrand Total = %d\n", total.total);

}
____________________________________________________________________
____________________
55. Structure as Function
Parameter
____________________________________________________________________
_____________________

// Structure 5

/* Passing a copy of the entire structure */


struct stores
{
char name[20];
float price;
int quantity;
};
struct stores update (struct stores product, float p, int q);
float mul (struct stores stock);
main()
{
float p_increment, value;
int q_increment;

struct stores item = {"XYZ", 25.75, 12};

printf("\nInput increment values:");


printf(" price increment and quantity increment\n");
scanf("%f %d", &p_increment, &q_increment);

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
item = update(item, p_increment, q_increment);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
printf("Updated values of item\n\n");
printf("Name : %s\n",item.name);
printf("Price : %f\n",item.price);
printf("Quantity : %d\n",item.quantity);

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
value = mul(item);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
printf("\nValue of the item = %f\n", value);
}
struct stores update(struct stores product, float p, int q)
{
product.price += p;
product.quantity += q;
return(product);
}
float mul(struct stores stock)
{
return(stock.price * stock.quantity);
}

____________________________________________________________________
____________________
56. Increment & Decrement
____________________________________________________________________
_____________________

#include<stdio.h>

void main()
{
int m, n, p, q, r, s;
m = 5;
n = 3;
p = 4;
q = -1;
r = p + (q--) + m - (++n);//4 + -1 + 5 - 4
s = ++p - q-- + m;

printf("The value of r = %d",r);


printf("\nq = %d",q);
printf("\nThe value of s = %d",s);
}

You might also like