0% found this document useful (0 votes)
134 views27 pages

Program To Say Hello. ?: #Include #Include Void Main (CLRSCR Printf ("Hello") Getch )

The document contains C code snippets for various basic programs including: 1. Printing "Hello" 2. Swapping two numbers 3. Swapping two numbers without a third variable 4. Finding the reverse of a digit 5. Finding the sum of digits in a three digit number 6. Checking if a number is a palindrome 7. Checking if a number is an Armstrong number 8. Using switch case statements 9. Checking student performance based on percentage 10. Finding the factorial of a number

Uploaded by

Dheeraj Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views27 pages

Program To Say Hello. ?: #Include #Include Void Main (CLRSCR Printf ("Hello") Getch )

The document contains C code snippets for various basic programs including: 1. Printing "Hello" 2. Swapping two numbers 3. Swapping two numbers without a third variable 4. Finding the reverse of a digit 5. Finding the sum of digits in a three digit number 6. Checking if a number is a palindrome 7. Checking if a number is an Armstrong number 8. Using switch case statements 9. Checking student performance based on percentage 10. Finding the factorial of a number

Uploaded by

Dheeraj Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Program to say hello. ?

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello");
getch();
}

2. Program to print swapping of two number. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("\n After swaping Value of a is : %d",a);
printf("\n After swaping Value of b is : %d",b);
getch();
}

3. Program to swap two number without using 3rd variable. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swaping Value of a is : %d",a);
printf("\n After swaping Value of b is : %d",b);
getch();
}

4. Program to find reverse of a digit. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,n,rev=0;
clrscr();
printf("Enter The Value Of Number\n");
scanf("%d",&n);
a=n%10;
n=n/10;
b=n%10;
n=n/10;
c=n%10;
n=n/10;
d=n%10;
rev=a*1000+b*100+c*10+d*1;
printf("Reverse Number is = %d",rev);
getch();
}

5. Program to find sum of the digit of three digit number. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n,sum;
clrscr();
printf("Enter Three Digit Number : ");
scanf("%d",&n);
a=n%10;
b=(n/10)%10;
c=n/100;
sum=a+b+c;
printf("\nSum Of Three Digits of a Number is : %d",sum);
getch();
}

6. Program to check the number is palindrome or not. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,r=0;
clrscr();
printf("enter a number: ");
scanf("%d",&a);
c=a;
while(a>0)
{
b=a%10;
a=a/10;
r=r*10+b;
}
if(c==r)
printf("%d is a palindrome number",c);
else
printf("%d is not a palindrome number",c);
getch();
}

7. Program to check the number is Armstrong or not. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int n,a1,a2,a3,r;
clrscr();
printf("enter a number: ");
scanf("%d",&n);
a1=n%10;
a2=(n/10)%10;
a3=(n/100)%10;
r=(a1*a1*a1)+(a2*a2*a2)+(a3*a3*a3);
if(n==r)
printf("%d is an Armstrong number",n);
else
printf("%d is not an Armstrong number",n);
getch();
}

8. Program to use switch () cases. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int ch,a,b,c;
printf("1: Addition of 2 numbers\n2: Square of a number");
printf("\n3: Calculate (a+b)^2\n4: Calculate Modulas");
printf("\nEnter Your Choice (1-4)");
scanf("%d",&ch);
if(ch==1 || ch==3 || ch==4)
{
printf("Enter First Numbers: ");
scanf("%d",&a);
printf("Enter Second Numbers: ");
scanf("%d",&b);
}
switch (ch)
{
case 1: c=a+b;
printf("Sum of %d and %d is = %d",a,b,c);
break;
case 2: printf("Enter a Number: ");
scanf("%d",&a);
printf("Square of %d is %d",a,(a*a));
break;
case 3: c=(a+b)*(a+b);
printf("Result is %d",c);
break;
case 4: c=a%b;
printf("Result of %d%%d = %d",a,b,c);
break;
default: printf("Invalid Choice");
}
getch();
}

9. Program to check student performance in studies. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
printf("Enter the percentage of marks of a student = ");
scanf("%d" , & marks);
if (marks < 40)
printf ("\nStudent is fail");
else if (marks >=40 && marks < 50)
printf ("\nStudent got 3rd division");
else if (marks>=50 && marks <60)
printf ("\nStudent got 2nd division");
else if (marks >=60 && marks <75)
printf ("\n Student got 1st division");
getch();
}

[Link] to find factorial of number. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int f=1,n,copy;
printf("Enter Number: ");
scanf("%d",&n);
copy=n;
while(n>0)
{
f=f*n;
n--;
}
printf("Factorial of %d is %d",copy,f);
getch();
}

10

[Link] to find et salary after tax deduction. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
float basic,gross,net,da,med,hra,tax;
printf("Enter 1 for Male and 2 for Female: ");
scanf("%d",&ch);
if(ch==1 || ch==2)
{
printf("Enter Basic Salary: ");
scanf("%f",&basic);
da=(basic*50)/100;
med=(basic*15)/100;
hra=(basic*35)/100;
gross=basic+da+med+hra;
if(ch==2)
gross=gross+(gross*15)/100;
if(gross>600000)
tax=(gross*3)/100;
else if(gross>300000 && gross<=600000)
tax=(gross*1.5)/100;
else if(gross>250000 && gross<=300000)
tax=(gross*0.5)/100;
else
tax=0;
net=gross-tax;
printf("Gross Salary: Rs. %f",gross);
printf("Net Salary after Tax Deduction: Rs. %f",net);
}
else
printf(Invalid Choice);
getch();
}

11

[Link] to print logic pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=0;i<=4;i++)
{
for(j=0;j<=i;j++)
{
printf("$ ");
}
printf("\n");
}
getch();
}

12

[Link] to print a pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
for(i=0;i<=4;i++)
{
for(k=0;k<4-i;k++)
printf(" ");
for(j=0;j<=i;j++)
{
printf("$ ");
}
printf("\n");
}
getch();
}

13

[Link] to seacrch an item in array. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], n, item, loc=0, i;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("\nEnter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to search: ");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
loc=i;
}
if(loc==0)
printf("\nItem Not Found");
else
printf("\nItem Found at Location %d",loc+1);
getch();
}

14

[Link] to print a pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,n;
printf("Enter Number: ");
scanf("%d",&n);
if(n%2==0)
n--;
for(i=1;i<=n;i=i+2)
{
for(k=1;k<=(n-i);k++)
printf(" ");
l=1;
for(j=1;j<=i;j++)
{
printf("%d ",l);
if(j<(i/2)+1)
l++;
else
l--;
}
printf("\n");
}

for(i=n-2;i>=1;i=i-2)
{
for(k=1;k<=(n-i);k++)
printf(" ");
l=1;
for(j=1;j<=i;j++)
{
printf("%d ",l);
if(j<(i/2)+1)
l++;
else
15

l--;
}
printf("\n");
}
getch();
}

16

[Link] to print a logic pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=1;
for(i=0;i<=4;i++)
{
for(j=0;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
getch();
}

17

17. Program to print a logic pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=0;i<=4;i++)
{
for(j=0;j<=i;j++)
{
printf("%d ",j+1);
}
printf("\n");
}
getch();
}

18

[Link] to print a pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=0;
for(i=0;i<=4;i++)
{
if(i%2==0)
k=1;
else
k=0;
for(j=0;j<=i;j++)
{
printf("%d ",k);
if(k==1)
k--;
else
k++;
}
printf("\n");
}
getch();
}

19

[Link] to print a pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=0;i<=4;i++)
{
for(j=0;j<=i;j++)
{
printf("%d ",j+1);
}
printf("\n");
}
getch();
}

20

[Link] to print a pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l;
for(i=0;i<=4;i=i+2)
{
for(k=0;k<(4-i);k++)
printf(" ");
l=1;
for(j=0;j<=i;j++)
{
printf("%d ",l);
if(j<i/2)
l++;
else
l--;
}
printf("\n");
}
getch();
}

21

[Link] to print a pattern. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,x1,x2,x3;;
printf("Enter Size: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
x1=x3=i;
x2=(n-i)*2;
while(x1>0)
{
printf("$ ");
x1--;
}
while(x2>0)
{
printf(" ");
x2--;
}
while(x3>0)
{
printf("$ ");
x3--;
}
printf("\n\n");
}
getch();
}

[Link] to pint a pattern. ?


22

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
printf("Enter Number: ");
scanf("%d",&n);
if(n%2==0)
n--;
for(i=1;i<=n;i=i+2)
{
for(k=1;k<=(n-i);k++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("$ ");
}
printf("\n");
}

for(i=n-2;i>=1;i=i-2)
{
for(k=1;k<=(n-i);k++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("$ ");
}
printf("\n");
}
getch();

23

[Link] of binary search. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,item,beg,mid,end,loc=0;
printf("Enter size of Array: ");
scanf("%d",&n);
printf("Enter %d elements in ascending order: ",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter Element to Search: ");
scanf("%d",&item);
beg=1;
end=n;
while((beg<=end))
{
mid=(beg+end)/2;
if(item==a[mid])
{
loc=mid;
break;
}
else if(a[mid]>item)
end=mid-1;
else
beg=mid+1;
}
if(loc==0)
printf("%d not found ",item);
else
printf("%d found at location %d",item,loc);
getch();
}

24

[Link] of bubble sort. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,n,temp;
printf("Enter size of Array: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After Sorting: \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}

25

25. Program of insertion of item in array.?


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,loc,item;
printf("Enter size of Array: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter location where element is to be inserted: ");
scanf("%d",&loc);
printf("Enter the element: ");
scanf("%d",&item);
i=n;
while(i>=loc-1)
{
a[i+1]=a[i];
i--;
}
a[loc-1]=item;
n=n+1;
printf("After Insertion, New Array: \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}

26

[Link] of deletion in an array. ?


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,loc,item;
printf("Enter size of Array: ");
scanf("%d",&n);
printf("Enter %d elements: \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be deleted: ");
scanf("%d",&item);
for(i=0;i<=n;i++)
{
if(a[i]==item)
loc=i;
break;
}
while(loc<n)
{
a[loc]=a[loc+1];
loc++;
}
n--;
printf("After Deletion, New Array: \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}

27

You might also like