Write a C-prog. for Addition of two 4x4 Matrix


#include <stdio.h>
#include <conio.h>
void main()
{
 int a[4][4],b[4][4],i,j;
 clrscr();
 printf("1st Matrix");
 for(i=0;i<=3;i++)
  {

   for(j=0;j<=3;j++)
    scanf("%d",&a[i][j]);
   printf("\n");
  }
printf("2nd  Matrix");
for(i=0;i<=3;i++)
 {
  for(j=0;j<=3;j++)
   scanf("%d",&b[i][j]);
  printf("\n");
 }
printf("Print 1st Matrix = \n");
for(i=0;i<=3;i++)
 {
  for(j=0;j<=3;j++)
     printf("%d ",a[i][j]);
     printf("\n");
 }
printf("Print 2nd Matrix = \n");
for(i=0;i<=3;i++)
 {
  for(j=0;j<=3;j++)
    printf("%d ",b[i][j]);
    printf("\n");

 }
printf("\n addition of 1st and 2nd matrix = \n");
 for(i=0;i<=3;i++)
 {
  for(j=0;j<=3;j++)
   printf("%d ",a[i][j]+b[i][j]);
   printf("\n");
 }
getch();
}

Write a C-prog. to Accept a 4*4 Matrix and print Transpose

#include <stdio.h>
#include <conio.h>
void main()
{
 int a[4][4],i,j,r,c;
 clrscr();

for(i=0;i<=3;i++)
   for(j=0;j<=3;j++)
      scanf("%d",&a[i][j]);
printf("\n Original Matrix \n\n");
for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
      printf("%d ",a[i][j]);
   printf("\n");

 }
printf("\nTranspose of matrix\n");
for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
    printf("%d ",a[j][i]);
   printf("\n");

 }
getch();
}

Accept a 5*5 Matrix and find maximum number

#include <stdio.h>
#include <conio.h>
void main()
{
 int a[5][5],i,j,max=0;
 clrscr();
 printf("Enter any 5x5 matrix= ");
 for(i=0;i<=4;i++)
  {
   for(j=0;j<=4;j++)
    scanf("%d",&a[i][j]);
   printf("\n");
  }
max=a[0][0];
for(i=0;i<=4;i++)
   for(j=i+1;j<=4;j++)
    if(max<a[i][j])
     max=a[i][j];
printf("Max. value in Matrix is = %d",max);
getch();
}

Write a Program to print Prime Numbers between 1 to 100

#include <stdio.h>
#include <conio.h>
void main()
{
  int a[100],i,j=2;
  clrscr();
  for(i=0;i<100;i++)
   a[i]=i+1;
printf("Prime Number between 1 to 100 \n");
  for(i=0;i<100;i++)
   {
    for(j=2;a[i]>j;j++)
      {
     if(a[i]%j==0)
     break;
      }
     if(a[i]==j || a[i]==1)
       printf("%d\t",a[i]);
    }
getch();
}

Accept a String and Convert into Proper Case

#include<stdio.h>
#include<conio.h>
void incap(char s[]);
void main()
{
 char ch[20];
 clrscr();
 puts("Enter any string ");
 gets(ch);
 incap(ch);
 getch();
}
void incap(char s[])
{
 int i,j;
 for(i=0;s[i]!='\0';i++)
  {
   if(s[i]==' '&& s[i+1]!=' ')
      s[i+1]=s[i+1]-32;
    else if(i==0)
      s[i]=s[i]-32;
   }
 puts(s);
}

Write a Program to get char and line count in File

//File char count and line count

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fp;
 char ch;
 int line=1,c=1;
 clrscr();
 fp=fopen("C:\\abc.txt","r");
  if(fp==NULL)
   {
    puts("File not found");
    exit(0);
   }
 while(1)
  {
   ch=fgetc(fp);
   if(ch==EOF)
    break;
    c++;
    if(ch=='\n')
      line++;

     }
       fclose(fp);
        printf("Total characters =%d\n",c);
        printf("line count =%d",line);


getch();
}

Addition of 2 Numbers in C++

#include<iostream.h>
#include<conio.h>

void main()
{
    int i,j,add;
    clrscr();
    cout<<"Enter 1st Number";
    cin>>i;
    cout<<"Enter 2nd Number";
    cin>>j;
    add=i+j;
    cout<< "Addition  ="<<add;
    getch();


}

What will be the output ?

void main()
{
    char ch[40]={'p','g','\0','d','c','a'};
    int len=strlen(ch);
    printf("string =%s and length of the string =%d",ch,len);
  
}


Output : string =pg and length of the string =2
Explanation: String ends with  null character.


 void main()
{
    static int a[19];
    clrscr();
    printf("%d",sizeof(a));
    return;
}
Output: 38
Explanation: size of int a[19] =19*2=38

Write a C++ Program to Convert Binary into Decimal Number

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
  {
  int a[10],i,no,sum=0,rem,j,k=0;
  clrscr();
  cout<<"Enter Binary No:";
  cin>>no;
  i=0;
  while(no>0)
  {
  a[i]=no%10;
  no=no/10;
  i++;
  }
  for(j=0;j<i;j++)
  {
      sum=sum+a[j]*pow(2,k);
      k++;
  }
  cout<<"Decimal NO:"<<sum;
  getch();
  }

Write a Program to print reverse of a given number

#include<stdio.h>
#include<conio.h>

void main()
{
int no,rem,rev=0;
clrscr();
printf("Enter a no:");
scanf("%d",&no);
while(no>0)
{
rem=no%10;
rev=(rem*10)+rev;
no=no/10;
}
printf("Reversed number =%d",rev);
getch();
}

Write a Program to print Product of Digits (eg. 212=2*1*2=4) of a given number

#include<stdio.h>
#include<conio.h>

void main()
{
int no,r,pt=1;
clrscr();
printf("Enter a no:");
scanf("%d",&no);
while(no>0)
{
r=no%10;
pt=pt*r;
no=no/10;
}
printf("Product of Digits= %d",pt);
getch();
}

Array 5 - Write a program to sort 10 Array elements in descending order

# include<stdio.h>
# include<conio.h>


void main()
{
int arr[10];
int i,j,temp;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
for(i=0;i<9;i++)
{
  for(j=i+1;j<10;j++)
  {
   if(arr[i]<arr[j])
   {
   temp=arr[i];
   arr[i]=arr[j];
   arr[j]=temp;
   }
}
}
printf("\nSorted array elements :\n");
for(i=0;i<10;i++)
printf("%d ",arr[i]) ;
getch();
}

Array 4 -Write a program to reverse print an array

# include<stdio.h>
# include<conio.h>

void main()
{
int arr[10];
int i,j,temp;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("\nPrinting an sorted array\n");
for(i=9;i>=0;i--)
printf("%d ",arr[i]) ;
getch();
}

Array 3 -Write a Program to print alternate element of an array

# include<stdio.h>
# include<conio.h>

void main()
{
int arr[10];
int i,j,temp;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
for(i=0;i<10;i+=2)
printf("%d ",arr[i]) ;
getch();
}

Array 2- Accept 10 numbers and no. to search, print its occurence

/* Write a program to accept 10 no’s  and a number to search and print how many times it occurs in array */
 
# include<stdio.h>
# include<conio.h>

 
void main()
{
int arr[10];
int i,cnt=0,tosearch;
clrscr();
/* accepting an array*/
for(i=0;i<10;i++)
  {
   printf("\n Enter %d number-",i);
   scanf("%d",&arr[i]);
   }
printf("\nEnter a number to be search-");
scanf("%d",&tosearch);
for(i=0;i<10;i++)
{
 if(arr[i]==tosearch)
   cnt++;
}
printf("\n Number occurs %d times",cnt);
getch();

Array 1- Accept 10 numbers from user and display sum and average .

# include<stdio.h>
# include<conio.h>
void main()
{
int arr[10];
int i,sum=0,avg=0;
clrscr();
for(i=0;i<10;i++)
 {
  scanf("%d",&arr[i]);
  sum = sum + arr[i];
 }
printf("\n Sum of array elements = %d",sum);
avg = sum/10;
printf("\n Average = %d",avg);
getch();
}

Write a function occur(string, char) that returns the number of occurrences of that character in the string.

#include<stdio.h>
#include<conio.h>
int strchr(char [],char );
void main()
{
char a[80],b;
int i;
clrscr();
printf("Enter 1st string\n");
gets(a);
printf("Enter character to find occurence\n");
fflush(stdin);
scanf("%c",&b);
i=strchr(a,b);
if(i!=0)
printf("The character occured at %d position\n",i);
printf(" Sorry! the character is not present in the string");
getch();
}
 int strchr(char a[],char y)
 {
 int i,j;
 for(i=0;a[i]!=y&&a[i];i++);
  if(a[i]=='\0')
  return (0);
  return(i);

 }

Write a function that accepts a string and converts it into lowercase

#include<stdio.h>
#include<conio.h>

void strlwr(char[]);
void main()
{
char a[80];
clrscr();
printf("Enter any string\n");
gets(a);
printf("String in lower case\n");
strlwr(a);
getch();
}
void strlwr(char a[])
{

int i;
for(i=0;a[i];i++)

if(a[i]>='A'&&a[i]<='Z')
 a[i]=a[i]+32;
 else
 a[i]=a[i];


printf("%s",a);
}

Write a function that accepts 2 strings and compares them.

#include<stdio.h>
#include<conio.h>

int strcmp(char [],char []);
void main()
{
int i;
char a[80],b[80];
clrscr();
printf("Enter 1st string\n");
gets(a);
printf("Enter string to compare\n");
fflush(stdin);
gets(b);
i=strcmp(a,b);
printf("%d",i);
if(i==0)
printf("Strings are same\n");
else
printf("\nNot same");

getch();
}
 int strcmp(char x[],char y[])
 {
 int j;
 for(j=0;x[j]&&y[j];j++)
 {
 if(x[j]!=y[j])
 return(x[j]-y[j]);
 }
 return(0);
}

Accept a String , start, end positions and remove substring from it

#include<stdio.h>
#include<conio.h>
#include<string.h>


void remove(char[],int,int);
 main()
{
 char a[80];
 int s,e;
 clrscr();
 printf("Enter a string\n");
 gets(a);
printf("Enter start\n");
fflush(stdin);
scanf("%d",&s);
printf("Enter end\n");
scanf("%d",&e);
 remove(a,s,e);
getch();
return 0;
}
void remove(char a[],int s,int e)
{
int i;
while(a[i]!='\0')
{
 for(i=0;i<s&&a[i];i++)
   printf("%c",a[i]);
 for(i=s;i<=e&&a[i];i++);
 for(;a[i]!='\0';i++)
   printf("%c",a[i]);

  }
}

Accept a String and get Substring from it at specific position

#include<stdio.h>
#include<conio.h>
#include<string.h>

void substr(char [],int,int,char []);
void main()
{
 int s,e;
 char a[80],b[80];
 clrscr();
 printf("Enter any string\n");
 gets(a);
 printf("Enter start\n");
 fflush(stdin);
 scanf("%d",&s);
 printf("Enter end\n");
 fflush(stdin);
 scanf("%d",&e);
 substr(a,s,e,b);
 getch();
 }
void substr(char a[],int x,int y,char b[])
{
int i,j=0;
for(i=x;i<=y&&a[i]!='\0';i++)
{ b[j]=a[i];
 j++;}
 b[j]='\0';
 printf("%s",b);
 }



Write a function that accepts a string and returns the number of words in it.

//Accept a String and display word count

#include<stdio.h>
#include<conio.h>
#include<string.h>

int count_words(char []);
void main()
{
int i;
char a[80];
clrscr();
printf("Enter any string\n");
gets(a);
i=count_words(a);
printf("The no of words is %d",i);
getch();
}
int count_words(char a[])
{
int wcnt=0,i=0;
while(a[i])
{
for(;a[i]==' ';i++);
if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
wcnt++;
for(;a[i]!=' '&&a[i];i++);
}
return(wcnt);

}

Write a function ustrcat(s1, s2, i) that concatenates contents of string s2 at the i position of string s1.

// Accept 2 string and insert 2nd string into first at given position

#include<stdio.h>
#include<conio.h>
#include<string.h>

void insert(char [],char [],int);
void main()
{
char a[80],b[80];
int i;
clrscr();
printf("Enter 1st string\n");
gets(a);
printf("Enter 2nd string\n");
fflush(stdin);
gets(b);
printf("Enter the position\n");
scanf("%d",&i);
insert(a,b,i);
getch();
}
void insert(char x[],char y[],int j)
{
int l1,l2,k;
l1=strlen(x);
l2=strlen(y);
 k=l1+l2;
for(;l1>=j;)
x[k--]=x[l1--];

for(k=0;y[k];)
x[j++]=y[k++];
printf("%s",x);
}





Accept a String and Convert it into Proper Case

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char a[80];
int i=0,j;
clrscr();
printf("Enter a string\n");
gets(a);
while(a[i]!='\0')
{
for(;a[i]==' ';i++);
{
 if(a[i]>='a'&&a[i]<='z')
   {
   a[i]-=32;
   }
 }
  for(;a[i]!=' '&&a[i]!='\0';i++)
  a[i]=a[i];
}
printf("%s",a);
getch();
}

Accept a String and remove extra spaces from it

#include <stdio.h>
#include <conio.h>
#include<string.h>

void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
 for(;a[i]==' ';i++);
 {
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c",a[i]);}
getch();
}

Accept 2 String and display common chars in them.

#include <stdio.h>
#include <conio.h>
#include<string.h>

void main()
{
char a[80],b[80];
int i,j,t;
clrscr();
printf("Enter 1st string\n");
gets(a);
fflush(stdin);
printf("Enter 2nd string\n");
gets(b);
printf("Common characters are :\n");
 for(i=0;a[i]!='\0';i++)
 {
  for(j=i-1;j>=0;j--)

   if(a[i]==a[j])
   break;
   if(j==-1)
   for(t=0;b[t]!='\0';t++)

     if(a[i]==b[t])
    {
     printf("%c",a[i]);
     break;
    }
  }

 getch();
}

Write a C-prog to accept a String and print reverse of it


#include<stdio.h>
#include<conio.h>
#include<string.h>
 
void main()
{
 char a[80];
 int i,j;
 clrscr();
 printf("Enter a string\n");
 gets(a);
 for(i=0;a[i]!='\0';i++);
for(--i;i>=0;i--)
 printf("%c",a[i]);
 getch();
}

Write a C-prog to Accept a String and print vowel count.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80];
int i=0,j,vcnt=0;
clrscr();
printf("Enter a string\n");
gets(a);
while(a[i]!='\0')
{
  if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
  { vcnt++;
  }
 i++;
}
printf("\nvowel count=%d",vcnt);

getch();
}

Write a Program to accept days & convert it into equivalent no. of years, months, weeks & remaining days.

#include<stdio.h>
#include<conio.h>
 
void  main( )
{
  int  days ,yr,mn,wk,d;
printf("Enter the no of days");
scanf("%d",&days);
yr = days /365;
mn =(days /365)/30;
wk = (((days/365)%30)/7);
d =(((days/365)%30)%7);
printf("Years= %d \t Months=  %d \t Weeks =%d \t days = %d",yr,mn,wk,d);
getch();
 }

Write a C- program to display sum of no. from 1 to 10

#include<stdio.h>
#include<conio.h>
void main( )
{
int  no ,sum=0;
for(no=1;no<=10;no++)
{
sum = sum+no;
}
printf("Total = %d",sum);
getch();
}

Write a C program to display Armstrong nos between 1 to 500

#include <stdio.h>
#include <conio.h>
void main()
{
int n=1,dum,sum=0,r;
clrscr();
printf("Armstrong nos between 1 to 500/n");
while (n<=500)
{
dum=n;
while (dum>0)
{
r=dum%10;
sum=sum+(r*r*r);
dum=dum/10;
 }
if(sum==n)
  printf("\n\n%d",sum);
  n=n+1;
  sum=0;
}
 getch();

Write a Program to display all ASCII characters

#include <stdio.h>
#include <conio.h>

void main()
{
int i=0;
clrscr();
while(i<255)
{
 printf("%d\t%c",i,i);
 i=i+1;
}
getch();
}

Write a Program to find a power of give no.

#include <stdio.h>
#include <conio.h>

void main()
{
int i,base,index,power=1;
clrscr();
printf("Enter the base and index");
scanf("%d%d",&base,&index);
for(i=0;i<index;i=i+1)
{
  power=power*base;

}
printf("%d",power);
getch();
}

Write a Program to accept a no & find the Factorial

#include <stdio.h>
#include <conio.h>
void main()
{
int fact=1,no;
clrscr();
printf("Enter the Number:-");
scanf("%d",&no);
ram:
fact=fact*no;
no=no-1;
if(no>1)
 goto ram;
 else
 printf(" Factorial of a no=%d",fact);
 getch();
 }

Write a Program that accept a number and check if palindorme or not

#include <stdio.h>
#include <conio.h>
void main()
{
int no,rev;
clrscr();
printf("Enter the number:-");
scanf("%d",&no);
ent:
rev=no%10;
no=no/10;
if (no>0)
goto ent;
if (no==rev)
printf("%d is equal to reverse no",no);
else
printf("%d is not equal to reverse no",no);
getch();
}

Write a Program to accept a no. & find sum of digit

#include <stdio.h>
#include <conio.h>
void main()
{
int sum=0,no;
clrscr();
printf("Enter upto four digit number");
scanf("%d",&no);
 sum=0;
plus:
sum=sum+no%10;
no=no/10;

if (no>0)
goto plus;
else
printf("sum of digits is %d",sum);
getch();
}

Character handling

Write a Program to accept a character & find whether capital/small case,digit/special case letter

#include <stdio.h>
#include <conio.h>

void main()
{
char ch;
clrscr();
printf("enter any character");
scanf("%c",&ch);
if((ch >='A')&&(ch<='Z'))
  printf("capital case letter");
else if((ch>='a')&&(ch<='z'))
  printf("lower case letter");
else if((ch>='1')&&(ch<='9'))
  printf("digit");
else
  printf("Special symbol");
getch();
}

Write a Program that accept a character & find whether it is a vowel

#include <stdio.h>
#include <conio.h>
void main ()
{
char ch;
clrscr();
printf("enter any character:- ");
scanf("%c",&ch);
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
{
printf("this is a vowel");
}
else
{
printf("this is not a vowel");
}
getch();
}

Write a Program that accepts a no & find if odd or even

#include<stdio.h>
#include<conio.h>
void main()
{
  int no;
  clrscr();
  printf("Enter a number");
  scanf("%d",&no);
  if(no%2==0)
  {
    printf("Even number");
  }
  else
  {
    printf("Odd number");
  }
  getch();
}

Calculate Area, Perimeter of a Rectangle

Write a Program that accept length,breadth and radius of a rectangle and circle and
display Area,perimeter of a rectangle and circumference of circle

#include <stdio.h>
#include <conio.h>

void main()
{
int a1,p,a2,c,l,b,r;
clrscr();
printf("Enter length,breadth and radius");
scanf("%d%d%d",&l,&b,&r);
a1=l*b;
p=2*(l+b);             
a2=3.14*r*r;
c=2*3.14*r;
printf("\nArea and Perimeter of a rectangle is %d and %d",a1,p);
printf("\nArea and circumference of a circle is %d and %d",a2,c);
getch();
}

Write a Program that accept temperature in Fahrenheit and display into Celsius

#include <stdio.h>
#include <conio.h>

void main()
{
float faren,deg;
clrscr();
printf("Enter Temperature in Fahrenheit");
scanf("%f",&faren);
deg=(faren-32)/1.8;
printf("This is your Temp in degree: ");
printf("%f",deg);
getch();
}

Related Post

Array:
String functions:
General (Loops-for/while)
Write a Program to print reverse of a given number Write a Program to print Product of Digits (eg. 212=2*1*2=4) of a given number Write a C++ Program to Convert Binary into Decimal Number