C Programs-Print Hello word

C Programs-Print Hello word

1.Print Hello word

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

printf(“Hello World”);

getch();

}

2. Program to assign values of two numbers and print their addition.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ans;
clrscr();
a=10;
b=20;
ans=a+b;
printf(“Addition is : %d”,ans);
getch();
}

3.Program to accept values of two numbers and print their addition.

include<stdio.h>
void main()
{
int a,b,ans;
clrscr();
printf(“Enter 1st number:”);
scanf(“%d”,&amp;a);
printf(“Enter 2nd number:”);
scanf(“%d”,&amp;b);
ans=a+b;
printf(“Addition is : %d”,ans);
getch();

}

4.Program to accept two values of a & b and swap their values.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf(“Enter 1st number: “);
scanf(“%d”,&amp;a);
printf(“Enter 2nd number: “);
scanf(“%d”,&amp;b);
printf(“\nBefore Swapping..\n A=%d, B=%d”,a,b);
temp=a;
a=b;

printf(“\nAfter Swapping..\n A=%d, B=%d”,a,b);

getch();

}

5.Program to accept two number and print largest among them.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter 1st number: “);
scanf(“%d”,&amp;a);
printf(“Enter 2nd number: “);
scanf(“%d”,&amp;b);
if(a&gt;b)
printf(“Largest value is: %d”,a);
else
printf(“Largest value is: %d”,b);

getch();

}

6. Program to accept a number and check whether the number is Positive, Negative or Zero.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter number: “);
scanf(“%d”,&amp;n);
if(n&gt;0)
printf(“Number is positive”);
else if(n&lt;0)
printf(“Number is negative”);
else
printf(“Number is Zero”);
getch();
}

7. Program to check whether the number is even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter number: );
scanf(“%d”,&amp;n);
if(n%2==0)
printf(“Number is even”);
else
printf(“Number is odd”);
getch();
}

8. Program to accept three numbers from user and print them in ascending and decending order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter numbers:”);
scanf(“%d%d%d”,&amp;a,&amp;b,&amp;c);
if((a&gt;=b)&amp;&amp;(a&gt;=c))
{
if(b&gt;=c)
{
printf(“\n Descending order:
%d %d %d”,a,b,c);
printf(“\n Ascending order :
%d %d %d”,c,b,a);
}
else
{
printf(“\n Descending order :
%d %d %d”,a,c,b);
printf(“\n Ascending order :
%d %d %d”,b,c,a);
}
}
else if((b&gt;=a)&amp;&amp;(b&gt;=c))
{
if(a&gt;=c)
{
printf(“\n Descending order :
%d %d %d”,b,a,c);
}
}
getch();
}

9. Program to accept a number from user and print it’s square & cube.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sqre,cube;
clrscr();
printf(“Enter Number: “);
scanf(“%d”,&amp;n);
sqre=n*n;
cube=n*n*n;
printf(“\nSquare: %d\nCube: %d”,sqre,cube);
getch();
}

10. Program to find the roots of a quadratic equation.

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

void main()
{
float a, b, c, determinant, real;
double r1, r2, imag;
printf(“Enter coefficients a, b and c: “);
scanf(“%f%f%f”, &amp;a, &amp;b, &amp;c);

determinant = b * b – 4 * a * c;
if (determinant > 0)
{
r1 = (-b + sqrt(determinant)) / (2 * a);
r2 = (-b – sqrt(determinant)) / (2 * a);
printf(“Roots are: %.2f and %.2f”, r1, r2);
}
else if (determinant == 0)
{
r1 = r2 = -b / (2 * a);
printf(“Roots are: %.2f and %.2f”, r1, r2)
}
else
{
real = -b / (2 * a);
imag = sqrt(-determinant) / (2 * a);
printf(“Roots are: %.2f+%.2fi and %.2f-%.2fi”, real, imag, real, imag);
}
getch();
}

11. Program to accept rollnumber and marks of three subjects from user and print total marks, average and grade.

#includestdio.h>
#include<conio.h>
void main()
{
int RollNum, m1, m2, m3, total;
float avg;
clrscr();
printf(“Enter Roll Number : “);
scanf(“%d”,&amp;RollNum);
printf(“Enter marks for three subjects : “);
scanf(“%d%d%d”, &amp;m1, &amp;m2, &amp;m3);

total=m1+m2+m3;
avg=total/3.0;

printf(“\nTotal is : %d”, total);
printf(“\nAverage is : %5.2f”, avg);

if(avg>80)
printf(“\nGrade : A”);
else if((avg>60)&&(avg<=80))
printf(“\nGrade : B”);
else if((avg>40)&&(avg<=60))
printf(“\nGrade : C”);
else if((avg>=33)&&(avg<=40)
printf(“\nGrade : D”);
else
printf(“\nGrade : Fail”);

getch();
}

12. Program to accept value of radius and print area of a circle.

#include<stdio.h>
#include<conio.h>
void main()
{
float area,radius;
clrscr();
printf(“Enter Radius:”);
scanf(“%f”,&amp;radius);
area=3.14*radius*radius;
printf(“Area of the given radius is : %6.2f”,area);
getch();
}

13. Program to print numbers from 1 to n using while loop.

#includestdio.h>
#include<conio.h>
void main()
{
int i=1, n;
clrscr();
printf(“Enter n : “);
scanf(“%d”, &amp;n);
while(i&lt;=n)
{
printf(“%d\t”,i);
i++;
}
getch();

}

14. Program to print numbers from n to 1 using Do While loop.

#includestdio.h>
#include<conio.h>
void main()
{
int i=1, n;
clrscr();
printf(“Enter n : “);
scanf(“%d”, &amp;n);
i=n;
do
{
printf(“%d\t”,i);
i–;
}while(i&gt;=1);
getch();
}

15. Program to print first n even numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=2, n;
//to print odd numbers set variable i=1
clrscr();
printf(“Enter n : “);
scanf(“%d”, &amp;n);
while(i&lt;=n)
{
printf(“%d\t”,i);
i=i+2;
}
getch();
}

16. Program to accept a number and print that number in reverse order.
Ex:- 1024
Output:- 4201

#include<stdio.h>
#include<conio.h>
void main()
{
int reminder, n;
clrscr();
printf(“Enter n : “);
scanf(“%d”, &amp;n);
while(n&gt;0)
{
reminder=n%10;
printf(“%d”, reminder);
n=n/10;
}
getch();
}

17. Program to accept a number and print sum of it’s digits.

#include<stdio.h>
#include<conio.h>
void main()
{
int reminder, sum=0, n;
clrscr();
printf(“Enter n : “);
scanf(“%d”, &amp;n);
while(n&gt;0)
{
reminder=n%10;
sum=sum+reminder;
n=n/10;
}
printf(“Sum of digits : %d”,sum);
getch();
}

18. Program to take a number from user and check whether it is Armstrong number or not.

#includestdio.h>
#include<conio.h>
void main()
{
int i=2, temp, rem, sum=0, n;
clrscr();
printf(“Enter n : “);
scanf(“%d”, &amp;n);
temp = n;

while(n>0)
{
rem = n%10;
sum = sum+(rem*rem*rem);
n = n/10;
}

if(temp==sum)
printf(“Entered number is an Armstrong Number”);
else
printf(“Entered number is not an Armstrong Number”);
getch();
}

19.Program to take number from user and print table of that number.

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
clrscr();
printf(“Enter number : “);
scanf(“%d”, &amp;n);
for(i=1; i&lt;=10; i++)
printf(“%d × %d = %d\n”, n, i, n*i);
getch();
}

21. Pattern 1


• •
• • •
• • • •
• • • • •

#includestdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf(Enter number : “);
scanf(“%d”, &amp;n);
for(i=1; i&lt;=n; i++)
{
for(j=1; j&lt;=i; j++)
{
printf(“• “);
}

printf(“\n”);
}
getch();
}

22. Pattern 2

• • • • •
• • • •
• • •
• •

#includestdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf(“Enter number : “);
scanf(“%d”, &amp;n);
for(i=n; i&gt;=1; i–)
{
for(j=1;j&lt;=i;j++)
{
printf(“• “);
}
printf(“\n”);
}
getch();
}

23. Pattern 3


• •
• • •
• • • •
• • • • •

#include<stdio.h>

#include<conio.h>
void main()
{
char ch = ‘*’;
int n;
int i, j, no_of_spaces = 0, spaceCount;

printf(“Enter number : “);
scanf(“%d”, &n);
printf(“\n”);
no_of_spaces = n – 1;
for (i = 1; i <= n; i++)
{
for (spaceCount = no_of_spaces; spaceCount >= 1; spaceCount–)
{
printf(” “);
}
for (j = 1; j <= i; j++)
{
printf(“%2c”, ch);
}

printf(“\n”);
no_of_spaces–;
}

getch();

}

Tags: , ,

Leave a Reply