CProgram
CProgram
____________________________________________________________________
_____________________
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;
____________________________________________________________________
____________________
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
____________________________________________________________________
_____________________
#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;
____________________________________________________________________
____________________
11. I/O
____________________________________________________________________
_____________________
// I/O 2
#include <stdio.h>
#include <ctype.h>
____________________________________________________________________
____________________
12. I/O
____________________________________________________________________
_____________________
// I/O 3
#include <stdio.h>
#include <ctype.h>
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>
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
____________________________________________________________________
_____________________
#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
____________________________________________________________________
_____________________
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
____________________________________________________________________
_____________________
#include<stdio.h>
main()
{
int n,i;
printf("Number ? ");
scanf("%d",&n);
i=2;
while(i<n/2)
{
if(n%i==0)
{
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()
{
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;
}
____________________________________________________________________
____________________
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)
____________________________________________________________________
____________________
29. Sort
____________________________________________________________________
_____________________
// Sort
#include<stdio.h>
int main()
{
int i,n,j,t;
int a[100];
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;
}
}
}
____________________________________________________________________
____________________
30. Sort
____________________________________________________________________
_____________________
// Sort
#include<stdio.h>
int main()
{
int i,j,t,k=0;
int a[100];
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;
}
}
}
____________________________________________________________________
____________________
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
____________________________________________________________________
_____________________
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,/): ");
scanf("%c", &operator);
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;
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
____________________________________________________________________
_____________________
#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");
____________________________________________________________________
____________________
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;
}
// Initializing all elements of result matrix to 0
for(i=0; i<m; ++i)
for(j=0; j<q; ++j)
{
c[i][j] = 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);
____________________________________________________________________
____________________
#include<stdio.h>
//#include<conio.h>
int fact(int);
int main() {
int factorial, 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
____________________________________________________________________
_____________________
#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("1 ");
fibonacci(prev_number, number);
//getch();
}
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
____________________________________________________________________
_____________________
#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()
{
____________________________________________________________________
____________________
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");
____________________________________________________________________
____________________
48. Filehandling
____________________________________________________________________
_____________________
// Filehandling 2
#include <stdio.h>
main()
{
FILE *f1, *f2, *f3;
int number, i;
f1 = fopen("DATA", "r");
f2 = fopen("ODD", "w");
f3 = fopen("EVEN", "w");
f2 = fopen("ODD","r");
f3 = fopen("EVEN", "r");
printf("\n\nContents of ODD file\n\n");
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];
fp = fopen(filename, "r");
// Filehandling 4
#include <stdio.h>
main()
{
char *filename;
FILE *fp1, *fp2;
int i, number;
fclose(fp1);
printf("\nInput filename\n");
open_file:
scanf("%s", filename);
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);
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);
____________________________________________________________________
____________________
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;
printf("\nSUBJECT TOTAL\n\n");
for(j = 0; j <= 2; j++)
printf("Subject-%d %d\n", j+1, total.sub[j]);
}
____________________________________________________________________
____________________
55. Structure as Function
Parameter
____________________________________________________________________
_____________________
// Structure 5
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
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;