Friday, 21 November 2014

C programs important for cse students

Here are the basic and very important 27 C programs for the 1st year students of engineering.




1.To print message on the screen.
Program:
#include <stdio.h>

int main()
{
  printf("Hello world\n");
  return 0;
}

Output:
Hello world



2.WAP  that will take an input and display it on the screen.

Program:
#include <stdio.h>

int main()
{
   int a;
  printf("enter any number for an input\n");
  scanf(%d%,&a);
  printf(“\n the entered number is %d”,a);
  return 0;
}

Output:
enter any number for an input
5
the entered number is 5
3.WAP that will take dividend and divisor and display the quotient and remainder.

Program:
#include <stdio.h>

int main()
{
   int dividend,divisor,remainder;
   float quotient;
   printf("enter any dividend and divisor \n");
   scanf(“%d%d”,&dividend,&divisor);
   quotient=dividend/divisor;
   remainder=dividend%divisor;
   printf(“\n the quotient is %f and remainder is        %d”,quotient,remainder);
   return 0;
}

Output:
enter any dividend and divisor
5
7
the quotient is 0.7142 and remainder is 5

4.WAP to add two numbers
     a) using third variable
      b) without using third variable
a)Program:
#include <stdio.h>

int main()
{
   int a,b;
  printf("enter any numbers for addition \n");
  scanf(“%d%d”,&a,&b);
  printf(“\n the sum is %d”,a+b);
  return 0;
}

Output:
enter any numbers for addition
5
9
The sum is 14

b)Program:
#include <stdio.h>

int main()
{
   int a,b,c;
  printf("enter any numbers for addition \n");
  scanf(“%d%d”,&a,&b);
  c=a+b;
  printf(“\n the sum is %d”,c);
  return 0;
}

Output:
enter any numbers for addition
5
7
The sum is 12









5.WAP that will take principal,rate and time as input and display  the simple interest.
Program:

#include <stdio.h>
#include <conio.h>
void main()
{
   float p, r, si;
   int t;
   clrscr();
   printf("Enter the values of p,r and t\n");
   scanf ("%f %f %d", &p, &r, &t);
   si = (p * r * t)/ 100.0;
  
   printf ("Amount = Rs. %5.2f\n", p);
   printf ("Rate   = Rs. %5.2f%\n", r);
   printf ("Time   = %d years\n", t);
   printf ("Simple interest  = %5.2f\n", si);
}

Output:

Enter the values of p,r and t
2000
8
3
Amount = Rs. 2000.00
Rate   = Rs.  8.00%
Time   = 3 years
Simple interest  = 480.00





6. WAP to display the size of integer,float and char using sizeof() operator
    Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
float y;
double z;
char ch;
clrscr();
printf("Size of integer variable x:%d\n",sizeof(x));
printf("Size of float variable y:%d\n",sizeof(y));
printf("Size of double variable z:%d\n",sizeof(z));
printf("Size of character variable char:%d\n",sizeof(char));
getch();

  
Output: 

Size of integer variable x:2
Size of float variable y:4
Size of double variable z:8
Size of character variable char: 1


7.To find that given number is even or odd.

Program:
#include <stdio.h>
int main(){
      int num;
      printf("Enter an integer you want to check: ");
      scanf("%d",&num);
      if((num%2)==0)      /* Checking whether remainder is 0 or not. */
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}

Output 1:
Enter an integer you want to check: 25
25 is odd.

Output 2:
Enter an integer you want to check: 12
12 is even.



8.WAP to check whether number is prime or not.
Program:
#include<stdio.h>                   
#include<conio.h>
int main()
{
       int n; int i; printf(" enter the number : ");
       scanf("%d",&n);
        for(i=2; i<n; i++)
        {
                if(n%i==0)
                {
                        printf("%d is not a prime number.",n);
                        getch();        
                        return (1);                                                                                 
                }
        }
   printf("%d is a prime number.", n);
   getch();
}
Output:
enter the number : 29
29 is a prime number.


9.WAP to check whether the year entered by the user is leap year or not.
Program:
#include<stdio.h>
int main()
{
    int year;

    printf("Enter any year: ");
    scanf("%d",&year);

    if(((year%4==0)&&(year%100!=0))||(year%400==0))
         printf("%d is a leap year",year);
    else
         printf("%d is not a leap year",year);
 
    return 0;
}

Output:

Enter any year: 2008
2008 is a leap year








10.WAP for greatest among two numbers.
Program:
#include<stdio.h>
main()
{
 int a,b,c;
 printf("\n Enter two Numbers: ");
 scanf("%d %d",&a,&b); 
 c=(a>b)?a:b;
 printf("\n The Greatest number is : %d",c);
 return(0);
}

Output:
Enter two Numbers: 10 20
The Greatest Number is : 20

11.To find the greatest among 3 numbers 
a)using if else statements.
 
Program:
 
#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if (a>=b)
      {
          if(a>=c)
            printf("Largest number = %.2f",a);
          else
            printf("Largest number = %.2f",c);
      }
      else
      {
          if(b>=c)
            printf("Largest number = %.2f",b);
          else
            printf("Largest number = %.2f",c);
      }
      return 0;
}
 
 

Output:
Enter three numbers: 12.2
13.452
10.193
Largest number = 13.45

b)To find greatest among three numbers using &&.

Program:

#include<stdio.h>
Int main()
{
 Int a,b;
Printf(“\n Enter two numbers”);
Scanf(“%d%d”,&a,&b);
If((a>b)&&(a>c)
{
Printf(“\n a is greater”);
}
Else
If((b>a)&&(b>c)
{
Printf(“\n b is greater”);
}
Else
If((c>a)&&(c>b)
{
Printf(“\n c is greater”);
}
Getch();
Return 0;
}

Output:
Enter three numbers: 12.2
13.452
10.193
Largest number = 13.45


12.WAP greatest among four numbers.
Program:
#include<stdio.h> 
main() 
{ 
       int a,b,c,d; 
       clrscr(); 
       printf("Enter the Four Numbers :"); 
       scanf("%d %d %d %d",&a,&b,&c,&d); 
       if(a>b) 
        { 
             if(a>c) 
              { 
                 if(a>d) 
                  {   
                     printf("%d is big",a); 
                   } 
                 else 
                  { 
                     printf("%d is big",d); 
                   } 
               } 
          } 
              else if(b>c) 
                      { 
                           if(b>d) 
                             { 
                                 printf("%d is big",b); 
                              } 
                            else 
                             { 
                                 printf("%d is big",d); 
                             } 
                       } 
               else if(c>d) 
                      { 
                             printf("%d is big",c); 
                       } 
              else 
               { 
                    printf("%d is big",d); 
               } 
                    getch(); 
} 

Output:
Enter four numbers : 12 11 15 19
19 is big


13.To add,subtract,multiply,divide using switch statement.

Program:
 
# include <stdio.h>
int main()
{
    char o;
    float num1,num2;
    printf("Enter operator either + or - or * or divide : ");
    scanf("%c",&o);
    printf("Enter two operands: ");
    scanf("%f%f",&num1,&num2);
    switch(o) {
        case '+':
            printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
            break;
        case '-':
            printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
            break;
        case '*':
            printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
            break;
        case '/':
            printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            printf("Error! operator is not correct");
            break;
    }
    return 0;
}

Output:

Enter operator either + or - or * or divide : -
Enter two operands: 3.4
8.4
3.4 - 8.4 = -5.0


14.WAP to swap two numbers using with and without third variable.
a)Program:
#include<stdio.h>
 
void swap(int*,int*);
 
int main()
{
int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x,&y);
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
return0;
}
 
void swap(int*a,int*b)
{
int temp;
 
   temp =*b;
*b =*a;
*a = temp;
}
Output:

Enter the value of x and y
12
13
Before swapping
X=12
Y=13
After swapping
X=13
Y=12

b)Program:
#include<stdio.h>

int main() {
   int a, b;

   printf("\nEnter value for num1 & num2 : ");
   scanf("%d %d", &a, &b);

   a = a + b;
   b = a - b;
   a = a - b;

   printf("\nAfter swapping value of a : %d", a);
   printf("\nAfter swapping value of b : %d", b);
    return (0);
}
Output:
Enter value for num1 & num2 : 10 20

After swapping value of a : 20
After swapping value of b : 10


15.WAP to print a counting from 1 to 100 using
a)for loop
b)while loop
c)do while loop
 a)Program:
#include<stdio.h>
int main()
{    int n; 
     for(n=1;n<=100;n++)
printf(“%d “ “ ”,n); 
return 0;
}
b)Program:
#include<stdio.h>
int main()
{
int n=1; 
while(n<=100)
printf(“%d “ “ ”,n); 
return 0;
}
c)Program:
#include<stdio.h>
int main()
{
int n=1; 
do 
{ printf(“%d “ “ ”,n); 
n++; 
}while(n<=100);
return 0;
}
Output:
1 2 3 4 5 ……….. 98 99 100


16.To print the table of given numbers using while/do while loop .

Program:

#include<stdio.h>
#include<conio.h>
 voidmain()
{
inti=2,m,n;
printf("enternumber");
scanf("%d",&n);
do
{
m=i*n;
printf("%d*%d=%d\n",n,i,m);
i=i+1;
}
while(i<=10);
getch();
}
Output:
enter number 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90



17.To print a factorial of a number:

Program:
#include <stdio.h>
int main()
{
    int n, count;
    unsigned long long int factorial=1;         
    printf("Enter an integer: ");
    scanf("%d",&n);
    if ( n< 0)
        printf("Error!!! Factorial of negative number doesn't exist.");
    else
    {
       for(count=1;count<=n;++count)    /* for loop terminates if count>n */
       {
          factorial*=count;              /* factorial=factorial*count */
       }
    printf("Factorial = %lu",factorial);
    }
    return 0;
}

Output 1:
Enter an integer: -5
Error!!! Factorial of negative number doesn't exist.
 
Output 2:
 
Enter an integer: 10
Factorial = 3628800

18. To print pyramid for different patterns using for loop.

Program:

#include<stdio.h>
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}


Output:
*
* *
* * *
* * * *
* * * * *


19.To swap two numbers using call by value & call by reference.

a)Program:
#include<stdio.h>
 
void swap(int*,int*);
 
int main()
{
int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x,&y);
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
return0;
}
 
void swap(int*a,int*b)
{
int temp;
 
   temp =*b;
*b =*a;
*a = temp;
}
Output:

Enter the value of x and y
12
13
Before swapping
X=12
Y=13
After swapping
X=13
Y=12

b)Program:
#include<stdio.h>
 
void swap(int,int);
 
int main()
{
int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(x,y);
 
return0;
}
 
void swap(int a,int b)
{
int temp;
 
   temp =b;
b =a;
a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
 
}
Output:
Enter the value of x and y
12
13
Before swapping
X=12
Y=13
After swapping
X=13
Y=12


20.To print the sum of first n natural numbers by using recursion.

Program:

#include<stdio.h>
int add(int n);
int main()
{
    int n;
    printf("Enter an positive integer: ");
    scanf("%d",&n);
    printf("Sum = %d",add(n));
    return 0;
}
int add(int n)
{
    if(n!=0)
     return n+add(n-1);  /* recursive call */
}

Output:
Enter an positive integer: 10
Sum = 210
 
 
 
 





21)To display the biggest value in an integer array.

Program:

#include <stdio.h>
int main(){
    int i,n;
    float arr[100];
    printf("Enter total number of elements(1 to 100): ");
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;++i)  /* Stores number entered by user. */
    {
       printf("Enter Number %d: ",i+1);
       scanf("%f",&arr[i]);
    }
    for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
    {
       if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
           arr[0]=arr[i];
    }
    printf("Largest element = %.2f",arr[0]);
    return 0;
}

Output:
Enter total number of elements(1 to 100): 5
Enter Number 1: 54
Enter Number 2: 65
Enter Number 3: 45
Enter Number 4: 78
Enter Number 5: 99
Largest element = 99

22)To multiply two matrices using arrays.
Program:
        #include <stdio.h>
#include<conio.h>
void main()
{
  int a[3][3],b[3][3],c[3][3];
  int i,j,k,l = 1,m = 2;

  clrscr();
  printf("----Enter the first matrix A--------------\n");
     for(i=0;i<3;i++)
      {
          for(j=0;j<3;j++)
           {

            gotoxy(l,m);
                scanf("%d",&a[i][j]);
            l = l+6;

           }

           m =  m+1;
           l=1;
      }
  printf("------------Enter the first matrix B----\n")
 m = m+1;
    for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
        {
           gotoxy(l,m);
           scanf("%d",&b[i][j]);
           l = l+6;
        }
        m = m+1;
        l = 1;

      }

      for(i=0;i<3;i++)
      {
         for(j=0;j<3;j++)
          {
           c[i][j] = 0;
           for(k=0;k<3;k++)
          {
             c[i][j] += a[i][k] * b[k][j];
          }
          }
      }
    printf("\n\n--The productof two matrix------ \n\n" );
  for(i=0;i<3;i++)
  {
  for(j=0;j<3;j++)
  {
  printf("\t%d\t",c[i][j]);
  if(j==2)
  {
  printf("\n\n");
  }
  }
  }
getch();

}
       OUTPUT:
    ---------------Enter the first matrix A--------------
    1     2     3
    2     3     4
    3     4     5
    ---------------Enter the first matrix B--------------
    1     2     3
    3     4     5
    2     3     4
         ----------The product of two matrix---------------

    13              19              25

    19              28              37
         25              37              49


23)To concatenate two strings & store the result in the third string.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
     char str1[20],str2[20],str3[40]="";
     int len,i,j;
     clrscr();
     printf("***StringsConcatination Function*****\n\n");
     printf("Enter string1: ");
     scanf("%s",str1);
     printf("Enter string2: ");
     scanf("%s",str2);
     for(i=0,j=0;str1[i]!='\0';i++,j++)
     str3[j] = str1[i];

     for(i=0;str2[i]!='\0';i++,j++)
     str3[j] = str2[i];

     str3[j] = '\0';
     len = j;
          printf("\n\nAfter Concatination : %s",str3);
     printf("\n\nLength of concated string is %d",len);
     getch();
}
Output:   
******Strings Concatination Function*******
Enter string1: saiteja
Enter string2: Nagamothu
After  Concatenation :  Saiteja Nagamothu
Length After concated string is 16
24)To check whether the string is a palindrome without using string inbuilt functions.
Program:
#include <stdio.h>
#include <string.h>

int main()
{
   char text[10];
   int begin, middle, end, length = 0;
   printf(“enter a string:”);
   gets(text);

   while ( text[length] != '\0' )
      length++;

   end = length - 1;
   middle = length/2;

   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("Not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
      printf("Palindrome.\n");

   return 0;
}

Output:
enter a string:
wow
Palindrome


25) To check whether the string is a palindrome with using string inbuilt functions.
Program:
#include <stdio.h>
#include <string.h>

int main()
{
   char a[10], b[10];

   printf("Enter the string to check if it is a palindrome\n");
   gets(a);

   strcpy(b,a);
   strrev(b);

   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");

   return 0;
}

Output:
enter a string:
wow
Palindrome



26)To read & print the record of 5 students using structures.

Program:

#include <stdio.h>
struct student{
    char name[5];
    int roll;
    float marks;
};
int main(){
    struct student s[5];
    int i;
    printf("Enter information of students:\n");
    for(i=0;i<5;++i)
    {
        s[i].roll=i+1;
        printf("\nFor roll number %d\n",s[i].roll);
        printf("Enter name: ");
        scanf("%s",s[i].name);
        printf("Enter marks: ");
        scanf("%f",&s[i].marks);
        printf("\n");
    }
    printf("Displaying information of students:\n\n");
    for(i=0;i<5;++i)
    {
     printf("\nInformation for roll number %d:\n",i+1);
     printf("Name: ");
     puts(s[i].name);
     printf("Marks: %.1f",s[i].marks);
   }
   return 0;
}

Output:
Enter information of students:

For roll number :1
Enter name: teja
Enter marks: 98

For roll number :2
Enter name: shanu
Enter marks: 89

For roll number :3
Enter name: sahib
Enter marks: 80

For roll number :4
Enter name: srilekha
Enter marks: 99

For roll number:5
Enter name: prudhvi
Enter marks: 75

Displaying information of students:

Information for roll number 1:
Name: teja
Marks: 98

Information for roll number 2:
Name: sahnu
Marks: 89

Information for roll number 3:
Name: sahib
Marks: 80

Information for roll number 4:
Name: srilekha
Marks: 99

Information for roll number 5:
Name: prudhvi
Marks: 75



27)To implement the concept of union
Program:
#include <stdio.h>
#include <conio.h>

void main()
{
 union number
 {
  int  n1;
  float n2;
 };

 union number x;
 clrscr() ;
 printf("Enter the value of n1: ");
 scanf("%d", &x.n1);
 printf("Value of n1 =%d", x.n1);
 printf("\nEnter the value of n2: ");
 scanf("%d", &x.n2);
 printf("Value of n2 = %d\n",x.n2);

}

Output:
Enter the value of n1:2
Value of n1 =2
Enter the value of n2:2.1234

Value of n2 =2.1234

No comments:

Post a Comment