C Program for Different Pattern

Question 1:

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

#include<stdio.h>
void main()
{
int i,j,n;
printf(“Enter the Number of lines:”);
scanf(“%d”,&n);
for(i=n;i>=1;i–)
{
for(j=n;j>=i;j–)
printf(“*”);
printf(“\n”);
}
}

Question 2:

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

#include<stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i–)
{
for(j=i;j>1;j–)
printf(“*”);
printf(“\n”);
}
}

Question 3:

*
*      *
*      *      *
*     *       *     *
*      *      *      *      *
#include<stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i–)
{
for(j=1;j<=i;j++)
printf(” “);
for(j=5;j>=i;j–)
printf(“* “);
printf(“\n”);
}
}

Question 4:

A
AB
ABC
ABCD
ABCDE

#include<stdio.h>
void main()
{
int i,j;
for(i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
printf(“%c”,j);
printf(“\n”);
}
}

Question 5:

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

#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
printf(” “);
for(j=0;j<(2*i+1);j++)
printf(“*”);
printf(“\n”);
}
}

 

 

 

 

 

Advertisements

Some important C Programs

//Write a C Program to convert lower case to upper case and vice versa

#include<stdio.h>
void main()
{
char ch;
printf(“Enter the Character:”);
scanf(“%c”,&ch);
if(islower(ch))
printf(“%c”,toupper(ch));
else
printf(“%c”,tolower(ch));

}

//Write a C Program to display the ASCII value with characters

#include<stdio.h>
void main()
{
int Num;
for(Num=0;Num<=255;Num++)
printf(“%d=%c\n”,Num,Num);
}

//Write a program to calculate the factorial of a Numbers

#include<stdio.h>
void main()
{
int fact=1,Num,i;
printf(“Enter the Number:”);
scanf(“%d”,&Num);
for(i=Num;i>1;i–)
fact=fact*i;
printf(“Factorial of %d is %d”,Num,fact);
}

//Write a program to swap a two number without using extra variables

#include<stdio.h>
void main()
{
int a,b;
printf(“\nEnter two numbers:”);
scanf(“%d %d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“a=%d,b=%d”,a,b);
}

//Write a program to get the salary and gender as a input and calculate the bonus 7% for male and 10 % for female . In addition to that 5% extra bonus for the employee salary less than 10000

#include<stdio.h>
void main()
{
float salary,bonus=0;
char gen;
printf(“Enter the salary and gender:”);
scanf(“%f %c”,&salary,&gen);
if(gen==’M’ || gen==’m’)
bonus=bonus+salary*0.07;
else
bonus=bonus+salary*0.10;
if(salary<10000)
bonus=bonus+salary*0.05;
salary=salary+bonus;
printf(“Salary=%f, Bonus=%f”,salary,bonus);
}