[go: up one dir, main page]

0% found this document useful (0 votes)
44 views14 pages

CIA Program

The document contains multiple C programs demonstrating various programming concepts such as printing numbers, checking conditions, displaying odd/even numbers, accepting strings, performing arithmetic operations, and implementing algorithms like sorting and calculating factorials. Each program includes user input prompts and outputs based on the logic implemented. The examples cover a range of topics suitable for beginners learning C programming.

Uploaded by

pushpaappavu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views14 pages

CIA Program

The document contains multiple C programs demonstrating various programming concepts such as printing numbers, checking conditions, displaying odd/even numbers, accepting strings, performing arithmetic operations, and implementing algorithms like sorting and calculating factorials. Each program includes user input prompts and outputs based on the logic implemented. The examples cover a range of topics suitable for beginners learning C programming.

Uploaded by

pushpaappavu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

write a C program to print numbers from 1 to N using an array?

#include <stdio.h>
int main() {
int i,size,n;
printf("Enter size of array :");
scanf("%d",&size); //input size of the array
int array[size]; //initialize array with size
printf("Enter Numbers to be printed :");
scanf("%d",&n); //input N integers to be printed
for(i=1;i<=n;i++){
array[i] = i; // set consecutive integers at i+1 position with each iteration
printf("%d\n",array[i]); // for loop to print the elements in the array
}
return 0;
}

Write a C Program to check whether the given input is equal to 36.

#include<stdio.h>
Void main()
{
Int a=36;
If (a==36)
Printf(“the given number is equal to 36”);
else
{
Printf(“the given number is not equal to 36”);
}

Write a C Program to display the odd numbers from 1 to n. Get the value of n from
the user.

#include <stdio.h>

int main()
{
int i, n;

/* Input upper limit from user */


printf("Print odd numbers till: ");
scanf("%d", &n);

printf("All odd numbers from 1 to %d are: \n", n);

/* Start loop from 1 and increment it by 1 */


for(i=1; i<=n; i++)
{
/* If 'i' is odd then print it */
if(i%2!=0)
{
printf("%d\n", i);
}
}

return 0;
}
Write a C code to display your department name for n number of times. Get the value
of n from the user.

#include <stdio.h>
int main()
{
int n, i;
char dept[20];
printf("Enter your department name: ");
scanf("%s", dept);
printf("Enter the number of times to display: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("%s\n", dept);
}
return 0;
}

Write a C program to accept string through keyboard using gets() function.

#include <stdio.h>

int main()
{
char str[100];
printf("Enter a string: ");
gets(str);
printf("You entered: %s", str);
return 0;
}

output

Enter a string: senthil


You entered: senthil

#include <stdio.h>

int main()
{
int a,b,sum,diff,prod;float div;double modulo;
printf("Enter the number for a :: \n");
scanf("%d",&a);
printf("Enter the number for b :: \n");
scanf("%d",&b);
sum=a + b;
diff=a-b;
prod=a*b;
div=a/b;
modulo=a % b;
printf("sum = %d ",sum);
printf("diff = %d ",diff);
printf("prod = %d ",prod);
printf("div = %f ",div);
printf("modulo= %lf ",modulo);
return 0;
}

Enter the number for a ::


5
Enter the number for b ::
3
sum = 8 diff = 2 prod = 15 div = 1.000000 modulo= 2.000000

#include <stdio.h>
float fahrenheit_to_celsius(float f)
{
return ((f - 32.0) * 5.0 / 9.0);
}
int main()
{
float f = 40;
printf("Temperature in Degree Celsius : %0.2f",fahrenheit_to_celsius(f));
return 0;
}

Temperature in Degree Celsius : 4.44

#include<stdio.h>
void main()
{
float x=1.9,y=2.4;
int a,b;
printf("Value Of x:%f\n",x);
printf("Value Of y:%f\n",y);
a = (int)(x+0.5);
b = (int)(y+0.5);
printf("Value Of a:%d\n",a);
printf("Value Of b:%d\n",b);
}

output

Value Of x:1.900000
Value Of y:2.400000
Value Of a:2
Value Of b:2

#include <stdio.h>
int main()
{
int radius;
float PI = 3.14, area, circumference;
printf("Enter the radius of circle: ");
scanf("%d", &radius);
area = PI * radius * radius;
printf("The Area of circle is: %f", area);
circumference = 2 * PI * radius;
printf("\nThe Circumference of circle is: %f", circumference);
return 0;
}

Output
Enter the radius of circle: 2

The Area of circle is: 12.560000


The Circumference of circle is: 12.560000

#include <stdio.h>

int main()
{
int i, n;
printf("Print odd numbers till: ");
scanf("%d", &n);
printf("All odd numbers from 1 to %d are: \n", n);
for(i=1; i<=n; i++)
{
if(i%2!=0)
{
printf("%d\n", i);
}
}

return 0;
}

#include<stdio.h>
void main()
{
int number;
float temp, sqrt;
printf("number: \n");
scanf("%d", &number);
sqrt = number / 2;
temp = 0;
while(sqrt != temp)
{
temp = sqrt;
sqrt = ( number/temp + temp) / 2;
}
printf("%f",sqrt);
}
output

number:
4
2.000000

#include<stdio.h>
int main()
{
int i, number;
printf("\n Please Enter the Maximum Limit Value : ");
scanf("%d", &number);
printf("\n Even Numbers between 1 and %d are : \n", number);
for(i = 1; i <= number; i++)
{
if ( i % 2 == 0 )
{
printf(" %d\t", i);
}
}
return 0;
}

Please Enter the Maximum Limit Value : 7


Even Numbers between 1 and 7 are :
2 4 6

#include <stdio.h>
int main()
{
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
else if (B >= A && B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
return 0;
}

Enter the numbers A, B and C: 3 5


6
6 is the largest number.

#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}

Enter an integer: 4
4 is even.

#include <stdio.h>

int main() {
int monthNumber;

printf("Enter a month number (1-12): ");


scanf("%d", &monthNumber);

switch (monthNumber) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 12.\n");
break;
}

return 0;
}

output

Enter a month number (1-12): 5


May

#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 5 == 0 && number % 3 != 0 && number % 7 != 0) {
printf("%d is divisible by 5 and not divisible by 3 and 7.\n", number);
} else {
printf("%d is not divisible by 5 or it's divisible by 3 or 7.\n", number);
}
return 0;
}

Enter an integer: 4
4 is not divisible by 5 or it's divisible by 3 or 7.
7

#include <stdio.h>
int main()
{
int marks;
printf("Enter the student's marks: ");
scanf("%d", &marks);
if (marks > 90) {
printf("O Grade\n");
} else if (marks >= 80 && marks <= 89) {
printf("A+ Grade\n");
} else if (marks >= 70 && marks <= 79) {
printf("A Grade\n");
} else if (marks >= 60 && marks <= 69) {
printf("B+ Grade\n");
} else if (marks >= 55 && marks <= 59) {
printf("B Grade\n");
} else if (marks >= 50 && marks <= 54) {
printf("C Grade\n");
} else {
printf("RA or Fail\n");
}
return 0;
}

Enter the student's marks: 56


B Grade

#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("The given character is a vowel.\n");
} else {
printf("The given character is a consonant.\n");
}
return 0;
}

Enter a character: r
The given character is a consonant.

#include <stdio.h>

int main() {
int n;
long long factorial = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (int i = 1; i <= n; i++) {
factorial *= i;
}

printf("Factorial of %d = %lld\n", n, factorial);


}

return 0;
}

Enter a non-negative integer: 5


Factorial of 5 = 120
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series up to %d terms: \n", n);
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d", next);
if (i < n - 1) {
printf(", ");
}
}
printf("\n");
return 0;
}

Enter the number of terms: 5


Fibonacci Series up to 5 terms:
0, 1, 1, 2, 3

#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {


int originalNum, remainder, result = 0, n = 0;

originalNum = num;

// Count the number of digits in the number


while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;

// Check if it is an Armstrong number


while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if (result == num)
return 1; // It is an Armstrong number
else
return 0; // It is not an Armstrong number
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isArmstrong(num)) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}

return 0;
}

#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter a positive integer n: ");


scanf("%d", &n);

if (n < 1) {
printf("Please enter a positive integer.\n");
} else {
for (int i = 2; i <= n; i += 2) {
sum += i;
}

printf("Sum of even numbers from 1 to %d is: %d\n", n, sum);


}

return 0;
}

#include <stdio.h>

int main() {
int num, reversedNum = 0, originalNum, remainder;

printf("Enter an integer: ");


scanf("%d", &num);

originalNum = num;

// Reverse the number


while (num > 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}

if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}

return 0;
}

#include <stdio.h>
int main() {
int number, sum = 0;
printf("Enter a number: ");
scanf("%d", &number);
int originalNumber = number; // Store the original number for later reference
while (number > 0) {
int digit = number % 10;
sum += digit;
number /= 10;
}
printf("The sum of the digits of %d is: %d\n", originalNumber, sum);
return 0;
}

#include <stdio.h>

int main() {
int n;

printf("Enter the number of rows: ");


scanf("%d", &n);

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}

return 0;
}

#include <stdio.h>

void bubbleSort(int arr[], int n) {


int temp;
int swapped;
for (int i = 0; i < n - 1; i++) {
swapped = 0;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}

// If no two elements were swapped in the inner loop, the array is already
sorted.
if (swapped == 0) {
break;
}
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Sort the array in ascending order


bubbleSort(arr, n);

printf("Sorted array in ascending order:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}

#include <stdio.h>
int main() {
int numbers[5];
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}
printf("Even/Odd Status:\n");
for (int i = 0; i < 5; i++) {
if (numbers[i] % 2 == 0) {
printf("%d is even\n", numbers[i]);
} else {
printf("%d is odd\n", numbers[i]);
}
}

return 0;
}

#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
printf("The smallest number in the array is: %d\n", smallest);
return 0;
}

#include <stdio.h>

void bubbleSortDescending(int arr[], int n) {


int temp;

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
// Swap arr[j] and arr[j+1] for descending order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int linearSearch(int arr[], int n, int target) {


for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Element found, return its index
}
}
return -1; // Element not found
}

int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Sort the array in descending order


bubbleSortDescending(arr, n);

printf("Sorted array in descending order:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

int target;
printf("\nEnter the element to search: ");
scanf("%d", &target);

int result = linearSearch(arr, n, target);

if (result != -1) {
printf("%d found at index %d.\n", target, result);
} else {
printf("%d not found in the array.\n", target);
}

return 0;
}

You might also like