0% found this document useful (0 votes)
15 views

Programming

The document provides examples of C programs that demonstrate various input/output functions and control structures in C including print, scanf, getchar, putchar, if/else conditional statements, for loops, and calculating sums of series. The programs cover basic concepts like reading user input, outputting text, comparing values, and iterating through ranges of numbers. Examples include programs to read and display names, numbers, characters, calculate results, sums, and display series of even or odd numbers. Control flow structures like if/else and for loops are used to make conditional checks and repeat blocks of code.

Uploaded by

Milan Oli
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming

The document provides examples of C programs that demonstrate various input/output functions and control structures in C including print, scanf, getchar, putchar, if/else conditional statements, for loops, and calculating sums of series. The programs cover basic concepts like reading user input, outputting text, comparing values, and iterating through ranges of numbers. Examples include programs to read and display names, numbers, characters, calculate results, sums, and display series of even or odd numbers. Control flow structures like if/else and for loops are used to make conditional checks and repeat blocks of code.

Uploaded by

Milan Oli
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

A.

C USING INPUT / OUTPUT FUNCTION


1. /*to print Hello class 12*/
#include<stdio.h>
#include<conio.h>
void main()
{
Printf(“\nHello Class 12”);
getch();
}

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();
}

B. C USING CONTROL FUNCTION

1. /* to input mark in a subject and to find his/her result*/


#include<stdio.h>
#include<conio.h>
void main()
{
int m;
printf(“\nEnter the mark in a subject”);
scanf (“%d”, &m);
if(m>=35)
{
printf (“\nThe result is pass”);
}
else
{
printf(“\nThe result is fail”);
}
getch()
}

2. /*to input any two numbers and find greatest number*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2;
prinf (“\nEnter any two numbers”);
scanf (“%d %d”, &n1, &n2);
if(n1>n2)
{
prinf (“\nThe bigger number is %d”, n1);
}
else
{
Printf (“\nThe bigger number is %d”, n2);
}
getch();
}

3. /*to input a number and find whether it is exactly divisible by 5 or not*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n, r;
printf (“\nEnter a number”);
scanf (%d’, &n);
r=n%5;
if (r==0)
{
printf (“\n%d is exactly divisible by 5” , n);
}
else
{
printf(“\n%d is not exactly divisible by 5”, n);
}
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();
}

7. /*to input a number and find whether it is >=100 or not*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
printf (“\nEnter a number”);
scanf (“%d”, &n1);
if (n1>=100)
{
printf (“\n%d is >=100”, n1);
}
else
{
printf (“\n %d is not >=100”, n1);
}
getch();
}
8. /*to input monthly sales amount of a sales person and find his/her commission amount*/
#include<stdio.h>
#include<conio.h>
void main()
{
float sa, ca;
printf (“\nEnter the monthly sales of the sales person”);
scanf (“%f”, &sa);
if (sa>=1000)
{
ca=sa*10/100;
}
else
{
ca=sa*5/100l
}
printf (“\nThe commission amount of the sales person=Rs. %f”, ca);
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();
}

11. /*to swap two value by using XOR(^) operator*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
printf (“\nEnter two numbers a and b:”);
scanf (“%d%d”, &a, &b);
printf (“\nBefore swapping: a = %d and b = %d\n”, a, b);
a=a^b;
b=b^a;
a=a^b;
printf (“After swapping: a = %d and b = %d”, a, b);
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();
}

14. /*to calculate his or her commission amount*/


#include<stdio.h>
#include<conio.h>
void main()
{
float sa, ca;
printf (“\nEnter the monthly sales amount of:”);
scanf (“%f”, &sa);
if (sa<=5000)
{
ca=sa*5/100;
}
else if (sa<=1000)
{
ca=sa*10/100;
}
else
{
ca=sa*15/100;
}
printf (“nThe commission amount is Rs. %f”, ca);
getch()
}

15. /*to display the series: 1,2,3,4,5,……100*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf (“\nThe reuired series is”);
for (i=0; i<=100; i++)
{
Printf (“%d\t”,i);
}
getch();
}

16. /*to display the series:1,2,3……….100 and find the sum*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum;
printf (“\nThe required series is :”);
sum=0;
for (i=0; i<=100; i++)
{
printf (“%d\t”, i);
sum+=i; //sum=sum+i;
}
printf (“\nThe sum of the series=%d”,sum);
getch();
}

17. /*to display the series of even number upto 100*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf (“\nThe series of even number is: ”);
for (i=2; i<=100; i++)
{
printf (“%d\t”, i);
}
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();
}

21. /*to convert decimal to binary number*/


#include<stdio.h>
#include<conio.h>
void main()
{
int I, k, a, n;
printf (“\nEnter any decimal number:”);
scanf (“%d”, &n);
printf (“\nBinary in 16 bits is: \n”);
{
a=1<<I;
k=n&a;
k=0?printf (“0”) :printf (“1”);
}
getch();
}

22. /*to display the series of square number up to 100*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf (“\nThe series of square number is”);
for (i=0;i<=10;i++)
{
Printf (“%d\t”,i*i);
}
getch();
}

23. /*to display the following series up to 10 th terms*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i, a;
printf (“\nThe series of cube number is”);
for (i=0;i<=10;i++)
{
a=i*i*i;
printf (“%d\t”, a);
}
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();
}

26. /*to display all prime numbers upto 1000*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,j,i;
for (j=1;j<=1000;j++)
{
for (i=2;i<=j;i++)
{
if (j%i==0)
{
break;
}
}
if (i==j)
printf (“\t%d”,j);
}
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();
}

29. /*to input integer and reverse it*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i,r,n,x;
printf (“Enter a number”);
scanf (“%d”, &n);
if (n>0);
{
x=0;
while (n>0)
{
r=n%10;
x=x*10=r;
n=n/10;
}
printf (“\nThe reverse number is=%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();
}

You might also like