AB, V PK: Solutions For C Program
AB, V PK: Solutions For C Program
GENERAL PROGRAMS:
Number divisible by 2 and not by 3 and 5.
#include<stdio.h> int main() { int i; printf("The numbers are"); for(i=1;i<=100;i++) if((i%3!=0)&&(i%5!=0)) { if(i%2==0) { printf("%d ",i); } } return 0; }
Number of words
#include<stdio.h> #include<string.h> int main() { char s1[50]; int word=0,i=0; printf("Enter the string\n"); gets(s1); while(s1[i]!='\0') { if(s1[i]==' ') { word++; } i++; } printf("The number of words %d",word+1);
A B, V
PK
return 0; }
CONCATENATION
#include<stdio.h> int main() { int i,j,k=0; printf("Enter s1: "); gets(s1); printf("Enter s2: "); gets(s2); for(i=0;s1[i]!='\0';i++)
char s1[50],s2[50],s3[50];
A B, V
PK
{ s3[i]=s1[i]; } for(j=i;s2[k]!='\0';j++) { s3[j]=s2[k]; k++; } printf("\nThe concatenation of the given string: %s",s3); return 0; }
int m,a[50],r,b,i;
printf("Enter the number of array elements\n"); scanf("%d",&m); for(i=0;i<m;i++) scanf("%d",&a[i]); printf("Enter the array elements\n");
printf("Enter the element to be searched in the array:\n"); scanf("%d",&b); do{ printf("position is "); for(i=0;i<m;i++) { if(b==a[i])
A B, V
PK
printf("%d------%c\n",a,a);
A B, V
PK
scanf("%d",&a); n=a%10; if(n==5) {printf("The square of the given number is %d",(a*a));} else printf("This %d number doesn't have least significant digit as 5",a); return 0; }
A B, V
PK
f=n/100; n=n%100; printf("%s hundred and",s[f-1]); } if(n>10&&n<20) { f=n%10; printf("%s ",t[f-1]); } if(n>19 && n<=99) { f=n/10; n=n%10; printf("%s ",u[f-1]); } if(n>=1&&n<=9) { printf("%s",s[n-1]); } } return 0; }
Factors of Integers.
#include<stdio.h> int main() { int n,i,rem;
A B, V
PK
NCR Value
A B, V
PK
for(i=c-1;i>=0;i--)
for(i=c-1;i>=0;i--)
A B, V
PK
return 0; }
int main() { int n,base,dec,rem; dec=0;base=1; printf("Enter the binary number:"); scanf("%d",&n); while(n>0) { rem=n%10; if(rem==1)
printf("The decimal equivalent is %d\n",dec); printf("The octal equivalent is %o\n",dec); printf("The hexadecimal equivalent is %x",dec); return 0; }
A B, V
PK
#include<stdio.h>
{ int n,a,base,dec,rem; dec=0;base=1; printf("Enter the decimal number:"); scanf("%d",&n); a=n; while(n>0) { rem=n%2; if(rem==1) {dec=dec+base;} base=base*10; n=n/2; } printf("The binary equivalent is %d\n",dec); printf("The octal equivalent is %o\n",a); printf("The hexadecimal equivalent is %x",a); return 0; }
Hexadecimal to Binary
#include<stdio.h> int main() { dec=0;base=1; scanf("%x",&n);
int n,a,base,dec,rem;
A B, V
PK
while(n>0) { rem=n%2; if(rem==1) {dec=dec+base;} base=base*10; n=n/2; } printf("The binary equivalent is %d\n",dec); return 0; }
MATRIX PROGRAMS:
Matrix Multiplication.
#include<stdio.h> int main() { int r1,c1,i,j,k,r2,c2,a[50][50],b[50][50],c[50][50]; printf("Enter the size of the matrix a\n"); scanf("%d%d",&r1,&c1); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) } printf("Enter the size of b\n"); scanf("%d%d",&r2,&c2); printf("Enter the elements of a\n");
scanf("%d",&a[i][j]);
A B, V
PK
if(c1!=r2) printf("matrix multiplication is not possible"); else { printf("Enter the elements of b\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&b[i][j]); } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0;
for(k=0;k<c1;k++) } }
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("after multiplication the matrix is\n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf("%d\t",c[i][j]);} printf("\n"); } } return 0;}
A B, V
PK
Matrix Addition.
#include<stdio.h> int main() { int r,c1,i,j,k,a[50][50],b[50][50],c[50][50]; printf("Enter the size of the matrix a\n"); scanf("%d%d",&r,&c1); printf("Enter the elements of a\n"); for(i=0;i<r;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); } printf("Enter the elements of b\n"); for(i=0;i<r;i++) { for(j=0;j<c1;j++) scanf("%d",&b[i][j]); } c[i][j]=0; for(i=0;i<r;i++) {
c[i][j]=a[i][j]+b[i][j];
{printf("%d ",c[i][j]);}
A B, V
PK
A B, V
PK
for(i=0;i<r;i++)
printf("\n");} return 0; }
A B, V
PK
else printf("not a lower triangular matrix"); return 0;} Upper triangular matrix is similar to this
A B, V
PK
for(j=0;j<c1;j++) } { {
scanf("%d",&b[i][j]); for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
a[i][j]=a[i][j]-b[i][j]; b[i][j]=a[i][j]+b[i][j];
A B, V
PK
for(j=0;j<c1;j++)
a[i][j]=b[i][j]-a[i][j]; } } printf("the elements of a\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%d\t",a[i][j]); } printf("\n"); } for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { } } return 0; } printf("%d\t",b[i][j]); printf("\n");
A B, V
PK
scanf("%d%d",&r,&c); printf("Enter the elements of a\n"); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) scanf("%d",&a[i][j]); } for(i=1;i<=r;i++) { for(j=1;j<=c;j++) {sum=sum+a[i][j];} sum=0; } { printf("\nThe sum of %d row is %d",i,sum);
for(j=1;j<=c;j++) for(i=1;i<=r;i++)
EASY PROGRAMS:
Celsius -> Farenheit, Farenheit-> Celsius.
#include<stdio.h> int main(void) { float c,f; printf("celsius");
A B, V
PK
sum=0;
A B, V
PK
A B, V
PK
A B, V
PK
for(i=0;i<n;i++)
Armstrong number
#include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num!=0) { r=num%10; sum=sum+(r*r*r); } if(sum==temp) else return 0; } num=num/10;
A B, V
PK
A B, V
PK
if(i%2==0) { sum=sum+A[i]; } else { z=z+A[i]; } } printf("\nThe sum of elements in the even position is %d",sum); printf("\nThe sum of elements in the odd position is %d",z); return 0; }
printf("enter number up to which prime number is to be found: "); scanf("%d",&a); for(c=0;a>0;a--) {n=a; for(c=0;n>0;n--) { z=(a%n); if (z==0) c++; printf("prime numbers are: ");
A B, V
PK
A B, V
PK
{ int i,j=0; char a[100]; printf("Enter:\n"); gets(a); for(i=0;a[i]!='\0';i++) j++; for(i=0;i<j;i++) { if(a[i]==a[j-(i+1)]) { if(i==j/2) printf("yes"); continue; } else {printf("no"); break;} } }
Fibonacci
A B, V
PK
return 0;} void fib(int n) { int f,s,z,i; f=0;s=1; printf("The fibonacci series is %d %d ",f,s); for(i=0;i<n-2;i++) { z=f+s; printf("%d ",z); f=s; s=z;} }
Pascals Triangle.
#include<stdio.h> int fact(int); int main() { int i,k,n;
printf("Enter the number of rows to print: "); scanf("%d",&n); { for(i=0; i<n; i++)
printf("%d ",(fact(i))/(fact(k)*fact(i-k)));
A B, V
PK
Floyds Triangle.
#include<stdio.h> int main() { int a=1,i,j,n;
printf("Enter the number of rows to print: "); scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<=i;j++) { printf("%d ",a); a++; } printf("\n"); } return 0; }
A B, V
PK
strcpy(s[j-1],s[j]);
A B, V
PK
A B, V
PK
PROGRAMS:
( for which output is given) 1 1 2
printf("Enter the number of rows to print: "); for(i=0;i<n;i++) { for(k=0;k<=(n-i-2);k++) printf(" "); for(k=0;k<=i;k++) printf("%d ",k+1); printf("\n"); }return 0; }
A B, V
5 5 5
PK
scanf("%d",&n);
#include<stdio.h> int main() { int i,k,n,m; printf("Enter the number of rows to print:"); scanf("%d",&n); printf("Enter the element to be printed on that triangle:"); scanf("%d",&m); for(i=0;i<n;i++) { for(k=0;k<=(n-i-2);k++) printf(" "); for(k=0;k<=i;k++) printf("%d ",m); printf("\n"); }
Some codes are not listed here. Sorry for that. VPK-Deepak VPK AB-Ajay Balaji
A B, V
PK
}return 0;