Wednesday, January 30, 2013

Write a program to create a structure which will store name, roll and marks of 6 courses that you obtained. This structure will also store the total obtained marks. Which you can obtain by summing all the marks. Store all the data for 10 students. Print the name, roll and total obtained marks of each student.

#include<stdio.h>
#include<conio.h>
struct student
{
    char name[30];
    int roll,bangla,english,math,computer,fundamental,physics,total_m;
};
main()
{
    clrscr();
    struct student result[10];
    int i;
    for(i=0;i<10;i++)
    {
        printf("Enter Name:= ");
        scanf("%s",result[i].name);
        printf("Enter roll:= ");
        scanf("%d",&result[i].roll);
        printf("Enter Bangla Marks:= ");
        scanf("%d",&result[i].bangla);
        printf("Enter English Marks:= ");
        scanf("%d",&result[i].english);
        printf("Enter Math Marks:= ");
        scanf("%d",&result[i].math);
        printf("Enter comuter  Marks:= ");
        scanf("%d",&result[i].computer);
        printf("Enter fundamenta Marks:= ");
        scanf("%d",&result[i].fundamental);
        printf("Enter Phusics Marks:= ");
        scanf("%d",&result[i].physics);
        result[i].total_m = result[i].bangla + result[i].english + result[i].math + result[i].computer + result[i].fundamental + result[i].physics;
    }
    clrscr();
    for(i=0;i<10;i++)
    {
        printf("%s---",result[i].name);
        printf("%d---",result[i].roll);
        printf("%d\n",result[i].total_m);
    }
    getch();
}

Write a program which will input some numbers in an integer array and pass this to a function called maximum (). This function will find the maximum of the array values and returns this value to the main () function for displaying it

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

int maximum( int [] );        

int  maximum( int values[5] )
{
    int  max_value, i;
       
    max_value = values[0];
    for( i = 0; i < 5; ++i )
        if( values[i] > max_value )
            max_value = values[i];
       
    return max_value;
}
   
main()
{
    int values[5], i, max;
   
    printf("Enter 5 numbers\n");
    for( i = 0; i < 5; ++i )
        scanf("%d", &values[i] );
       
    max = maximum( values );
    printf("\nMaximum value is %d\n", max );
    getch();
}

Write a program which will input some numbers in an integer array and pass this to a function called minimum (). This function will find the minimum of the array values and returns this value to the main () function for displaying it

#include <stdio.h>
#include<conio.h>
int minimum( int [] );        

int  minimum( int values[5] )
{
    int  min_value, i;
       
    min_value = values[0];
    for( i = 0; i < 5; ++i )
        if( values[i] < min_value )
            min_value = values[i];
       
    return min_value;
}
   
main()
{
    int values[5], i, min;
   
    printf("Enter 5 numbers\n");
    for( i = 0; i < 5; ++i )
        scanf("%d", &values[i] );
       
    min = minimum( values );
    printf("\nminimum value is %d\n", min );
    getch();
}

Write a C program to find the number of and sum of all integers greater than 50 and less than 300 that are divisible by 9 using function.

#include <stdio.h>
#include <conio.h>
int div(int n);
int main()
{
    int i,sum=0,m;
    clrscr();
    for(i=51;i<300;i++)
    {
        m=div(i);
        sum=sum+m;
    }
    printf("\n\nSum of these numbers is: %d",sum);
    getch();
}

int div(int n)
{
    if(n%9==0)
        return(n);
    else
        return(0);
}

Write a program which will read a text and count all occurrences of a particular word.

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

void main()
{
    char a[100],b[20],c[20];
    int i,count=0,k=0,len;
    clrscr();
    printf ("\nEnter a string:-> ");
    gets(a);
    printf("\nEnter the word to be searched:-> ");
    gets(b);
    len=strlen(a);
    for(i=0;i<=len;i++)
    {
        //Assuming Space , Tab and NULL as word separator
        if(a[i]==32 || a[i]=='\t' || a[i]=='\0')
        {
            c[k]='\0';
            if(strcmp(b,c)==0)
                count++;
            k=0;
        }
        else
        {
            c[k]=a[i];
            k++;
        }
    }
    printf("Occurance of %s is = %d",b,count);
    getch();
}

Write a program to test whether the given input string is palindrome or not.

/*level, madam, radar, refer, rotator, stats */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char s[20]={'\0'},a[20]={'\0'};
    int i,j,l=0;
    clrscr();
    printf("Enter String\n");
    gets(s);
    j=strlen(s)-1;
    for(i=0;i<strlen(s);i++)
        a[i]=s[j--];


    for(i=0;i<strlen(s);i++)
    {
        if (s[i]!=a[i])
        {
            printf("\n The given input is not a Palindrome");
            break;
        }
        else
        {
            l=1;
            break;
        }
    }
    if(l==1)
        printf("\n The Given Input is Palindrome");

    getch();
}

Write a c program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with nth character

Write a c program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted,  starting with nth character


#include<stdio.h>
#include<conio.h>
int main()
{
    char a[50],b[50];
    int s,m,i,p=0;
    printf("Please Enter String:= ");   
    gets(a);
    printf("Enter Start point for Extract:= ");
    scanf("%d",&s);
    printf("How many characters are extracted:= ");
    scanf("%d",&m);
    for(i=s;i<m+s;i++)
        b[p++]=a[i];
    b[p]='\0';
    puts(b);
    getch();
}



Write a program to compute and print a multiplication for numbers 1 to 5

#include<stdio.h>
#include<conio.h>
int main()
{
    int r,i,j,k;
    printf("Enter the number range: ");
      scanf("%d",&r);
      for(i=1;i<=r;i++)
    {
              for(j=1;j<=10;j++)
               printf("%d*%d=%d ",i,j,i*j);
              printf("\n\n");
      }
    getch();
}

Write a C program to add & subtract two same size matrices

#include<stdio.h>

#include<conio.h>



main()

{

    clrscr();

    int a[50][50],b[50][50],c[50][50],i,j,row,col;

    printf("Enter how many row number you want= ");

    scanf("%d",&row);

    printf("Enter how many col number you want= ");

    scanf("%d",&col);

    //First Matrix Input

    for(i=0;i<row;i++)

        for(j=0;j<col;j++)

            scanf("%d",&a[i][j]);



    //Second Matrix Input

    for(i=0;i<row;i++)

        for(j=0;j<col;j++)

            scanf("%d",&b[i][j]);



    //Calculation addition

    for(i=0;i<row;i++)

        for(j=0;j<col;j++)

            c[i][j]=a[i][j]+b[i][j];

    clrscr();

    printf("Firat Matrix=\n");

    for(i=0;i<row;i++)

    {

        printf("| ");

        for(j=0;j<col;j++)

            printf("%d  ",a[i][j]);

        printf("|\n");

    }

    printf("\nSecond Matrix=\n");

    for(i=0;i<row;i++)

    {

        printf("| ");

        for(j=0;j<col;j++)

            printf("%d  ",b[i][j]);

        printf("|\n");

    }

    printf("\nOutput addition= \n");



    for(i=0;i<row;i++)

    {

        printf("| ");

        for(j=0;j<col;j++)

            printf("%d  ",c[i][j]);

        printf("|\n");

    }

   

    //Calculation Subtraction

    for(i=0;i<row;i++)

        for(j=0;j<col;j++)

            c[i][j]=a[i][j]-b[i][j];



    printf("\nOutput Subtraction= \n");



    for(i=0;i<row;i++)

    {

        printf("| ");

        for(j=0;j<col;j++)

            printf("%d  ",c[i][j]);

        printf("|\n");

    }

   

    getch();

}

Write a program to set all the diagonal elements of a two dimensional array to 1 and others to 0

#include<stdio.h>
#include<conio.h>
main()
{


     int a[10][10],i,j;       //...{a,i,j,n,i,o, gnimit si sgniht yreve}
     for(i=0;i<=8;i++)
       {
    for(j=0;j<=8;j++)
      {
       if(i==j || i+j==8)
         printf("1  ");
       else
         printf("0  ");
      }
    printf("\n\n");
       }

    getch();
}

Write a program to set all the diagonal elements of a one dimensional array to 1 and others to 0

#include<stdio.h>
#include<conio.h>
main()
{
    int a[10][10],i,j;
    clrscr();
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            if(i!=j)
                printf("| 0 ");
            else
                printf("| 1 ");
        }
        printf("|\n\n");
     }
     getch();
}

The number in a sequence 1 1 2 3 5 8 13 21 . . . . . . . are called Fibonacci numbers. Write a C program using a for loop to calculate and print the first 20 Fibonacci number.

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

void main()
{
    int x,y,n,i,z;
    clrscr();
    printf("Please enter a value:");
    scanf("%d",&n);
    printf("\n\n");
    x=0;
    y=1;

    for(i=1;i<=n;i++)
    {
        z=x+y;
        printf("%d  ",z);
        y=x;
        x=z;
    }

    getch();
}

Write a C program to determine whether a number is a prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,n,b=0;
    clrscr();
    printf("Please enter a number:");
    scanf("%d",&n);
    if(n==1)
        printf("%d is not a prime number",n);
    else
     {
          for(i=2;i<n;i++)
                   if(n%i==0)
            {
                  b=1;
                  break;
            }
             
           if(b==1)
               printf("%d is not a prime number",n);
           else
               printf("%d is a prime number",n);
    }
    getch();
}

Write a C program to find the number of and sum of all integers greater than 50 and less than 300 that are divisible by 9

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

main()
{
    int i,sum=0;
    clrscr();

    for(i=51;i<300;i++)
    {
        if(i%9==0)
            sum=sum+i;
       
    }
    printf("\n\nSum of these numbers is: %d",sum);
    getch();
}

Write a program that read the value of x and evaluate the following function using if statement and ?: Operator

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


void main()
{
       clrscr();
    int x,y;
       scanf("%d", &x);
    if(x==0)
        y=0;
    else
        y=(x>0)?1:-1;

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

Write a program that evaluate y = xn

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

void main()
{
       clrscr();
    int x,n;
       scanf("%d %d", &x, &n);
    printf("%.0lf",pow(x,n));
       getch();
}

Write a program that will obtain the length and width of a rectangle from the user and Compute its area and perimeter.

#include <stdio.h>
#include<CONIO.H>

main()

{

clrscr();
float length, breadth, aor, por;

printf ("\nEnter the Length and Breadth of the Rectangle: ");
scanf("%f %f", &length, &breadth);


aor = length*breadth;
por= 2*(length+breadth);


printf("\nThe area of the rectangle is: %f", aor);

printf("\nThe perimeter of the rectangle is: %f ", por);


getch();
}

Write a C program to compute the roots of a quadratic equation.

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    float a,b,c,d,x1,x2;
    printf("Please Enter the value of a:= ");
    scanf("%f",&a);
    Printf("Please Enter the value of b:= ");
    scanf("%f",&b);
    printf("Please Enter the value of c:= ");
    scanf("%f",c);
    d:=(b*b)-4*a*c;
    if(d<0);
        printf("No Solutions");
    else
        {
            x1=(-b+sqrt(d))/(2*a);
            x2=(-b-sqrt(d))/(2*a);
            printf("%f%f",x1,x2);
        }
    getch();

}

Write a c program for triangle

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

void main()
 {
   float ht,base,area;
   clrscr();
   printf("Enter the height and base of the triangle");
   scanf("%f %f",&ht, &base);
   area=0.5*ht*base;
   printf("Area of the triangle is= %.2f",area);
   getch();
 }

Write a program to calculate the area and circumference of a circle.

#include <stdio.h>
#include<conio.h>
#define PI 3.1416
main()
{

float radius, aoc, coc;
clrscr();
printf("\nEnter the Radius of the Circle: ");
scanf("%f", &radius);


aoc = PI*radius*radius;

coc = 2*radius*PI;



printf("\n\nThe area of the Circle with radius %f is: %f ", radius, aoc);

printf("\nThe circumference of the same circle is: %f", coc);
getch();
}

Write a C program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7

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

main()
{
    int i,sum=0;
    clrscr();

    for(i=100;i<200;i++)
    {
        if(i%7==0)
            sum=sum+i;
       
    }
    printf("\n\nSum of these numbers is: %d",sum);
    getch();
}

Write a program to calculate of average of five numbers.

#include<stdio.h>
#include<conio.h>
main()
{
  clrscr();
       int a,b,d,c,e,sum;
       float avg;
    printf("Enter marks of english- ");
       scanf("%d",&a);
    printf("Enter marks of Bangla- ");
      scanf("%d",&b);
    printf("Enter marks of Math- ");
      scanf("d%",&c);
    printf("Enter marks o Physics- ");
      scanf("d%",&d);
    printf("Enter marks of Chemistry- ");
      scanf("d%",&e);
       sum=a+b+c+d+e;
      printf("%d",sum);
      avg=sum/5;
    printf("%f",avg);

  getch();
}

Write a program to convert the given temperature in Fahrenheit to Celsius.

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

main()

{
    float f,c;

    clrscr();
    printf("Enter a Farhenite number-- ");
    scanf("%f",&f);
    c=(f-32)/1.8;
    printf("the Secious temparatute is %f",c);
    getch();
}

What are the difference between global and local variables?


Global Variable
Local Variable
Global Variable is variable which is declared before main function.
Local Variable is variable which is declared inside the main function or other functions.
A global variable can be accessed by all functions.

A local variable is isolated in its function.

It’s working with changing values.
It’s not working with changing values.
Variables treat(use) only one name in program.
Same variable names are working different values in different functions.
They are known to other functions and to the main program.
They are unknown to other functions and to the main program.

In what ways does a switch statement differ from an IF statement?



The if statement is a conditional branch, a branch to other statements if a certain condition or set of conditions is met. The condition can be logical or arithmetic.
 if (A>B)
 if else (C<D)
else j=14x7

 The switch statement (also known as a case statement) can branch to individual statements for a number of conditions or cases. it is more easily understood.
 switch case a>b... case c<d... case j=k... case else.. end

What is the difference between ++m and m++?



If the Increment operator has placed before the operand it Increment first and other operations later. Example:
m=5      
x = ++m;
In this case first Increment the value of m and then transfer the value of m into x.
And
If the Increment operator has placed after the operand it Increment after the other operations. Example:
m=5      
x = m++;
In this case first transfer the value of m and then Increment the value of m.

What is the difference between --m and m--?



If the decrement operator has placed before the operand it decrement first and other operations later. Example:
m=5      
x = --m;
In this case first decrement the value of m and then transfer the value of m into x.
And
If the decrement operator has placed after the operand it decrement after the other operations. Example:
m=5      
x = m--;
In this case first transfer the value of m and then decrement the value of m.