1.
#include<stdio.h>
double matrixAvg(int m,int n,double a[m][n])
{int i,j;
double average,sum=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
sum=sum+a[i][j];}
}average=sum/(m*n);
return average;}
int main()
{int M,N,i,j;
scanf("%d%d",&M,&N);
double a[M][N],aver;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
scanf("%lf",&a[i][j]);
aver=matrixAvg(M,N,a);
printf("%.2f\n",aver);
return 0; }
2.#include<stdio.h>
void arrayShiftRight(int arr[], int n, int m)
{
for (int i = 0; i < m; i++)
{
int t = arr[n - 1];
for (int j = n - 1; j >= 0; j--)
arr[j] = arr[j - 1];
arr[0] = t;
}
}
int main() {
int i, n, m;
scanf("%d%d", &n, &m);
int arr[100];
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
arrayShiftRight(arr, n, m);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
3.#include<stdio.h>
void sort(int a[],int n){
int i,j,t;
for(j=0;j<n-1;j++)
for(i=0;i<n-1-j;i++)
if(a[i]>a[i+1]){
t=a[i];
a[i]=a[i+1];
a[i+1]=t;}
}
int main(){
int i, n;
scanf("%d",&n);
int a[n];
for( i=0; i<n; i++ )
scanf("%d", &a[i]);
sort(a, n);
printf("After sorted the array is:");
for( i = 0; i < n; i++ )
printf(" %d", a[i]);
printf("\n");
return 0;
}
4.#include<stdio.h>
#include<math.h>
int reverse(int number){
int num=0,b[100],i,n,p=fabs(number),t;
if(number>0) t=1;
else t=-1;
while(p!=0){
n=p%10;
p=p/10;
b[num]=n;
num++;}
int result=b[0];
for(i=0;i<num-1;i++)
result=result*10+b[i+1];
return result*t;
}
int main(){
int n;
scanf("%d", &n);
printf("%d\n", reverse(n));
return 0;
}
5.#include<stdio.h>
double ave(double a[],int n){
double sum=0,average;
int i;
for(i=0;i<n;i++)
sum=sum+a[i];
average=sum/n;
return average;}
int main(void){
int N;
scanf("%d",&N);
double x[N],p;
int i;
for(i=0;i<N;i++)
scanf("%lf",&x[i]);
p=ave(x,N);
printf("ave=%.2f\n",p);
return 0;
}
6.#include<stdio.h>
double result_real, result_imag;
void complex_prod( double x1, double y1, double x2, double y2 ){
result_real=x1*x2-y1*y2;
result_imag=x1*y2+x2*y1;}
int main(void) {
double imag1, imag2, real1, real2;
scanf("%lf %lf", &real1, &imag1); scanf("%lf %lf", &real2, &imag2); complex_prod(real1, imag1, real2, imag2);
printf("product of complex is (%f)+(%f)i\n", result_real, result_imag);
return 0;
}
7.
#include<stdio.h>
int search(int n)
{
int i, count = 0, p, t, q;
for (i = 11; i * i <= n; i++) {
int w = i * i;
p = w;
t = w % 10;
w = w / 10;
w = w % 10;
p = p / 100;
if (w == p) count++;
else if (t == w || t == p) count++;
}
return(count);
}
int main() {
int number;
scanf("%d", &number);
printf("count=%d\n", search(number));
return 0;
}
8.#include<stdio.h>
int f(int n) {
int i, a[100],result;
a[0] = 1; a[1] = 1;
for (i = 2; i < n; i++) {
a[i] = a[i - 2] + a[i - 1];
}result = a[n-1];
return result;
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", f(n));
return 0;
}
9.#include<stdio.h>
int Factorial( const int N ){
int s;
if(N<0) return 0;
else if(N==1) s=1;
else s=Factorial(N-1)*N;
return(s);}
int main(){
int N, NF;
scanf("%d", &N);
NF = Factorial(N);
if (NF!=0) printf("%d! = %d\n", N, NF);
else printf("Invalid input\n");
return 0;
}
10.#include<stdio.h>
int even(int n) {
if (n % 2 == 0) return 1;
else return 0;
}
int main() {
int n;
scanf("%d", &n);
if (even(n))
printf("%d is even.\n", n);
else printf("%d is odd.\n", n);
return 0;
}
11.#include<stdio.h>
#include<string.h>
int isPalindromicString(char s[]){
int n=strlen(s),i=0,j=n-1;
for(i=0;i<=n/2;i++){
if(s[i]!=s[j]){
return 0;}
j--;}
return 1;
}
int main(){
char s[100];
gets(s);
if(isPalindromicString(s)==1)
printf("Yes");
else
printf("No");
return 0;}
12.#include<stdio.h>
int f(int n)
{int t=0,i;
for(;n>0;){
i=n%10;
n=n/10;
if(i==1||i==2) t++;}
return(t);}
void main()
{
int i,j=0,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
j=j+f(i);
printf("%d\n%d",j,j);
}
13.
#include<stdio.h>
#include<string.h>
#include<math.h>
typedef struct
{
char s[1000];
int len;
}Str;
int Index (Str S,Str T)
{
int pos=0;
int i=0,j=0;
while (i<S.len && j<T.len)
{
if (S.s[i] == T.s[j])
{
printf("%c",S.s[i]);
++i;++j;
}
else
{
printf("%c",S.s[i]);
j=0;
i = ++pos;
}
}
putchar('\n');
if (j==T.len)
return pos;
else
return -1;
}
int main()
{
Str S, T;
while(~scanf("%s",S.s))
{
scanf("%s",T.s);
S.len = strlen(S.s);
T.len = strlen(T.s);
int ans = Index(S,T);
printf("%d\n",ans+1);
}
return 0;
}
14.#include<stdio.h>
int result(double x){
double z;
if(x>=5) z=x*x+4*x-2;
else if(x>=0&&x<5) z=3*x+2;
else z=0;
return z;}
int main()
{double z,x;
scanf("%lf",&x);
z=result(x);
printf("f(%.1f) = %.1f",x,z);
return 0;
}