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

Basic C Programming

The documents contain C program code written by Rishi Singh to demonstrate various programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, data types etc. The programs are commented and include sample input/output to show the expected behavior of each program. The programs cover basics of C programming like simple print statement, addition of two numbers, finding circumference of circle, checking eligibility, identifying even/odd numbers and more advanced concepts like swapping values, finding roots of quadratic equation, printing pyramid pattern.

Uploaded by

Rishi Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Basic C Programming

The documents contain C program code written by Rishi Singh to demonstrate various programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, data types etc. The programs are commented and include sample input/output to show the expected behavior of each program. The programs cover basics of C programming like simple print statement, addition of two numbers, finding circumference of circle, checking eligibility, identifying even/odd numbers and more advanced concepts like swapping values, finding roots of quadratic equation, printing pyramid pattern.

Uploaded by

Rishi Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

/*WAP TO PRINT A SIMPLE STATEMENT*/

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
printf(“This is my first C program”);
}

/*OUTPUT*/

This is my first C program

BY: - RISHI SINGH


/*WAP TO ADD TWO INTEGERS*/

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

/*OUTPUT*/

43

BY: - RISHI SINGH


/*PROGRAM TO FIND THE CIRCUMFERENCE OF CIRCLE*/

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr() ;
float pi=3.14,circum;
int r;
printf(“Enter the radius”);
scanf(“%d”,&r) ;
circum=2*pi*r;
printf(“The circumference is=%f”,circum) ;
getch() ;
}

/*OUT PUT*/

RADIUS OF CIRCLE= 4
CIRCUMFERENCE OF CIRCLE IS = 25.120001

BY: - RISHI SINGH


/*WAP TO CHECK ELIGIBILITY*/

#include<stdio.h>
#include<conio.h>
void main()
{ int age;
clrscr();
printf (“Enter age”);
scanf(“%d”,&age);
if(age>18)
{
printf(“Eligible for voting”) ;
}
else
printf(“Not eligible for voting”) ;
getch() ;
}

/*OUTPUT*/

Enter age =17


Not eligible for voting

BY: - RISHI SINGH


/*WAP TO CHECK NUMBER IS EVEN OR ODD*/

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

void main()
{
int ival , remainder;
clrscr() ;
printf(“Enter the integer”) ;
scanf(“%d” , &ival) ;
remainder=ival%2 ;
if(remainder==0)
printf(“%d,is an even integer”,ival);
else
printf(“%d, is an odd integer”, ival) ;
getch ();
}

/*OUTPUT*/

Enter the integer 20


20 is an even integer

BY: - RISHI SINGH


/*WAP TO GENERATE SUM, DIFFERENCE, PRODUCT, QUOTIENT */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr() ;
int nl,n2 ;
int sum,difference,product,quotient;
n1=5 ;
n2=3 ;
sum=n1+n2 ;
difference=n1-n2 ;
product=n1*n2 ;
quotient=n1/n2 ;
printf(“sum=%d\n” ,sum) ;
printf(“difference=%d\n” ,difference) ;
printf(product=%d\n” ,product) ;
printf(‘quotient=%d\n” ,quotient) ;
getch() ;
}

/*OUTPUT*/

sum=8
difference=2
product=15
quotient=1

BY: - RISHI SINGH


/*WAP TO FIND OUT THE LARGEST THREE NOS, USING CONDITION AND
BRANCHING*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr() ;
printf(“Enter three no”) ;
scanf(“%d%d%d” ,&a,&b,&c) ;
if ( (a==b)&& (b==c) )
{
printf(“All are equal”) ;
}
else
{
if(a>b&&a>c)
{
printf(“%d is largest”,a) ;
}
else if(b>c &&b>a)
{
printf(“%d is largest “,b) ;
}
else
{
printf(“%d is largest “,c) ;

BY: - RISHI SINGH


}
}
getch() ;
}

/*OUTPUT*/

Enter three no
45
50
30
50 is largest

BY: - RISHI SINGH


/*WAP TO SWAP TWO NUMBERS USING CALL BY VALUE*/

#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter the numbers\n");
scanf("%d%d",&a,&b);
swap(a,b);
printf("a=%d and b=%d",a,b);
getch();
}
void swap(int p,int q)
{
int s;
s=p;
p=q;
q=s;
printf("Swaped numbers are %d and %d\n",p,q);
}

OUTPUT

Enter the numbers: 7, 45


Swapped numbers: 45 and 7
a=7 and b=45

BY: - RISHI SINGH


/*WAP TO FIND FACTORIAL OF NUMBER USING DO WHILE LOOP*/

#include<stdio.h>
#include<conio.h>
void main()
{
int f=1,num;
clrscr();
printf("enter a number");
scanf("%d",&num);
do
{
f=f*num;
num--;
}
while(num>=1);
printf("factorial=%d",f);
getch();
}

OUTPUT
Enter A Number5
Factorial=120

BY: - RISHI SINGH


/*WAP TO FIND FACTORIAL OF A NUMBER*/

#include<stdio.h>
#include<conio.h>
void main()
{
int f,num;
clrscr();
printf("enter a number\n");
scanf("%d",&num);
while(num<=1)
{
f=f*num;
num--;
}
printf("factorial=%d",f);
getch();
}

OUTPUT

Enter a Number: 6
Factorial=1256

BY: - RISHI SINGH


/*WAP TO FIND LARGEST AMONG THREE NUMBERS*/

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf("Enter three number\n");
scanf("%f%f%f",&a,&b,&c);
if((a==b)&&(a==c))
printf("Three number are equal");
else
{
if((a>=b)&&(a>=c))
printf("Greater number=%f",a);
else
{
if((b>a)&&(b>c))
printf("%f is largest",b);
else
printf("%f is largest",c);
}
}
getch();
}
OUTPUT:
Enter three numbers: 4, 6, 7
7 is largest

BY: - RISHI SINGH


/*WAP TO CHECK WHETHER YEAR IS LEAP YEAR OR NOT*/

#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a YEAR\n");
scanf("%d",&year);
if((year%100)==0)
{
if((year%400)==0)
{
printf("LEAP YEAR");
}
else
{
printf("NOT a LEAP YEAR");
}
}
else
{
if((year%4)==0)
{
printf("LEAP YEAR");
}
else
{

BY: - RISHI SINGH


printf("NOT LEAP YEAR");
}
}
getch();
}

OUTPUT
Enter A YEAR: 2011
NOT LEAP YEAR

BY: - RISHI SINGH


/*WAP TO INPUT AN ALPHABET AND CHECK, IT IS UPPER CASE OR NOT*/

#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr();
printf("Enter the character\n");
scanf("%c",&a);
if((a>=65)&&(a<=90))
{
printf("upper case");
}
else
{
if((a>=97)&&(a<=122))
printf("lower case");
}
getch();
}

OUTPUT

Enter the Character: g


Lower Case

BY: - RISHI SINGH


/*WAP TO CONVERT TEMPERATURE, DEGREE CELSIUS TO DEGREE
FAHRENHEIT*/
/* TO CONVERT TEMPERATURE CONVERSION */

#include<stdio.h>
#include<conio.h>
void main( )
{
float c,f=0;
clrscr();
printf("enter temperture in celcius");
scanf("%f",&c);
f=(c*9)/5+32;
printf("temperature in f=%f",f);
getch( );
}

OUTPUT

Enter temperature in Celsius: 25


Temperature in f=77

BY: - RISHI SINGH


/*WAP TO PRINT TABLE OF 2*/

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,p;
for(i=1;i<=10;i++)
{
p=2*i;
printf("\n2*%d=%d",i,p);
}
getch();
}

OUTPUT

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

BY: - RISHI SINGH


/*WAP TO INPUT AN OPERATOR (+) FOR ADDITION, (-) FOR
SUBTRACTION*/

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,sum,diff;
char ch;
printf("Enter two number\n");
scanf("%f%f",&a,&b);
printf("enter any operater+or-\n");
scanf("%c",&ch);
switch(ch)
{
case'+':
sum=a+b;
printf("sum=%f",sum);
break;
case'-':
diff=a-b;
printf("Difference=%f',diff);
break;
default:printf("invalid choice");
}
getch();
}

BY: - RISHI SINGH


OUTPUT

Enter Two Numbers:


5
6
Enter Any Operator + Or -
+
11

BY: - RISHI SINGH


/*WAP TO SWAP TWO NUMBERS USING CALL BY REFERENCE*/

#include<stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int a,b;
clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("a=%d and b=%d",a,b);
getch();
}
void swap(int* p,int* q)
{
int z;
z=*p;
*p=*q;
*q=z;
printf("Swaped values are %d and %d\n",*p,*q);
}

OUTPUT
Enter two numbers: 5 and 9
Swapped values are 9 and 5
a=9 and b=5

BY: - RISHI SINGH


/*WAP TO SWAP TWO NUMBER WITHOUT USING THIRD VARIABLE*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter two number");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("swaped numbers=%d,%d",a,b);
getch();
}

OUTPUT

Enter Two Numbers:


57
56
Swapped Numbers= 56, 57

BY: - RISHI SINGH


/*WAP TO FIND THE SUM OF THE SERIES*/
1/1! + 1/2! + 1/3! + .. ..1/n! */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float sum=0.0;
float p,f=1.0;
clrscr();
printf("Enter number of terms\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
p=1.0/f;
sum=sum+p;
}
printf("SUM=%f",sum);
getch();
}

OUTPUT
Enter number of terms: 10
SUM=1.718282

BY: - RISHI SINGH


/*WAP TO FIND SUM OF DIGITS OF A NUMBER*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num,s=0,a=0;
printf("enter a number");
scanf("%d",&num);
while(num!=0)
{
a=num%10;
s=s+a;
num=num/10;
}
printf("sum of digit=%d",s);
getch();
}

OUTPUT

Enter a Number: 567


18

BY: - RISHI SINGH


/*WAP TO FIND THE ROOTS OF QUADRATIC EQUATION*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x=0,y=0,s=0,d=0;
printf("enter variable");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf("eq. is not quadratic");
else
{
d=(b*b)-(4*a*c);
if(d<0)
printf("roots are imagenary");
else
{
if(d==0)
x=-b/2*a;
y=-b/2*a;
printf("roots are equal=%f%f",x,y);
{
if(d>0)
s=sqrt(d);
x=(-b+s)/(2*a);
y=(-b-s)/(2*a);

BY: - RISHI SINGH


printf("roots are=%f%f",x,y);
}
}
}
getch();
}

OUTPUT

Enter Variable
4
6
9
Roots Are Imaginary

BY: - RISHI SINGH


/*WAP TO PRINT THE PYRAMID OF STARS*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*\t");
}
printf("\n");
}
getch();
}

OUTPUT

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

BY: - RISHI SINGH


/*WAP TO INPUT A NUMBER AND CHECK WHETHER PRIME OR NOT*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
int i;
int c=0;
clrscr();
printf("Enter the Number\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if((num%i)==0)
{
c++;
}
}
if(c<=2)
{
printf("Number is PRIME");
}
else
{
printf("Number is NOT PRIME");
}
getch();
}

BY: - RISHI SINGH


OUTPUT:

ENTER THE NUMBER:


34
NUMBER IS NOT PRIME

BY: - RISHI SINGH

You might also like