Write a ‘C’ Program to calculate sum of elements of a mXn matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,m,n,sum=0;
clrscr();
printf("\nEnter the 3*3 matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+a[i][j];
}
}
printf("\nsum of elements of matrix is=%d",sum);
getch();
}
/* Output
Enter the 3*3 matrix=
1 2 3
4 5 6
7 8 9
sum of elements of matrix is=45 */

Write a C program to calculate sum of Fibonacci series up to a given number.


#include<stdio.h>
#include<conio.h>
void main()
 {
 int n,a=0,b=1,i,c;
 clrscr();
 printf("\n enter two many term u want");
 scanf("\n %d",&n);
 printf("\n %d",a);
 printf("\n %d",b);
 for(i=1;i<=n;i++)
  {
  c=a+b;
  printf("\n %d",c);
  a=b;
  b=c;
  }
  getch();
 }
/* output

 enter two many term u want5

 0
 1
 1
 2
 3
 5
 8 */

Write a ‘C’ Program to accept ‘n’ numbers from user, store these numbers into an array and count the number of occurrence of each number.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,cnt,a[10],temp,no;
clrscr();
printf("\nenter the limit = ");
scanf("%d",&n);
printf("\n Enter the %d number",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i=j)
{
no=a[i];
cnt=1;
for(j=i+1;j<n;j++)
{
if(a[j]!=no)
break;
else
cnt++;
}
printf("\n%d no occurs %d times\n ",no,cnt);
}
getch();
}
/* output

enter the limit = 5

 Enter the 5 number
11
56
89
22
11

11 no occurs 2 times
22 no occurs 1 times
56 no occurs 1 times
89 no occurs 1 times */

Write a C program to calculate x(y+z) by using user defined function


#include<stdio.h>
void power(int x,int y, int z);
void main()
{
 int x,y,z;
 printf("Enter value of x: ");
 scanf("%d",&x);
 printf("Enter value of y: ");
 scanf("%d",&y);
 printf("Enter value of z: ");
 scanf("%d",&z);
 power(x,y,z);
}
void power(int x,int y, int z)
{
 int ans = 1, i;
 for(i = 1; i <= (y+z); i++)
  ans *= x;
 printf("%d ^ (%d+%d) = %d",x,y,z,ans);
}

/* output
Enter value of x: 1
Enter value of y: 2
Enter value of z: 3
1 ^ (2+3) = 1 */

write a menu driven prodram in 'c' which performs the following ,operations on string . write separate function for each option.


program name:- write a menu driven prodram in 'c' which performs the following ,operations on string . write separate function for each option,
               -cheak if one string is substring of another string ,
               -count number of occurrences of acharacter in the         string,
               -Exit.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int ch;
char str1[30],str2[30],occ;
int i=0,cnt=0;
clrscr();
while(1)
{
printf("\n menues");
printf("\n1. check if one string is substring of another string");
printf("\n2. count number of occurrences of characters in the string");
printf("\n3.exit");
printf("\nEnter Your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n enter the first string");
               scanf("%s",str1);
               printf("\n enter the second string");
               scanf("%s",str2);
               if(strstr(str1,str2)==NULL)
                        {
                         printf("\n  second string is not substring of first string");
                         }
                         else
                         {
                                    printf("\n  second string is  substring of first string");

                          }

               break;
case 2:
                        printf("\n enter the string");
                        scanf("%s",&str1);
               printf("\n enter the  character to find occurences");
               scanf("%s",&occ);
               i=0,cnt=0;
               while(str1[i]!='\0')
                        {
                         if(str1[i]==occ)
                        {
                         cnt++;
                         }
                         i++;
                        }

               printf("\n the number of occurencs is %d",cnt);
               break;
case 3:exit(0);
              }
              }
               getch();
              }
   /*output
 menues
1. check if one string is substring of another string
2. count number of occurrences of characters in the string
3.exit
Enter Your choice: 1

 enter the first stringindia
enter the second stringind

  second string is  substring of first string
 menues
1. check if one string is substring of another string
2. count number of occurrences of characters in the string
3.exit
Enter Your choice: 2

 enter the stringmaharashtra
enter the  character to find occurencesa

 the number of occurencs is 4
 menues
1. check if one string is substring of another string
2. count number of occurrences of characters in the string
3.exit */