Professional Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,num,i,c=0,arm=0;
int ar[100];
for ( i=0;i<100;i++ )
{
ar[i]=0;
}
printf("\n Enter the number: ");
scanf("%d",&n);
num=n;
while ( num>0 )
{
ar[c]=num%10;
c++;
num/=10;
}
for ( i=1;i<c;i++ )
{
arm+=pow(ar[i],3);
}
printf("\n The number %d is",n);
if ( n==arm )
printf(" armstrong number");
else
printf(" not an armstrong number");
getch();
}
------------------------int temp,n,r,sum=0;
n=153;
temp=153;
while(n!=0)
{
r=n%10;
sum=sum+i*i*i;
}
if(sum==temp)
{
printf("Armstorong");
}
else
{
printf("Not");
}
================================================================================
/* Description:To multiply given to matrix using arrays*/
#include<stdio.h>
#include<math.h>
main()
{
int i,j,k;
int a,b,c;/*three variables to get the row and colum of the two matrix*/
int sum;
int m[10][10],n[10][10],l[10][10];
printf("The first matrix:\n");
printf("Enter the number of rows and columns for the first matrix:\t") ;
scanf("%d%d",&a,&b);
printf("The Second matrix:\n");
printf(Enter the rows and columns of the second matrix:\t:);
scanf("%d%d",&b,&c);
/* Note: For matrix multiplication the number of columns in the first matri
x should be equal to the number of rows in the second message*/
printf(":Enter the values of the first matrix:\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
scanf("%d",&m[i][j]);
}
}
printf("Enter the values of the second matrix:\n");
for(i=0;i<b;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&n[i][j]);
}
}
for(i=0;i<a;i++)
{
for(j=0;j<c;j++)
{
sum=0;
for(k=0;k<b;k++)
{
sum=sum+(m[i][k]*n[k][j]);
l[i][j]=sum;
}
}
}
printf("The multiplied matrix is:\n);
for(i=0;i<a;i++)
{
for(j=0;j<c;j++)
{
printf ("%d",l[i][j]);
printf("\t");
}
printf("\n");
}
================================================================================
Write a c program to find out sum of digit of given number
Code
1. C
2. C
3. C
1:
program to add digits of a number
program for sum of digits of a number
program to calculate sum of digits
#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
Sample output:
Enter a number: 123
#include<stdio.h>
int main(){
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);
return 0;
}
Sample output:
Enter the row and column of matrix: 3 3
Enter the elements of matrix: 2
3
5
6
7
9
2
6
7
The matrix is
2
3
5
6
7
9
2
6
7
Sum of the diagonal elements of a matrix is: 16
================================================================================
Program in c language to find prime factors of a given number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter a Number:");
scanf("%d",&n);
printf("\n\nPrime Factors of %d is: ",n);
for(i=2;i<=n;i++)
{
if(n%i==0)
{
printf("%d,",i);
n=n/i;
i--;
if(n==1)
break;
}
}
getche();
}
================================================================================
LCM & GCD
int gcd(int a, int b)
{
if (a==0 && b==0) return -1;
if (b==0) return a;
return gcd(b,a%b);
}
int lcm (int a, int b)
{
return (int) (a*b)/gcd(a,b);
}
================================================================================
C program to find transpose of given matrix
#include<stdio.h>
int main(){
int a[10][10],b[10][10],i,j,k=0,m,n;
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
================================================================================
CONCATENATION OF TWO STRINGS USING C PROGRAM
#include<stdio.h>
int main(){
int i=0,j=0;
char str1[20],str2[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
printf("After concatenation the strings are\n");
puts(str1);
return 0;
}
================================================================================
Write a c program to check given string is palindrome number or not
#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}
#include<stdio.h>
int main(){
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before swaping the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
temp[j++]=str1[i++];
}
temp[j]='\0';
i=0,j=0;
while(str2[i]!='\0'){
str1[j++]=str2[i++];
}
str1[j]='\0';
i=0,j=0;
while(temp[i]!='\0'){
str2[j++]=temp[i++];
}
str2[j]='\0';
printf("After swaping the strings are\n");
puts(str1);
puts(str2);
return 0;
}
================================================================================
How to compare two strings in c without using strcmp
#include<stdio.h>
int stringCompare(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
scanf("%s",str1);
printf("Enter second string: ");
scanf("%s",str2);
compare = stringCompare(str1,str2);
if(compare == 1)
printf("Both strings are equal.");
else
printf("Both strings are not equal");
return 0;
}
int stringCompare(char str1[],char str2[]){
int i=0,flag=0;
while(str1[i]!='\0' && str2[i]!='\0'){
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if (flag==0 && str1[i]=='\0' && str2[i]=='\0')
return 1;
else
return 0;
}
Sample output:
Enter first string: cquestionbank.blogspot.com
Enter second string: cquestionbank.blogspot.com
Both strings are equal.
================================================================================
CONVERSION FROM UPPERCASE TO LOWER CASE USING C PROGRAM
#include<stdio.h>
#include<string.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in uppercase is->%s",str);
return 0;
}
================================================================================
COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
#include<stdio.h>
int main(){
FILE *p,*q;
char file1[20],file2[20];
char ch;
printf("\nEnter the source file name to be copied:");
gets(file1);
p=fopen(file1,"r");
if(p==NULL){
printf("cannot open %s",file1);
exit(0);
}
printf("\nEnter the destination file name:");
gets(file2);
q=fopen(file2,"w");
if(q==NULL){
printf("cannot open %s",file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf("\nCOMPLETED");
fclose(p);
fclose(q);
return 0;
}
================================================================================
================================================================================