Write a program to find the sum of the series 1/2 – 3/4 + 5/6 – 7/8 + …….. upto n terms.

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

void main()
{
int i,n;
float sum=0;
clrscr();
printf("Enter number of termsn");
scanf("%d",&n);
if(n<=0)
printf("Invalid numbern"); //to check if valid positive integer is entered
else
{
for(i=1;i<=n;i++) //to keep a check on the count of n numbers
{
  if(i%2==1) //if it is an odd term number then add the term to the sum
    sum=sum+float(2*i-1)/(2*i);     //numerator of every ith term is (2*i-1) and denominator is (2*i)
  else  //else if it is an even term number then subtract the term from the sum
    sum=sum-float(2*i-1)/(2*i);
}
printf("Answer=%fn",sum);
}
getch();
}

 

Write a program to display the following pattern.

*

*    *

*    *    *

*    *

*

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

void main()
{
int i,j,k;
clrscr();

for(i=1;i<=3;i++) //to keep count on first three rows
{
  for(k=1;k<=2*(3-i);k++)
    printf(" ");  //to print initial spaces
  for(j=1;j<=i;j++)
    printf("*   "); //to print stars
    /* after every * there are 3 spaces */
  printf("n"); //to go to next row
}

for(i=2;i>=1;i--) //to keep count on the last 2 rows
{
 for(k=1;k<=2*(3-i);k++)
   printf(" ");     //to print initial spaces
 for(j=1;j<=i;j++)
   printf("*   ");  //to print stars
    /* after every * there are 3 spaces */
 printf("n");  //to go to next row
}
getch();
}

 

Write a program to display n lines of the pattern.

1

2  3

4  5  6

7  8  9  10

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

void main()
{
int i,j,n,k=1;
clrscr();
printf("Enter the number of lines to be printedn");
scanf("%d",&n);

for(i=1;i<=n;i++) //to keep count of rows
{
  for(j=1;j<=i;j++,k++)   //print characters in each row
    printf("%d  ",k); //k is the number to be printed
  printf("n");  //to take pointer to next row
}

getch();
}

 

Write a program to display n lines of the pattern.

A

B B

C C C

D D D D

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

void main()
{
int i,j,k,n;
clrscr();

printf("Enter the number of linesn");
scanf("%d",&n);    //input number of lines required

for(i=1;i<=n;i++) //to keep count of no. of rows
{
  for(k=1;k<=2*(n-i);k++) //to print initial spaces
    printf(" ");
  for(j=1;j<=i;j++)
    printf("%c   ",char(i+64)); //to print each character
  printf("n");   //to take pointer to next line
}
getch();
}

 

Write a program to display the following pattern.

A B C D

A B C

A B

A

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

void main()
{
int i,j,k,m=0;
clrscr();

for(i='D';i>='A';i--) //to keep count of rows
{
  for(k=m;k>0;k--)  //to print spaces
    printf(" ");
  for(j='A';j<=i;j++)   //print characters in each row
    printf("%2c",j);
  m=m+2;
  /* no. of spaces in 1st row=0
     no. of spaces in 2nd row=2
     no. of spaces in 3rd row=4
     no. of spaces in 4th row=6
     hence m is incremented by 2 in each iteration */
  printf("n");  //to take pointer to next row
}

getch();
}

 

Write a program to generate prime numbers between 1 to 100.

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

void main()
{
int i,n;
clrscr();

for(n=2;n<100;n++) //check numbers between 1 to 100
{
  i=2;
  while(n%i!=0) //check if the number n is divisible by any integer i starting from i=2
    i++;
  if(i==n)
  /* when an integer i is obtained such that n is divisible
  by i we exit from while loop and if the value of i is equal to n then
  n is a prime number...this is because a prime number is divisible only
  by 1 and itself */

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

 

Write a program that reads a character from the user. If the character is uppercase letter then it should print the corresponding lowercase letter and vice-versa and if it is any other character then it should be printed as it is.

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

void main()
{
  char c;
  clrscr();
  printf("Enter a charactern");
  c=getchar(); //get character from user
  if(c>='A' && c<='Z')
    putchar(c+32); //add 32 to get ascii value of corresponding lowercase letter

  else
    if(c>='a' && c<='z')
      putchar(c-32); //subtract 32 to get ascii value of corresponding uppercase letter

    else
      putchar(c); //if character is neither lowercase nor uppercase letter then print the character as it is

getch();
}

 

Write a program that reads a character from the user and displays message whether it is a vowel or consonant or digit or any other character.

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

void main()
{
  char c;
  clrscr();
  printf("Enter a charactern");
  c=getchar(); //get character from the user

  if(c>='A' && c<='Z' || c>='a' && c<='z') //to determine if the character is an alphabet
    if(c=='A'||c=='a'||c=='E'||c=='e'||c=='I'||c=='i'||c=='O'||c=='o'||c=='U'||c=='u')
      //if alphabet is a,e,i,o,u it is a vowel
      printf("%c is a voweln",c);
    else   //else it is a consonant
      printf("%c is a consonantn",c);

  else  //if the character is not an alphabet it check if it is a digit or other character
    if(c>='0' && c<='9')    //if character lies between 0 to 9 then it is a digit
      printf("%c is a digitn",c);
    else
      printf("%c is an other charactern",c);

getch();
}

 

Write a menu driven program that displays the following menu:- 1 Addition 2 Subtraction 3 Multiplication 4 Division 5 Quit The program should read option from user repeatedly and perform corresponding operation until user opts for quit.

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

void main()
{
int n1,n2,choice;

do
{
  clrscr();
  printf("1. Additionn");          //User menu
  printf("2. Subtractionn");
  printf("3. Multiplicationn");
  printf("4. Divisionn");
  printf("5. Quitn");
  printf("Enter your choicen");   //accept choice from user
  scanf("%d",&choice);
  if(choice>=1 && choice<=4)
  {
    printf("Enter two integersn");
    scanf("%d%d",&n1,&n2);
    switch(choice)
    {
      case 1 : printf("%dn",n1+n2); break; //perform addition
      case 2 : printf("%dn",n1-n2); break; //perform subtraction
      case 3 : printf("%dn",n1*n2); break; //perform multiplication
      case 4 : if(n2!=0) printf("%fn",(float)n1/n2); //if denominator is not zero then perform division
         else  printf("Divide by zero errorn"); //if denominator is zero then report divide by zero error
         break;
    }
    getch();
  }
}
while(choice!=5); //perform the do-while loop repeatedly till user does not opt to exit
}