Programming
Programming
2. /*to show the use of scanf() function to read a number, store and display it*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(“\nEnter any number to store in a:”);
scanf(“%d”, &a);
printf(“\n You entered %d for keyboard”,a);
getch();
}
3. /*to show the use of getchar() and putchar() function*/
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf(“\nEnter any character:”);
c=getchar();
printf(“\nYou entered the following character from keyboard:”);
putchar(c);
getch();
}
4. /*to show the use of getche() and getch()*/
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf(“\nEnter any character using of getche():”);
c=getche();
printf(“\nYou entered following character from keyboard:”);
putch(c);
printf(“\nEnter any character using of getch():”);
c=getch();
printf(“\nYou entered following character from keyboard:”);
putch(c);
getch();
}
5. /*to read and display your name by using gets() and puts() function*/
#include<stdio.h>
#include<conio.h>
void main()
{
char name[50];
printf(“\nEnter your name:”);
gets(name);
printf(“\nHi,” );
puts(name);
printf(“You are Welcome in C”);
getch();
}
6. /* to read some words in one paragraph and display them*/
#include<stdio.h>
#include<conio.h>
void main()
{
char text[100];
printf (“\nEnter one paragraph of text:”);
//scanf (“%[^\n]”, text); also read one paragraph
gets (text);
printf (“\nYou entered :\n”);
puts (text);
getch();
}
4. /*to input a number and find whether it is exactly divisible by 5 and 7 both or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, r1, r2;
printf (“\nEnter a number”);
scanf (“%d”, &n);
r1=n%5;
r2=n%7;
if (r1==0 && r2==0)
{
printf (“\n%d is exactly divisible by 5 and 7 both”,n);
}
else
{
printf (“\n%d is not exactly divisible by 5 and 7 both”,n);
}
getch();
}
5. /*to input any two numbers and find they are equal or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2;
printf (“\nEnter any two numbers”);
scanf (“%d %d”, &n1, &n2);
if (n1==n2)
{
printf (“\nThe numbers are equal”);
}
else
{
printf (“\nThe numbers are not equal”);
}
getch();
}
6. /*to input any two numbers and find the smaller number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2;
printf (“\nEnter any two numbers”);
scanf (“%d %d”, &n1, &n2);
if (n1<n2)
{
printf (“\nThe smaller number is %d”, n1);
}
else
{
printf (“\nThe smaller number is %d”, n2);
}
getch();
}
9. /*to input the no. of books that are sold and rate of a book and find the total amount,
discount amount and net amount*/
#include<stdio.h>
#include<conio.h>
void main()
{
int nob;
float r, ta, da, na;
printf (“\nEnter the number of books that are sold”);
scanf (“%d”, &nob):
printf (“\nEnter the rate of a book”);
scanf (“%f”, &ra);
ta=nob*r;
if (nob>=1000)
{
da=ta*10/100;
}
else
{
da=ta*5/100;
}
na=ta-da;
printf (“\nThe total amount= Rs. %f”, ta);
printf (“\nThe Discount amount= Rs. %f”, da);
printf (“\nThe net amount= Rs. %f”, na);
getch();
}
10. /*to input three different numbers and find the biggest number by using C language*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
printf (“\nEnter any numbers”);
scanf (“%d%d%d”, &n1, &n2, &n3);
if (n1>n2 && n1>n3)
{
printf (“\nThe biggest number is %d”, n1);
}
else if (n2>n1 && n2>n3)
{
printf (“\nThe biggest number is %d”, n2);
]
else
{
printf (“\nThe biggest number is %d”, n3);
}
getch();
}
12. /*to check the given number is positive or negative or equal to 0*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
printf (“\nEnter a number”);
scanf (“%d”, &n1);
if(n1>0)
{
printf (“\nThe given number is positive: n1);
}
else if (n1<0)
{
printf (“\nThe given number is negative”);
}
else
{
printf (“\nThe number is 0”);
}
getch();
}
13. /*to input sales price(SP) and cost price(CP) and to find gain or loss*/
#include<stdio.h>
#include<conio.h>
void main()
{
flaot sp, cp, amt;
printf (“\nEnter the sales price (SP) of an item”);
scanf (“%d”, &sp);
printf (“\nEnter the cost price (CP) of an item”);
scanf (“%d”, &cp);
if (sp>cp)
{
amt=sp-cp;
printf (“\nProfit amount =Rs. %f”, amt);
}
else if (sp<cp)
{
amt=cp-sp;
printf (“\nLoss amount=Rs. %f”, amt);
}
else
{
printf (“\nThe transaction has neither profit or loss”);
}
getch();
}
18. /*to display the series of even numbers upto 20 th term. 2, 4, 6……..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a;
printf (“\nThe series fo even number up to 20 th term is”);
a=2;
for (i=1; i<=20; i++)
{
printf (“%d\t”,a);
a=a+2;
}
getch();
}
19. /*to display the even number upto Nth term. 2 4 6……..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a, n;
printf (“\nEnter the value of Nth term”);
scanf (“%d”, &n);
printf (“the series fo even number upto %dth term “,n);
a=2;
for (i=1; i<=n; i++)
{
printf (“%d\t”,a);
a=a+2;
}
getch();
}
20. /* to display the series of odd number upto 100 in descending order and to find out their
sum too*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, s;
printf (“\nThe series of even number is”);
s=0;
for (i=99; i>=1; i=i-2)
{
printf (“%d\t”,i);
s=s+i;
}
printf (“\n the sum=%d”,s);
getch();
}
24. /*to input a positive number and find out it is a perfect square number or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a, n;
printf (‘Enter a number”);
scanf (“%d”, &n);
for (i=0;i<=n;i==)
{
a=i*I;
if (a==n)
{
printf (“\nThe number %d has perfect square.”);
break;
}
}
if (i>n)
printf (“\nThe numbe %d do not have perfect square.”);
getch();
}
25. /*to input positive integer and find it is prime number or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, r,i;
printf (“\nEnter a number”);
scanf (“%d”,&n);
for (i=2;i<n;i++)
{
if (n%i==0)
{
printf (“%d is not prime”,n);
break;
}
}
if (i==n)
printf (‘\n% is prime”,n);
getch();
}
27. /*to display all perfect square numbers from 100 to 500*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf (“\nThe square numbers between 100 and 500 are”);
for (n=100;n<=500;n++)
{
for (i=1;i<=n;i++)
{
if (n==i)
printf (“%d X %d = %d\n”, i,i,n);
}
}
getch();
}
28. /*to input a positive integer and find its sum of digits*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,r,n,x;
printf (“Enter a number”);
scanf (“%d”, &n);
if (n>o)
{
x=0;
while (n>0)
{
r=n%10;
x=x+r;
n=n/10;
}
printf (“\nThe sum of its digits =%d”,x)
}
else
{
printf (“\nIt is not a positive number”);
}
getch();
}
30. /*to input a positive integer and find it is polyndrome number of not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,r,n,x,a;
printf (“Enter a number”);
scanf (“%d”,&n);
a=n;
if (n>0)
{
x=0;while (n>0)
{
r=n%10;
x=x*10+r;
n=n/10;
}
if (a==x)
{
printf (“\n Yes, %d is palindrome number”,a);
}
else
{
printf (“\nNo, %d is not palindrome number”,a);
}
else
{
printf (“\nIt is not a positive number”);
}
getch();
}