Programs
Programs
By
#include <stdio.h>
#include <conio.h>
main()
{
printf("\n\n MY JOB ADDRESS IS");
printf("\n------------------------------------------------");
printf("\n\n L.D.R.P.-ITR,\n Gandhinagar.");
printf("\n------------------------------------------------");
}
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d,e;
float avg;
printf("\n\nEnter the five number:===>");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
avg=(float)(a+b+c+d+e)/5;
printf("\n\n\tAverage ===> %f",avg);
}
#include<stdio.h>
#include<conio.h>
main()
{
float p=3.14,r,area,rarea,l,w;
printf("\n\n Enter the Radious For Circle:===>");
scanf("%f",&r);
printf("\n\n Enter the Length and Width For Rectangle:===>");
scanf("%f %f",&l,&w);
area=p*r*r;
rarea=l*w;
printf("\n\n Area Of Circle ====> %f",area);
printf("\n\n Area Of Rectangle ====> %f",rarea);
}
#include<stdio.h>
#include<conio.h>
main()
{
long int year,minute;
printf("\n\nEnter The Year:===>");
scanf("%ld",&year);
minute=year*365*24*60;
printf("Minute Of %ld Year ====> %ld",year,minute);
}
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf(""\n\nEnter the Character:==>"");
scanf(""%c"",&ch);
printf("Entered Character Is:==>%c",ch);
}
#include<stdio.h>
#include<conio.h>
main()
{
char lowerChar, upperChar;
int ascii;
printf("Enter a lowercase Character: ");
scanf("%c", &lowerChar);
ascii = lowerChar;
upperChar = ascii-32;
printf("\nIts Uppercase = %c", upperChar);
}
Prepared by: Dr. Kiran Patel
8. Write a program to swap the values of two variables using
third variable.
#include<stdio.h>
#include<conio.h>
main()
{ int a,b,temp;
printf("ENTER THE VALUE OF A & B:===>");
scanf("%d %d",&a,&b);
temp=a;
a=b;
b=temp;
printf("\t\t\n\n USING THIRD VARIABLE");
printf("\t\t\nVALUE OF A IS:===>%d",a);
printf("\t\t\nVALUE OF B IS:===>%d",b);
}
Prepared by: Dr. Kiran Patel
9. Write a program to swap the values of two variables
without using third variable.
#include<stdio.h>
#include<conio.h>
main() {
int a,b;
printf("ENTER THE VALUE OF A & B:===>");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\t\n\n WITHOUT THIRD VARIABLE");
printf("\t\t\nVALUE OF A IS:===>%d",a);
printf("\t\t\nVALUE OF B IS:===>%d",b);
} Prepared by: Dr. Kiran Patel
10.Write a program to find maximum and minimum numbers from two
numbers by using conditional operator.
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("ENTER THE VALUE OF A:===>");
scanf("%d",&a);
printf("ENTER THE VALUE OF B:===>");
scanf("%d",&b);
if(a>b)
{
printf("\n %d IS LARGEST",a);
printf("\n %d IS SMALLEST",b);
}
else if(b>a)
{
printf("\n %d IS LARGEST",b);
printf("\n %d IS SMALLEST",a);
}
else
printf("\n %d and %d both are EQUAL",a,b);
} Prepared by: Dr. Kiran Patel
11. Write a program to demonstrate bitwise operator.
#include<stdio.h>
#include<conio.h>
main() {
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
// The result is 00000001
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b); //Bitwise and
#include <stdio.h>
main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
#include <stdio.h>
main()
{
int counter=1;
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
START:
printf("%d ",counter);
counter++;
if(counter<=n)
goto START;
} Prepared by: Dr. Kiran Patel
19. Write a program to print addition of first n numbers by
using go to statement.
#include <stdio.h>
#include<conio.h>
main()
{
int counter=1;
int n;
int sum=0;
//enter the value of n (range)
printf("Enter the value of n: ");
scanf("%d",&n);
START:
printf("%d ",counter);
sum=sum+counter;
counter++;
if(counter<=n)
goto START;
printf(" The sum is %d", sum);
} Prepared by: Dr. Kiran Patel
20. Write a program to find reverse of given numbers.
(For example 132-231)
#include <stdio.h>
main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
} Prepared by: Dr. Kiran Patel
21. Write a program to check whether entered number is
Armstrong or not.
#include <stdio.h>
// 3**3 + 7**3 + 1**3 = 371
main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
} Prepared by: Dr. Kiran Patel
22. Write a program to check whether entered number is
palindrome or not.
#include <stdio.h>
// Same when u read Forward and Backward
main()
{
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
} Prepared by: Dr. Kiran Patel
23. Write a program to print factorial of a given number.
#include <stdio.h>
main() {
int n, i;
int fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %d", n, fact);
}
}
Prepared by: Dr. Kiran Patel
24. Write a program to check whether entered number is
#include <stdio.h>
prime or not.
main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
} Prepared by: Dr. Kiran Patel
25. Write a program to print Different pattern using For Loop.
1
12
123
1234
12345
#include<stdio.h>
main()
{
int i,j,n;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
Prepared by: Dr. Kiran Patel
}
1
22
333
#include<stdio.h> 4444
main() 55555
{
int i,j,n;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
} Prepared by: Dr. Kiran Patel
*****
****
#include<stdio.h> ***
**
main() *
{
int i,j,n;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
} Prepared by: Dr. Kiran Patel
}
1
#include<stdio.h>
main()
12
{ 123
int i,j,k,n; 1234
printf("Enter the value of N:");
scanf("%d",&n); 12345
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
{
printf(" "); /* For extra Spaces to print*/
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
}
Prepared by: Dr. Kiran Patel
*
#include<stdio.h> **
main()
{
***
int i,j,k,n; ****
printf("Enter the value of N:"); *****
scanf("%d",&n);
for(i=1;i<=n;i++)
****
{ ***
for(j=1;j<=i;j++) **
{
printf("*");
*
}
printf("\n");
}
for(i=n-1;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
Prepared by: Dr. Kiran Patel
}
27. Write a program to print 1 to 5 numbers using array.
#include<stdio.h>
main()
{
int i,a[5];
for(i=0;i<5;i++)
{
a[i]=i+1;
}
for(i=0;i<5;i++)
{
printf("%d\t",a[i]);
}
}
Prepared by: Dr. Kiran Patel
27. Write a program to print 1 to 5 reverse numbers using array.
#include<stdio.h>
main()
{
int i,a[5];
for(i=0;i<5;i++)
{
a[i]=5-i;
}
for(i=0;i<5;i++)
{
printf("%d\t",a[i]);
}
}
Prepared by: Dr. Kiran Patel
28. Write a program to find sum and average of five numbers.
#include<stdio.h>
main()
{
int i,a[5],sum=0;
float average;
for(i=0;i<5;i++)
{
printf("Enter Value:");
scanf("%d",&a[i]);
sum=sum+a[i];
}
average=sum/5.0;
printf("Sum=%d\tAverage=%f",sum,average);
}
Prepared by: Dr. Kiran Patel
29. Write a program to find maximum and minimum number from given array.
#include<stdio.h>
main() {
int i,a[100],n,max,min;
#include<stdio.h>
main() {
int i,a[100],n,pos=0,neg=0,z=0;
#include<stdio.h>
main() {
int i,a[100],n,odd=0,even=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
even++;
else
odd++;
}
printf("Odd=%d\tEven=%d",odd,even);
Prepared by: Dr. Kiran Patel
}
32. Write a program to sort given n number using array.
#include<stdio.h> main() {
int i,j,k,a[100],n,temp;
printf("How many numbers?");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter Value:");
scanf("%d",&a[i]); }
for(i=0;i<n;i++)
{ k=i;
for(j=i+1;j<n;j++)
{ if(a[j]<a[k])
{
k=j; // Find index of the minimum
} }
if(k!=i)//Replace it.
{ temp=a[i];
a[i]=a[k];
a[k]=temp;
} }
printf("After sorting:\n");
for(i=0;i<n;i++)
{ printf("%d\n",a[i]);
} } Prepared by: Dr. Kiran Patel
33. Write a program to read matrix, display original and transpose of matrix.
main() {
{ for(j=0;j<c;j++)
int i,j,a[10][10],r,c; {
printf("How many rows?");
scanf("%d",&r); printf("%d",a[i][j]);
printf("How many columns?"); }
scanf("%d",&c); printf("\n");
for(i=0;i<r;i++) }
{ printf("Transpose of Matrix:\n");
for(j=0;j<c;j++) for(i=0;i<c;i++)
{ {
printf("Enter Value:"); for(j=0;j<r;j++)
scanf("%d",&a[i][j]); {
} printf("%d",a[j][i]);
} }
printf("Original Matrix:\n"); printf("\n");
for(i=0;i<r;i++) } }
#include<stdio.h>
int main()
{
char str[20];
int len;
printf("Enter any name:");
scanf("%s",str);
printf("\nString is ==>%s",str);
for(len=0;str[len]!='\0';)
len++;
printf("\n\nLength of '%s' is ==>%d",str,len);
return 0;
}
#include<stdio.h>
int main()
{
char str[20],str1[20];
int i;
printf("Enter any name:");
scanf("%s",str);
printf("\nString is ==>%s",str);
for(i=0;str[i]!='\0';i++)
str1[i]=str[i];
str1[i]='\0'; //put null character at the end of string
printf("\n\nAfter coping name1 into name2");
printf("\nName 1 is ==>%s",str);
printf("\nName 2 is ==>%s",str1);
return 0;
}
for(i=0;(str[i]!='\0')&&(str[i]==str1[i]);i++);
if(str[i]=='\0'&&str1[i]=='\0')
printf("\n\nBoth name are same\n");
else
printf("\n\nBoth name are not same\n");
return 0;
}
Prepared by: Dr. Kiran Patel
38. Write a program to find given string is palindrome or not.
#include<stdio.h>
#include<string.h>
int main()
{
char str[20],str1[20];
int i;
printf("Enter First name:");
gets(str);
strcpy(str1,str);
strrev(str1);
printf("\nName is ==>%s",str);
printf("\nReverse Name is ==>%s",str1);
for(i=0;(str[i]!='\0')&&(str[i]==str1[i]);i++);
if(str[i]=='\0'&&str1[i]=='\0')
printf("\n\nName is Palindrome\n");
else
printf("\n\nName is not Palindrome\n");
return 0;
} Prepared by: Dr. Kiran Patel
39. Write a program to convert a given string into upper case string.
#include<stdio.h>
#include<ctype.h>
int main()
{
char str[20],str1[20];
int i;
printf("Enter any name:");
gets(str);
printf("\nString is ==>%s",str);
for(i=0;str[i]!='\0';i++)
{
str1[i]=toupper(str[i]);
}