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

Basic C Programs

Uploaded by

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

Basic C Programs

Uploaded by

funfor340
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PSNA COLLEGE OF ENGINEERING & TECHNOLOGY, DINDIGUL

DEPARTMENT OF COMPUTER APPLICATIONS (MCA)


MC2181 – ADVANCED DATASTRUCTURES & ALGORITHM LAB

BASIC C PROGRAMS
(A prerequisite for ADSA lab)

1. C Program to implement arithmetic operation


#include<stdio.h>
int main()
{
int a,b,c;
float x;
printf("\nEnter 2 Nos : ");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nTotal : %d",c);
c=a-b;
printf("\nDifference : %d",c);
c=a*b;
printf("\nMul : %d",c);
x=(float)a/(float)b;
printf("\nDiv : %0.2f",x);
c=a%b;
printf("\nMod : %d",c);
return 0;
}

2. C program to find the greatest of two numbers


include<stdio.h>
int main()
{
//Fill the code
int num1, num2;
scanf(“%d %d”,&num1,&num2);
if(num1 > num2)

Dr.A.Vanitha Katharine, ASP/MCA 1|Page


{
printf(“%d is greater”,num1);
}
else
{
printf(“%d is greater”,num2);
}
return 0;
}

3. C program to use iteration using for loop


#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
printf(“%d”,i);
printf(“program is done”);
return 0;
}

4. C program to swap two numbers without using functions and


pointers
#include<stdio.h>
int main()
{
int a,b,temp;
printf(“enter the values:”);
scanf(“%d%d”,&a,&b);
printf(“before swap:”%d%d”,a,b);
if(a==b)
printf(“the same values have been entered!”);
else
{
temp=a;
a=b;
b=temp;

Dr.A.Vanitha Katharine, ASP/MCA 2|Page


printf(“after swap:%d%d”,a,b);
}
return 0;
}

5. C program to get the sum of digits in a number


#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1,sum=0;
clrscr();
printf(“\n Enter a number:”);
scanf(“%d”,&n);
while(n!=0)
{
n1=n%10;
sum=sum+n1;
n=n%10;
}
printf(“\n sum of digits in a number is :%d”,sum);
getch();
}

6. C program to get reverse a number


int main() {
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}

Dr.A.Vanitha Katharine, ASP/MCA 3|Page


7. C program to get reverse a number
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i) {


// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}

8. C program to get input and output of an array of size 10


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];i;
clrscr();
printf(“enter the values”);
for(i=0;i<10;i++)

Dr.A.Vanitha Katharine, ASP/MCA 4|Page


{
printf(“enter the values of a[%d]\n”,i);
scanf(“%d”,&a[i]);
}
for(“i=0;i<10;i++)
{
printf(“entered values is a[%d]=%d\n”,i,a[i]);
}
getch();
}

9. C program to do matrix multiplication:


#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

Dr.A.Vanitha Katharine, ASP/MCA 5|Page


printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

10. C program to print without semicolon


#include <stdio.h>
int main()
{
if(printf(“hello”))
{ }
}

11. C program to run without main


#include <stdio.h>
#define start main
void start()
{
printf(“you”);
}

Dr.A.Vanitha Katharine, ASP/MCA 6|Page


12. C program to print list of numbers
#include <stdio.h>
void main()
{
int n=10; THIS IS USING WHILE LOOP
while(n>0)
{
printf(“%d”,n);
n--;
}

#include <stdio.h>
void main()
{
int n=10; THIS IS USING FOR LOOP
for (i=10;i>0;i--);
printf(“%d”,i);}

13. C program to print pyramid of numbers using for loop


#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

14. C program to print if leap year or not


#include <stdio.h>
int main() {
int year;

Dr.A.Vanitha Katharine, ASP/MCA 7|Page


printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}

15. C program to print factorial of a number


#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;

Dr.A.Vanitha Katharine, ASP/MCA 8|Page


}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}

16. C program to print palindrome of a number


#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}

17. C program to print fibonacci series


#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int f1 = 0, f2 = 1;
// initialize the next term (3rd term)
int nextTerm = f1 + f2;
// get no. of terms from user
printf("Enter the number of terms: ");

Dr.A.Vanitha Katharine, ASP/MCA 9|Page


scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", f1, f2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
f1 = f2;
f2 = nextTerm;
nextTerm = f1 + f2;
}
return 0;
}

18. C program to print armstrong number


#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number


originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}

Dr.A.Vanitha Katharine, ASP/MCA 10 | P a g e

You might also like