Monday 29 February 2016

Get Student Marks and Calculate Percentage & Division

Q. The marks obtained by a student in 5 different subjects are input through the keyboard.
The student gets a division as per the following rules:

Percentage above or equal to 60- 1st division
Percentage between 50 and 59- 2nd division
Percentage between 40 and 49- 3rd division
Percentage less then 40- Fail


Ans:

#include<stdio.h>
int main()
{
 float m1,m2,m3,m4,m5,avg,per; //m=marks
 printf("Enter 5 subject marks: ");
 scanf("%f %f %f %f %f",&m1,&m2,&m3,&m4,&m5);
 per = (m1+m2+m3+m4+m5)*100/500;
 printf("Student get %0.2f percentage. \n",per);
 if(per>=60)
   printf("1st Division");
 else if(per>=50 && per<=59)
   printf("2nd Division");
 else if(per>=40 && per<=49)
   printf("3rd Division");
 else if(per<40)
   printf("Fail");
 return 0;

}

Output:

What is Function

What is function in C?

A function is a self-contained block of statement that perform a coherent task of some kind.
There are two types of function in C:

  • Library function or In-built function
  • User define function

#include<stdio.h>                                  
void disp();   /* function prototype declaration */
int main()                                         
{                                                  
  disp();      /*function calling*/                
  printf("C is Awesome!!!");                       
  return 0;                                        
}                                                  
void disp()    /* function definition */           
{                                                  
  printf("Hi, ");                                  

The output of above program would be:

Hi, C is Awesome!!!

Let's grab some useful info from the above program:
In above program we have 2 function:
main()
disp()
  
Here you can see and thought:
1. why main() is used only once in program?
2. why disp() is used three times in program?

The answer of first question is "if a c program contains only one function then it must be main()." i.e. every C program must have main() function.

The answer of second question: let's understand the meaning of every disp() in program:

void disp();
this is the first disp() that used in program. It is function prototype.
This prototype tells the compiler that as:
What is function type ( i.e. void or not ),
What is name of function( i.e. in our program function name is disp)
Both parenthesis () indicate that disp is function.
keep in mind: void word indicating that "nothing return".

disp();
this is the second disp() that used in program. It is function calling.
Every calling function main work is as:
when compiler reached at calling function disp(); program control passes to the function disp(), (i.e. activity of main() is temporary suspend ), when disp() function runs out all his statement execute, the control returns to the main() function and begins executing code at the exact point where it is left off.

void disp()
{
  printf("Hi, ");
}

this is the third disp() that used in program. It is function definition.
In function definition we can used all C language keywords, operators, variable etc.
In above program recently we have only 1 printf(); statement.

#include<stdio.h>
void fun1();
void fun2();
void fun3();
int main()
{
  printf("\nI am in main function!!");
  fun1();
  fun2();
  fun3();
  return 0;
}
void fun1()
{
  printf("\nI am in fun 1");
}


void fun2()
{
  printf("\nI am in fun 2");
}


void fun3()
{
  printf("\nI am in fun 3");
}

The output of above program would be:

I am in main function!!
I am in fun 1
I am in fun 2
I am in fun 3            

C Operators

What is operators?

In simple word operators are those which operator anything like as data,maths,information etc i.e.. When we talking about C operators, i want to say you that C is very rich in operators. Operators tells to compiler that what do with data. There are mainly operators in c as following:

  • Logical Operators
  • Bitwise Operators
  • Relational Operators
  • Assignment Operators
  • Sizeof Operators
  • Increment and Decrement operators
  • Comma operators
  • Type cast operator
  • Conditional operators or ternary operators
Sometime operators are divided according numbers of operands. So there are three types of operators as following : 


Operators

              Unary       Binary    Ternary
operand         single         double    third
works      right to left left to right left to right
Example_1        ++ , --     +, -, *, /     ? :
Example_2 ! , ~        && , ||

Lucas number Program

Lucas numbers: It is similar to Fibonacci numbers, each Lucas number is defined to be the sum of its two immediate previous terms Where as the first two number are 2 and 1.
The Lucas numbers are as following :
2 1 3 4 7 11 18 29 47 76

#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter the limit of Lucas number : ");
 scanf("%d",&num);
 x=2;
 y=1;
 while(num>=x)
 {
   printf(" %d",x);
   z=x+y;
   x=y;
   y=z;
 }
 getch();
 return 0;
}


Output;

Enter the limit of Lucas number : 125
 2 1 3 7 11 18 29 47 76 123


Array in C program

                                               Array Declaration

To declare an array we need three things as:
  • What kind types(i.e. data type) of array?
  • What is the name of array?
  • What is the size of array?
In our C program, we have declare array as:

syntax: data_type array_name[SIZE];
example: int result[10];

Here, int is a data type and the word result is the name of array. The [10] is however is new. The number 10 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The bracket ( [] ) tells the compiler that we are dealing with an array.

                    Accessing element of an Array

Once an array is declared, we can access it and use it in our program.
Now you know that, all the array elements are numbered, starting with 0. Thus, result[10] is not the 10th elements of the array, but the 9th.
We are using the variable i as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn. This ability to use variables to represent subscripts is what makes arrays so useful.

                  Entering Data into an Array

int result[10];
for(i=0; i<10; i++)
{
 printf("\nEnter elements: ");
 scanf("%d", &result[i]);
}

The for loop causes the process of asking for and receiving a result from the user to be repeated 10 times. The first time through the loop, i has a value 0, so the scanf() functions will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated until i become 9. This is last time through the loop, which is a good thing, because there is no array element like result[10].

                  Reading data from an array

for(i=0; i<10; i++)
printf("\n%d",result[i]);

           
                   Array Initialization

We has been discuss about how to declare arrays, access them and read them. Let us now see how to initialize an array while declaring it as:

Syntax:
data_type arrayName[SIZE]={n1,n2,n3,.....}
Here, n1,n2,... etc are equal to SIZE.

Example
int result[5]={10,20,4,8,99}
float price[]={1.5,53.3,-66.6}
int race[]={11,4,579,31,278,3}

  • Till the array elements are not given any specific values, they are supposed to contain garbage values.
  •  If the array is initialized where it is declared, mentioning the dimension of the array is optional as in the 2nd and 3rd example above.

                              Array elements in memory

Consider the following array declaration:
int a{5};

What happens in memory when we make this declaration?
10 bytes get immediately reserved in memory, 2 bytes each for the 5 integers. And since the array is not being initialized, all five values present in it would be garbage values. This so happens because the storage class of this array is assumed to be auto. If the storage class is declared to be static then all the array elements would have a default initial value as zero. Whatever be the initial values, all the array elements would always be present in contiguous memory locations. This arrangement of array elements in memory is shown in the following figure as:









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:

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

Number of words in string : 3 


Convert Fahrenheit to Celsius

Q. Write a C program to convert Fahrenheit to Celsius temp temperature.
or
Q. Write a C program to accept temperature in Fahrenheit  from user and convert into the Celsius.

Ans

Formula:Celsius = (Fahrenheit - 32) / 1.8

#include<stdio.h>
#include<conio.h>
int main()
{
 float fh,cl;
 printf("Enter temperature value in Fahrenheit: ");
 scanf("%f", &fh);
 cl = (fh - 32) / 1.8;
 printf("Converted Celsius value: %f",cl);
 getch();
 return 0;

}

Count digit repetition in number range

Q. Write a C program to count the no. of occurences ofn's in a given number range.

For example:
random numbers : 45,78,2,65,489,6,2,6,55,9
find out how many times the digit 2 repeat?
2's digit repeat 2 times.


Ans:

#include<stdio.h>
int main()
{
 int arr[10],i,num,r,s,c=0;
 for(i=1; i<=10; i++)
 {
  printf("Enter %d number : ",i);
  scanf("%d", &arr[i]);
 }
 printf("\nEnter search number : ");
 scanf("%d", &s);

 for(i=1; i<=10; i++)
 {
  num = arr[i];
  while(num>=1)
  {
   r=num%10;
   if(r==s)
     c++;
   num=num/10;
  }
 }
 printf("\nThe digit %d is repeat in entered number is %d times",s,c);
 getch();
 return 0;

}

kg to pound convert

Q. Write a C program to convert kilograms to pounds.

Ans
Formual:
1 kilogram = 2.20462262184878 pounds
Hence, pounds = 2.2 * kilograms 


#include<stdio.h>
#include<conio.h>
int main()
{
  float kg,pound;
  printf("Enter value of Kilograms : ");
  scanf("%f",&kg);
  pound=2.20462262184878*kg;
  printf("\n\t-- Convert Kilograms to pounds --\n");
  printf("\n%f kg = %f pound",kg,pound);
  getch();
  return 0;

}

Character triangle

Q. Write a C program to print the following character triangle:


                                  ABCDEDCBA
                                     BCDEDCB
                                        CDEDC
                                           DED
                                              E


Ans:

#include<stdio.h>
#include<conio.h>
int main()
{
  int sp;
  char ch,r,c;
  printf("Enter pyramid character : ");
  ch=getchar();
  if(ch>='a' && ch<='z')
    ch=ch-32;
  for(r='A'; r<=ch; r++)
  {
     for(sp=r; sp>'A'; sp--)
       printf(" ");
     for(c=r; c<=ch; c++)
       printf("%c",c);
     for(c=ch-1; r<=c; c--)
       printf("%c",c);
     printf("\n");
  }
  return 0;

}

Convert Inch to Feet and Feet to Inch

Q. Write a C program to convert Inch to Feet and Feet to Inch.

Ans

1 Foot  = 12 Inch
1 Inch  = 0.083 Foot


#include<stdio.h>
int main()
{
 int ch;
 double foot,inch;
 printf("\nEnter 1 for convert Foot to Inch.");
 printf("\nEnter 2 for convert Inch to Foot.");
 printf("\nEnter 0 for exit.");
 printf("\n\nEnter your choice : ");
 scanf("%d", &ch);
 switch(ch)
 {
  case 1:
    printf("\nEnter value in Foot: ");
    scanf("%lf", &foot);
    inch = 12 * foot;
    printf("\n\t-- Convert Foot to Inch --\n");
    printf("\n%lf foot = %lf Inch",foot,inch);
    break;
  case 2:
    printf("\nEnter value in Inch: ");
    scanf("%lf", &inch);
    foot = (0.083) * inch;
    printf("\n\t-- Convert Inch to Foot --\n");
    printf("\n%lf Inch = %lf Foot",inch,foot);
    break;
  case 0:
    goto exit;
  default:
    printf("\nYou enter invalid options.");
 }
 exit:
 return 0;

}

Fibonacci Series C program

Fibonacci numbers:Each Fibonacci numbers is defined to be the sum of its two immediate previous terms, where as the first two number are 0 and 1.
The Fibonacci numbers are the numbers in the following integer sequence :
0 1 1 2 3 5 8 13 21 34 55 89 144. . . . .

Ans:

/*Programme to print Fibonacci numbers less than given user number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter any last number of Fibonacci series : ");
 scanf("%d",&num);
 x=0;
 y=1;
 while(num>=x)
 {
    z=x+y;
    printf("%d\t",x);
    x=y;
    y=z;
 }
 getch();
 return 0;

}

Output:

Enter last number where goes to end Fibonacci series : 70


0  1  1  2  3  5  8  13  21  34  55


Data type in C

                   
                                            Data Type In C Language


There are mainly three data type in C as following :

1.Basic Data Type
  • int
  • char
  • float
  • long
  • double
  • long double
2.Derived Data Type
  • Array
  • Pointer
  • function
3.User Define Data Type
  • structure
  • union
  • enumerated
  • typedef


Following table tell which data type how much allocate memory?


         Data Type              Memory Allocation
              int                       2 byte
            long                       4 byte
            float                       4 byte
           double                       8 byte
        long double              10 byte
            char                      1 byte

Star (*) Praymid

Q. Write a C program to print the following triangle:







*








***






*****




*******


*********
***********

Ans.

/*c program for star pyramid*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int num=6,r,c,sp;
  for(r=1; num>=r; r++)
  {
   for(sp=num-r; sp>=1; sp--)
       printf(" ");
   for(c=r; c>=1; c--)
        printf("*");
   for(c=r; c>1; c--)
        printf("*");
   printf("\n");
  }
  getch();
  return 0; 
 }

Increment and Decrement operators


                                                Increment operators (++)


  • Increment operators are increase the value of subsequent. value may be increase according to the programmer.
  • Increment operator are two types as follows :
    1. Post increment
    2. Pre increment


                  Decrement operators ( -- )


  • decrement operators decrease the value to one, two and so on.
  • As like Increment operators, decrement operators are also two type as :  
    1. Post decrement
    2. Pre decrement
Before we more discuss about increment and decrement operator, let understand some expression; that's are using in all operators.

Now some issues of pre and post increment and decrement, we will discuss after solve following programming :



Now some issues of pre and post increment and decrement, we will discuss after solve following programming :

 x = 5       x = 5     x = 5  
 x = x+1      ++x       x++     
 x = 6       x = 6      x=5   


In above program u notice that value of x increase to one in all three expression but after the increasing one, the final value of x in not same, third box result is differ to another two boxes. Now you know that c operators are mostly three types, ++ and -- operators are Unary operators.

Post increment or decrement are lowest precedence so it is solve at the end of program. Hence above program in third box first value of x is print i.e. 5 after, it is increase.

Unary operators are solve right to left.     

Ternary operator



Main features of ternary operators as follows : 

  • There are three operand.
  • It works from left to right
  • Example of ternary operator is conditional operator. Symbol of conditional operator : condition ? True_parts : False_parts 

Ads Inside Post