Feb 26, 2014

Programs of C Programming

1. A Program to print "Himalaya College of Engineering" for 10 times using while loop.

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

int i=1;
while (i<=10)
    {
            printf("Himalaya College of Engineering\n");
i++;
    }
getch();
}

Output
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering





2. A Program to print "Himalaya College of Engineering" for 10 times using do while loop.

#include<stdio.h>
#include<conio.h>
void main ()
{
int i=1;
do
    {
            i++;
            printf("Himalaya College of Engineering\n");
    }
while (i<=10);
getch();
}


Output
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering
Himalaya College of Engineering





3. A program that evaluates following series.

y=1+x2+x3+……..+xn.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=2,x,y=1,n;
printf("Enter the value of x\n");
scanf("%d",&x);
printf("Enter the value of n\n");
scanf("%d",&n);
while (i<=n)
{
y=y+pow(x,i);
i++;
}
printf("y=%d\n",y);
getch();
}

Output:
Enter the value of x
2
Enter the value of n
3
y=13





4. A program that evaluates following series.y=x+x2+x3+……..+xn


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=1,x,y=0,n;
printf("Enter the value of x\n");
scanf("%d",&x);
printf("Enter the value of n\n");
scanf("%d",&n);
while (i<=n)
{
y=y+pow(x,i);
i++;
}
printf("y=%d\n",y);
getch();
}


Output:
Enter the value of x
3
Enter the value of n
3
y=39





5. A program that evaluates following series.y=1 + 1/x + 1/x2 + 1/x3+……..+1/xn


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=1,x,n;
float y=1;
printf("Enter the value of x\n");
scanf("%d",&x);
printf("Enter the value of n\n");
scanf("%d",&n);
while (i<=n)
{
y=y+(1/pow(x,i));
i++;
}
printf("y=%0.3f\n",y);
getch();
}


Output:
Enter the value of x
3
Enter the value of n
3
y=1.481



6. A Program that asks user to input two integer numbers and also asks to input operators and performs their sum if operator is "+", subtraction if operator is "–", product if operator is "*", modulo division if operator is "%" otherwise displays invalid operator. Use if_else_if and switch case.

Using switch case
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b;
char ch;
printf("Enter two integer\n");
scanf("%d%d",&a,&b);
printf("Press '+' for addition\nPress '-' for subtraction\nPress '*' for product\nPress '%%' for modulo division\n");
ch=getche();
switch (ch)
{
case '+':
printf("\nThe addition of two number is %d\t",a+b);
break;
case '-':
printf("\nThe subtraction of the two number is %d\t",a-b);
break;
case '*':
printf("\nThe Product of numbers is %d\t",a*b);
break;
case '%':
printf("\nThe modulo division is %d\t",a%b);
break;
default:
printf("\nInvalid Operator");
}
getch();
}
Output





Using if…else…if case:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b;
char ch;
printf("Enter two integer\n");
scanf("%d%d",&a,&b);
printf("Press '+' for addition\nPress '-' for subtraction\nPress '*' for product\nPress '%%' for modulo division\n");
ch=getch();
if (ch=='+')
printf("\nThe addition of two number is %d\t",a+b);
else if (ch=='-')
printf("\nThe subtraction of the two number is %d\t",a-b);
else if(ch=='*')
printf("\nThe Product of numbers is %d\t",a*b);
else if(ch=='%')
printf("\nThe modulo division is %d\t",a%b);
else
printf("\nInvalid Operator");
getch ();
}


Output:



  

7. A Program with menu drivers that asks user to input two integer numbers and performs:1.     Addition of two numbers.
2.     Subtraction of second number by first.
3.     Displays message "CSIT-FIRST"
Perform by switch case               

#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c;
printf("Enter two integers\n");
scanf("%d%d",&a,&b);
printf("Press 1 for addition of two number\nPress 2 for subtraction of 2nd from 1st \nPress 3 for the message\n");
scanf("%d",&c);
switch (c)
{
case 1:
printf("The addition of two number is %d \t",a+b);
break;
case 2:
printf("The subtraction of second num. from first is %d\t",b-a);
break;
case 3:
printf("CSIT-FIRST");
break;
default:
printf("Try again");
}
getch();
}



Output:







8. A  Program that displays 7 days of week when entered the number from 1-7.

#include<stdio.h>
#include<conio.h>
void main ()
{
int a;
printf("Enter the integer\n");
scanf("%d",&a);
switch (a)
{
case 1:
printf("The day is Sunday");
break;
case 2:
printf("The day is Monday");
break;
case 3:
printf("The day is Tuesday");
break;
case 4:
printf("The day is Wednesday");
break;
case 5:
printf("The day is Thursday");
break;
case 6:
printf("The day is Friday");
break;
case 7:
printf("The day is Saturday");
break;
default:
printf("Input correct integer");
}
getch();
}

Output:



 9. A Program that asks user to input two number and displays all numbers that lies between the two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i;
printf("Enter two numbers.\n");
scanf("%d%d",&m,&n);
printf("\n\nIntegers between these two numbers are:\n");
for (i=m;i<=n;i++)
printf("%d  ",i);
getch();
}
  

Output:



10. A Program that asks user to input two number and displays even numbers that lies between the two numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i;
printf("Enter two numbers.\n");
scanf("%d%d",&m,&n);
printf("\n\nEven integers between these two numbers are:\n");
for (i=m;i<=n;i++)
    if (i%2==0)
printf("%d  ",i);
getch();
}



Output :


11. Display the series:1          4             9 …………..… 144


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=1,j;
do
{
       j=i*i;
       i++;
printf("%d    ",j);
}
while (i<=12);
getch();
}

  
Output:
1    4    9   16   25   36   49   64   81   100   121   144



No comments:

Post a Comment

Don't be shy. Leave your comment here.