Friday, September 16, 2011

Find students grades through structure

Q. Write a C program using structure to find student grades in a class. Make necessary assumption.

Ans.
Before writing program we make some assumption as following :

Maximum total marks is 500.     
  Student_percentage         Grades
     >=80                        A
     >=60                        B
     >=50                        C
     >=40                        D
     <40                         F
  
/*C program to find students grades in a class through structure */
#include<stdio.h>
#include<conio.h>
struct stud
{
  char nam[20];
  int obtain_mark;
  int per;
  char grad[5];
};
struct stud s[5];
int i;
int main()
{
 for(i=1; i<=5; i++)
 {
  printf("Enter %d student name : ",i);
  scanf("%s",&s[i].nam);
  printf("Enter %d student obtained marks = ",i);
  scanf("%d",&s[i].obtain_mark);
  fflush(stdin);
 }
 for(i=1; i<=5; i++)
   s[i].per=s[i].obtain_mark/5;
 for(i=1; i<=5; i++)
 {
  if(s[i].per>=80)
    strcpy(s[i].grad,"A");
  else if(s[i].per>=60)
    strcpy(s[i].grad,"B");
  else if(s[i].per>=50)
    strcpy(s[i].grad,"C");
  else if(s[i].per>=40)
    strcpy(s[i].grad,"D");
  else
    strcpy(s[i].grad,"F");
 }
 for(i=1; i<=5; i++)
  printf("\n%d student %s has obtained grade %s ",i,s[i].nam,s[i].grad);
 getch();
 return 0;
}
  Output of above programe :

Enter 1 student name : John
Enter 1 student obtained marks : 250
Enter 2 student name : Robert
Enter 2 student obtained marks : 410
Enter 3 student name : Isabell
Enter 3 student obtained marks : 386
Enter 4 student name : Adem
Enter 4 student obtained marks :197
Enter 5 student name :Harry
Enter 5 student obtained marks : 490 

1 student Jhon has obtained grade C
2 student Robert has obtained grade A
3 student Isabell has obtained grade B
4 student Adem has obtained grade F
5 student Harry has obtained grade A
 


C program for find student grade through structure
Figure: Screen-shot of find student grades 
through structure C program

Related programs:

  1. Get student 5 subject marks and calculate percentage and division

Count number of wrods and characters in string

/* C programe for counting the number of characters and words in a given string */
#include<stdio.h>
#include<conio.h>
int main()
{
 int count_words=0,i;
 int count_char=0;
 char str[20];
 printf("Enter string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++)
 {
   count_char++;
   if(str[i]==' ')
      count_words++;
 }
 printf("\nNumber of characters in string : %d",count_char);
 printf("\nNumber of words in string : % d",count_words+1);
 getch();
 return 0;
}

     Output of above program :

Enter string : c programming codes
Number of characters in string : 19
Number of words in string : 3

Monday, September 12, 2011

write a c program which produces its own source code as its output


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}









How do you write a program
which produces its own source code as its output in c language?



Addition of two 3x3 matrix

Q. Write a C program for addition of two 3x3 matrix.

Ans.


#include<stdio.h>
#include<conio.h>
int main()
{
 int mata[3][3],matb[3][3],matc[3][3];
 int r,c,k;
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter first matrix : ");
    scanf("%d",&mata[r][c]);
  }
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter second matrix : ");
    scanf("%d",&matb[r][c]);
  } 
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    matc[r][c]=0;
    for(k=0; k<3;k++)
       matc[r][c]=mata[r][c] + matb[r][c];
  }
 }
 printf("New addition matrix : \n"); 
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
     printf(" %d",matc[r][c]);
  printf("\n");
 }
 getch();
 return 0;
}

Output of above program :

Enter first matrix :
  1 2 3
  4 5 6
  7 8 9
Enter second matrix :
  2 1 7
  4 6 3
  8 1 1

New addition matrix :
  3  3  10
  8  11 7
  15 9  10

GCD or HCF of two numbers

Q. Write a C program for finding GCD of two given numbers.

Ans.

 /*c program for finding GCD/HCF of two numbers*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int n1,n2,rem;
  printf("Enter first number : ");
  scanf("%d",&n1);
  printf("Enter second number : ");
  scanf("%d",&n2);
  if(n2>n1)
  {
    n1=n1+n2;
    n2=n1-n2;
    n1=n1-n2;
  }
  do{
      rem=n1%n2;
      n1=n2;
      n2=rem;
    }while(rem!=0);
  printf("\nGCD of two number is %d",n1);
  getch();
  return 0;
 }

/************** OUTPUT ***************
Enter first number : 65
Enter second number : 45


GCD of two number is 5
**********************************/

call by reference swap program

Q. what is calling by reference? How it is different from call by value? Write a C function to swap two given numbers using call by reference mechanism.

Ans.

When an argument is passed by reference, the caller actually allows the called function to modify the original variable's value.

  • Sends the address of a variable to the called function.
  • Use the address operator(&) in the parameter of the called function.
  • Anytime we refer to the parameter, therefore we actually referring to the original variable.
  • If the data is manipulated and changed in the called function, the original data in the function are changed.
/*swap function to interchange values by call by reference*/
#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
 int n1,n2;
 printf("Enter first number (n1) : ");
 scanf("%d",&n1);
 printf("Enter second number (n2) : "); 
 scanf("%d",&n2);
 printf("\nBefore swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 swaping(&n1,&n2);
 printf("\nAfter swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 getch();
 return 0;
}
void swaping(int *x, int *y)
{
  int z;
  z=*x;
  *x=*y;
  *y=z;
}

/***************** OUTPUT **************/
call by reference swap C program
Swap using call by reference