Term-2 Problems :
## Solution for problem 1 :
#include <stdio.h>
int main()
{
int i,j,p;
for(i=1;i<=12;i++)
{
for(j=1;j<=12;j++)
{
p=i*j;
printf("%3d ",p);
};
printf("\n");
};
return 0;
}
#include <stdio.h>
int main()
{
int i,j,p;
for(i=1;i<=12;i++)
{
for(j=1;j<=12;j++)
{
p=i*j;
printf("%3d ",p);
};
printf("\n");
};
return 0;
}
## Solution for problem 2 :
#include <stdio.h>
#include <math.h>
float SD(float a[], int n);
int main()
{
int n, i;
printf("Enter the number of data:");
scanf("%d",&n);
float a[n];
for(i=0; i<n; i++)
{
printf("Data no.%d is: ",i+1);
scanf("%f",&a[i]);
printf("");
}
printf("Standard Deviation = %.2f", SD(a,n));
return 0;
}
float SD(float a[], int n)
{
int j;
float x=0,y=0;
for(j=0; j<n;j++)
{
x=x+a[j]/n;
}
;
for(j=0; j<n;j++)
y=y+(a[j]-x)*(a[j]-x)/n;
y=sqrt(y);
return y;
}
#include <stdio.h>
#include <math.h>
float SD(float a[], int n);
int main()
{
int n, i;
printf("Enter the number of data:");
scanf("%d",&n);
float a[n];
for(i=0; i<n; i++)
{
printf("Data no.%d is: ",i+1);
scanf("%f",&a[i]);
printf("");
}
printf("Standard Deviation = %.2f", SD(a,n));
return 0;
}
float SD(float a[], int n)
{
int j;
float x=0,y=0;
for(j=0; j<n;j++)
{
x=x+a[j]/n;
}
;
for(j=0; j<n;j++)
y=y+(a[j]-x)*(a[j]-x)/n;
y=sqrt(y);
return y;
}
## Solution for problem 3 :
#include <stdio.h>
int main(){
int i,n;
float p=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
float x[n];
printf("\n");
for(i=0;i<n;++i)
{
printf("Number %d is: ",i+1);
scanf("%f",&x[i]);
}
for(i=1;i<n;i++)
{
if(p<x[i])
p=x[i];
}
printf("The largest element is: %.2f \n",p);
return 0;
}
#include <stdio.h>
int main(){
int i,n;
float p=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
float x[n];
printf("\n");
for(i=0;i<n;++i)
{
printf("Number %d is: ",i+1);
scanf("%f",&x[i]);
}
for(i=1;i<n;i++)
{
if(p<x[i])
p=x[i];
}
printf("The largest element is: %.2f \n",p);
return 0;
}
## Solution for problem 4 :
Can't solve.
Can't solve.
## Solution for problem 5 :
#include<stdio.h>
int prime(int, int);
int main()
{
int p,q;
printf("enter the starting number\n");
scanf("%d",&p);
printf("enter the ending number\n");
scanf("%d",&q);
printf("\nthe prime numbers are:\n");
prime(p,q);
return 0;
}
int prime(int p,int q)
{
int n,c;
for(n=p;n<=q;n++)
{
for ( c=2;c<=n-1;c++ )
{
if ( n % c == 0 )
break;
};
if ( c == n )
{printf("%d\n",n);}
else ;
};
}
#include<stdio.h>
int prime(int, int);
int main()
{
int p,q;
printf("enter the starting number\n");
scanf("%d",&p);
printf("enter the ending number\n");
scanf("%d",&q);
printf("\nthe prime numbers are:\n");
prime(p,q);
return 0;
}
int prime(int p,int q)
{
int n,c;
for(n=p;n<=q;n++)
{
for ( c=2;c<=n-1;c++ )
{
if ( n % c == 0 )
break;
};
if ( c == n )
{printf("%d\n",n);}
else ;
};
}
## Solution for problem 6 :
#include<stdio.h>
int prime(int );
int armstrong(int a);
int main()
{
int a;
char ch[2];
printf("enter the number\n");
scanf("%d",&a);
printf("enter 'p' to check prime \nor 'a' to check armstrong\n\n");
scanf("%s",ch);
if(ch[0]=='p')
{
prime(a);
}
else if(ch[0]=='a')
{
armstrong(a);
}
else
{
printf("wrong input");
return 0;
};
}
int prime(int n)
{ int m,c;
{
for( c=2;c<n;c++ )
{
if ( n % c == 0 )
{
printf("%d is not a prime number\n",n);
return 0 ;
}
else;
};
if ( c == n )
{
printf("%d is a prime number\n",n);
}
else;
};
}
int armstrong(int a)
{ int c,d,e,m,i,t,p;
e=0;
m=a;
while(a!=0)
{
c=a%10;
d=c*c*c;
a=a/10;
e=e+d;
};
if (e==m)
{
printf("armstrong number\n");
}
else
{
printf("not armstrong number\n");
};
return 0;
}
#include<stdio.h>
int prime(int );
int armstrong(int a);
int main()
{
int a;
char ch[2];
printf("enter the number\n");
scanf("%d",&a);
printf("enter 'p' to check prime \nor 'a' to check armstrong\n\n");
scanf("%s",ch);
if(ch[0]=='p')
{
prime(a);
}
else if(ch[0]=='a')
{
armstrong(a);
}
else
{
printf("wrong input");
return 0;
};
}
int prime(int n)
{ int m,c;
{
for( c=2;c<n;c++ )
{
if ( n % c == 0 )
{
printf("%d is not a prime number\n",n);
return 0 ;
}
else;
};
if ( c == n )
{
printf("%d is a prime number\n",n);
}
else;
};
}
int armstrong(int a)
{ int c,d,e,m,i,t,p;
e=0;
m=a;
while(a!=0)
{
c=a%10;
d=c*c*c;
a=a/10;
e=e+d;
};
if (e==m)
{
printf("armstrong number\n");
}
else
{
printf("not armstrong number\n");
};
return 0;
}
## Solution for problem 7 :
#include <stdio.h>
scan_func(v);
int main()
{
int n,r,g,q,i,p,v;
int e=0;
printf("enter the number of rows.");
v=scan_func(v);
for(n=0;n<=v;n++)
{
e=e+n;
g=e;
for(i=q+1;i<=g;i++)
{
printf("%4d ",i);
};
printf("\n");
q=e;
};
return 0;
}
scan_func(int t)
{
scanf("%d",&t);
return t;
}
#include <stdio.h>
scan_func(v);
int main()
{
int n,r,g,q,i,p,v;
int e=0;
printf("enter the number of rows.");
v=scan_func(v);
for(n=0;n<=v;n++)
{
e=e+n;
g=e;
for(i=q+1;i<=g;i++)
{
printf("%4d ",i);
};
printf("\n");
q=e;
};
return 0;
}
scan_func(int t)
{
scanf("%d",&t);
return t;
}
## Solution for problem 8 :
#include<stdio.h>
int main(){
int a[10],i,g,n,m,c=0;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m)
{
printf("The number is found in the location: a[%d]\n ",i,i+1);
}
else printf("");
};
printf("\n");
for(i=0;i<=n-1;i++){
if(a[i]==m)
{
g=1;
}
else g=0;
};
if(g==0)
{
printf("The number is not found in any location\n");
}
else printf("");
return 0;
}
#include<stdio.h>
int main(){
int a[10],i,g,n,m,c=0;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m)
{
printf("The number is found in the location: a[%d]\n ",i,i+1);
}
else printf("");
};
printf("\n");
for(i=0;i<=n-1;i++){
if(a[i]==m)
{
g=1;
}
else g=0;
};
if(g==0)
{
printf("The number is not found in any location\n");
}
else printf("");
return 0;
}
## Solution for problem 9 :
#include<stdio.h>
int main()
{
int b,n,c,d,x;
printf("enter the number of array\n");
scanf("%d",&n);
int a[n];
for( c=0;c<n;c++ )
{
printf("a[%d]=",c);
scanf("%d",&a[c]);
};
printf("your entered values are:\n\n");
for( c=0;c<n;c++ )
{
printf("a[%d]=%d \n",c,a[c]);
};
printf("\n\n");
printf("enter the value of x for a[x], for which you want to update the value.\n\n");
scanf("%d",&x);
for( d=n-1;d>=x;d-- )
{
a[d]=a[d-1];
};
printf("\nnew value of a[%d]=",x);
scanf("%d",&a[x]);
printf("\nafter change,the values are:\n");
for( c=0;c<n;c++ )
{
printf("a[%d]=%d \n",c,a[c]);
};
printf("\n");
return 0;
}
#include<stdio.h>
int main()
{
int b,n,c,d,x;
printf("enter the number of array\n");
scanf("%d",&n);
int a[n];
for( c=0;c<n;c++ )
{
printf("a[%d]=",c);
scanf("%d",&a[c]);
};
printf("your entered values are:\n\n");
for( c=0;c<n;c++ )
{
printf("a[%d]=%d \n",c,a[c]);
};
printf("\n\n");
printf("enter the value of x for a[x], for which you want to update the value.\n\n");
scanf("%d",&x);
for( d=n-1;d>=x;d-- )
{
a[d]=a[d-1];
};
printf("\nnew value of a[%d]=",x);
scanf("%d",&a[x]);
printf("\nafter change,the values are:\n");
for( c=0;c<n;c++ )
{
printf("a[%d]=%d \n",c,a[c]);
};
printf("\n");
return 0;
}
## Solution for problem 10 :
#include<stdio.h>
int main()
{
printf("Enter the string, whose length you want to see.\n\n");
char str[]="\n";
scanf("%s",str);
int x;
x=strlen(str);
printf("\nThe length of the string is: %d\n\n",x);
return 0;
}
#include<stdio.h>
int main()
{
printf("Enter the string, whose length you want to see.\n\n");
char str[]="\n";
scanf("%s",str);
int x;
x=strlen(str);
printf("\nThe length of the string is: %d\n\n",x);
return 0;
}
For more C problems