Professional Documents
Culture Documents
ACP Theory DA - 1.odt
ACP Theory DA - 1.odt
Problem 1
Problem 2
Problem 3
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
double principal = FIVE_LAKHS;
double years = 10.0;
double rate = 3.5/100;
return 0;
}
Problem 5
1. %2d implies the minimum width the output should be i.e. 2 units. If the
width of the output is less than 2 units, blank spaces will preceed the output
as filler. In our case the output length is greater than two so the full output
will be printed.
2. %10.2d implies the minimum width the output should be i.e. 10 units. If
the width of the output is less than 10 units, blank spaces will preceed the
output as filler. The formatting .2 implies the minimum length of the output
without the filler, preeceding zeros will be used to attain this. In our case
the output length is 5 so 5 more spaces will be printed before the actual
output to make the total length 10.
3. %-10.2d implies the minimum width the output should be i.e. 10 units.
The output should be left aligned ( because – sign is used ). If the width of
the output is less than 10 units, blank spaces will succeed the output as
filler. The formatting .2 implies the minimum length of the output without
the filler, preeceding zeros will be used to attain this. In our case the output
length is 5 so 5 more spaces will be printed after the actual output to make
the total length 10.
4. %-7d implies the minimum width the output should be i.e. 7 units. The
output should be left aligned ( because – sign is used ). If the width of the
output is less than 7 units, blank spaces will succeed the output as filler. In
our case the output length is 3 so 4 more spaces will be printed after the
actual output to make the total length 7.
5. %07.2d implies the minimum width the output should be i.e. 7 units. If
the width of the output is less than 7 units, blank spaces will preceed the
output as filler. The formatting .2 implies the minimum length of the output
without the filler, preeceding zeros will be used to attain this. In our case
the output length is 3 so 4 more spaces will be printed before the actual
output to make the total length 7.
6. %07d implies the minimum width the output should be i.e. 7 units. Zeros
will be used as filler instead of blank spaces ( because 0 is specifically
mentioned in the placeholder ). If the width of the output is less than 7
units, zeros will preceed the output as filler. In our case the output length is
3 so 4 zeros will be printed before the actual output to make the total length
7.
7. %+0-9.4d implies the minimum width the output should be i.e. 9 units. A
sign bit will preceed the output. The formatting .4 implies the minimum
length of the output without the filler, preeceding zeros will be used to attain
this. The output should be left aligned ( because – sign is used ). If the
width of the output is less than 9 units, blank spaces will succeed the
output as filler. In our case ‘+0123 ‘ will be printed.
8. %+09.4d implies the minimum width the output should be i.e. 9 units. A
sign bit will preceed the output. The formatting .4 implies the minimum
length of the output without the filler, preeceding zeros will be used to attain
this. If the width of the output is less than 9 units, blank spaces will preceed
the output as filler. In our case ‘ +0123‘ will be printed.
9. %+07d implies the minimum width the output should be i.e. 7 units. A
sign bit will preceed the output. Zeros will be used as filler instead of blank
spaces ( because 0 is specifically mentioned in the placeholder ). If the
width of the output is less than 7 units, zeros will preceed the output as
filler. In our case ‘+000123‘ will be printed.
Problem 6
Problem 7
Problem 8
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
double x;
printf("Enter the value of x : ");
scanf("%lf",&x);
if(x==0.0){
printf("Value of x must be nonzero: try again\n");
return 0;
} else {
printf("Value of sin(1/x) is %.4lf\n",sin(1.0/x));
}
return 0;
}
Problem 9
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
Problem 10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef long long int ll;
ll get_series_sum(int n){
ll ans = 0l;
ll cnt = 0ll;
for(ll i = 1l; i<=(ll)n; i+=cnt)
{ ans += (ll)pow(i,4l); +
+cnt;
}
return ans;
}
int main(void){
int n;
printf("Enter a +ve integer less than 50: ");
scanf("%d",&n);
if(n>=1 && n<50){
printf("Sum of series is
%lld\n",get_series_sum(n)); } else {
printf("Invalid input\n");
}
return 0;
}
Problem 11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void){
int n, mini = INT32_MAX, maxi = INT32_MIN, positives = 0, sum =
0; float avg = 0.0f;
while(1){
printf("Enter a +ve integer: ");
scanf("%d",&n);
if(n<=0){
if (positives==0){
printf("No positive number entered\n");
return 0;
}
printf("Number of +ve values entered is
%d\n",positives); printf("Maximum value entered is
%d\n",maxi); printf("Minimum value entered is
%d\n",mini); printf("Average value is %.4f\n",avg); return
0;
} else { positives++;
mini =
min(mini,n);
maxi = max(maxi,n);
sum += n;
avg = ((float)sum)/((float)positives);
}
}
return 0;
}
Problem 12
Problem 13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void){
ll salary;
long double ans = 0LL;
printf("Enter salary : ");
scanf("%lld",&salary);
}
else if (salary>=4001ll && salary<=8000ll){
ans = salary + ((20.0/100.0)*salary) + ((60.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);
}
else if (salary>=8001ll && salary<=12000ll){
ans = salary + ((25.0/100.0)*salary) + ((70.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);
}
else if (salary>12000ll){
ans = salary + ((30.0/100.0)*salary) + ((80.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);
} else {
printf("Invalid salary \n");
}
return 0;
}
Problem 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void){
char* str = "MONKEY";
int processed[strlen(str)];
for(int i = 0; i<strlen(str); i++){
processed[i]=0;
}
permute("",str,processed);
return 0;
}
Problem 15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void multiply(int** matrix_a, int** matrix_b, int ra, int ca, int rb, int cb)
{ if(ca!=rb){
printf("Can't multipy matrices, dimesions don't match \n");
return;
}
int** ans = get_matrix(ra,cb);
for(int i = 0; i<ra; ++i){
for(int j = 0; j<cb; ++j){
int k_sum = 0;
for(int k = 0; k<ca; ++k){
k_sum += (matrix_a[i][k]*matrix_b[k][j]);
}
ans[i][j] = k_sum;
}
}
print_matrix(ans,ra,cb);
}
int main(void){
printf("Enter dimensions for matrix A :
"); int ra,ca;
scanf("%d %d",&ra,&ca);
printf("Enter contents of matrix A \n");
int** matrix_a = get_matrix(ra,ca);
input_matrix(matrix_a,ra,ca);
multiply(matrix_a,matrix_b,ra,ca,rb,cb);
return 0;
}