C programming 2013

2nd Semester

2013

 
Q.no 3

#include<stdio.h>
int min(int a[],int n) //Returns minimum element in array
{
 int i,minn=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]<minn)
   minn=a[i];
 }
 return minn;
}
int max(int a[],int n) //Returns maximum element in array
{
 int i,maxx=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]>maxx)
   maxx=a[i];
 }
 return maxx;
}
void main()
{
 int n,i,a[n],j,count=0,min_element,max_element,pov=0,neg=0,cou=0,small;
printf("Enter how many number to enter ");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 small=min(a,n);
 Printf("smallest number is %d",small);
 min_element=min(a,n);
 max_element=max(a,n);
 for(i=min_element;i<max_element;i++)   //Run loop from min element to max element
 {                                      //and get count of each element in array as usual.
  count=0;
  for(j=0;j<n;j++)
  {
   if(a[j]==i)
    count++;
  }
  if(count)
   printf("Count of %d is %d\n",i,count);
 }
 for(i=0;i<n;i++)
 {
  if(i<0)
  {
   neg=neg+a[i];
cou++;
  }
  else
  {
   pov=pov+a[i];
  }
 }
 printf("Sum of positive number = %d\nSum of negative number = %d\n",pov,neg);
 for(i=0;i<n;i++)
 {
  if(a[i]%2==0)
  {
   cou++;
  }
 }
 printf("Total no of even elements = %d",cou);
}

Q.no. 4

#include<stdio.h>
#include<string.h>
int main()
{
 int n,i;
 int id[n];
 char name[n][20],oname[n][20],occ[n][20];
printf("Enter number of input to be entered");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("Enter id ");
  scanf("%d",&id[i]);
  printf("Enter name ");
  scanf("%s",name[i]);
  printf("Enter office name ");
  scanf("%s",oname[i]);
  printf("Enter occupation ");
  gets(occ[i]);
 }
 for(i=0;i<n;i++)
 {
  if(strcmp(oname[i],"Nepal Bank")==0 && strcmp(occ[i],"manager")==0)
   printf("%s\t %d\t %s\t %s\t",name[i],id[i],oname[i],occ[i]);
 }
}

Q.no 5

#include<stdio.h>
int min(int a[],int n) //Returns minimum rainfall month
{
 int i,minn=a[0];
for(i=0;i<n;i++)
 {
  if(a[i]<minn)
   minn=i;
 }
 return minn;
}
int max(int a[],int n) //Returns maximum rainfall month
{
 int i,maxx=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]>maxx)
   maxx=i;
 }
 return maxx;
}
void result(int a)
{
 switch(a)
 {
  case 1:
   printf("Baisakh\n");
   break;
  case 2:
   printf("Jestha\n");
   break;
case 3:
   printf("Ashad\n");
   break;
  case 4:
   printf("Shrawan\n");
   break;
  case 5:
   printf("Bhadoh\n");
   break;
  case 6:
   printf("Ashosh\n");
   break;
  case 7:
   printf("Kartik\n");
   break;
  case 8:
   printf("mangsir\n");
   break;
  case 9:
   printf("poush\n");
   break;
  case 10:
   printf("magh\n");
   break;
  case 11:
   printf("falgun\n");
   break;
  case 12:
   printf("chaitra\n");
}
}
void main()
{
 int n[12],i,total;
 float avg;
 for(i=0;i<12;i++)
 {
 printf("Enter total no of rainfall for %d month\n",i+1);
 scanf("%d",&n[i]);
 total=total+n[i];
 }
 avg=total/12;
 printf("Average = %f\n",avg);
 for(i=0;i<12;i++)
 {
  if(max(n,12)==i)
  {
   printf("month with highest rainfall is ");
   result(i+1);
  }
  if(min(n,12)==i)
  {
   printf("Month with lowest rainfall is ");
result(i);
  }
 }
}

Leave a Reply

Your email address will not be published.