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

CPPM_Journal[1]

The document contains multiple C programming tasks including checking for palindromes, replacing words in sentences, calculating areas of various shapes, performing arithmetic operations, checking for prime and Armstrong numbers, displaying Fibonacci series, sorting arrays, printing patterns, counting characters in strings, and summing digits of numbers. Each task is accompanied by its respective code and output. The document is intended for educational purposes, specifically for students in a Computer Programming course.

Uploaded by

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

CPPM_Journal[1]

The document contains multiple C programming tasks including checking for palindromes, replacing words in sentences, calculating areas of various shapes, performing arithmetic operations, checking for prime and Armstrong numbers, displaying Fibonacci series, sorting arrays, printing patterns, counting characters in strings, and summing digits of numbers. Each task is accompanied by its respective code and output. The document is intended for educational purposes, specifically for students in a Computer Programming course.

Uploaded by

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

1) Write a program to find whether a number is palindrome or not.

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

void main()
{
int num, r, sum = 0, t;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);

for(t=num;num!=0;num=num/10){
r=num % 10;
sum=sum*10+r;
}
if(t==sum)
printf("%d is a palindrome number.\n",t);
else
printf("%d is not a palindrome number.\n",t);

getch();

Output
2) Write a C program that replaces given word with new word in a sentence.
// Write a program to replace a word from a sentence
#include <stdio.h>
#include <conio.h>

void main()
{
char s1[80], w[20], ch, sw[20], rw[20];
int i, k;
clrscr();

printf("\nEnter string : ");


gets(s1);

printf("\nEnter search word : ");


gets(sw);

printf("\nEnter replace word : ");


gets(rw);

printf("\nOutput = ");
strcat(s1, " ");
k = 0;
for (i = 0; s1[i] != '\0'; i++)
{
ch = s1[i];
if (ch != ' ')
{
w[k] = ch;
k++;
}
else
{
w[k] = '\0';
// printf("\n %s",w);
k = 0;
if (strcmp(w, sw) == 0)
{
// strcat(rw," ");
printf("%s ", rw);
}
else
{
// strcat(w," ");
printf("%s ", w);
}
}
}

getch();
}

Output
3) Write a menu driven program to calculate area of Square, Triangle, Rectangle and Circle. (Use
concept of ‘switch’).
#include<stdio.h>
#include<conio.h>

#define PI 3.147

void main()
{
float radius, length, breadth;
float base, height, area;
int choice;
clrscr();

printf("Enter\n");
printf(" 1. To find area of triangle\n 2. To find area of Square\n");
printf(" 3. To find area of circle\n 4. To find area of rectangle\n");

scanf("%d",&choice);
switch(choice)
{
case 1:
printf(" Enter base and height of a triangle\n");
scanf("%f %f", &base, &height);

area = (1.0/2) * base * height;

printf(" Area of Triangle:\t%f\n", area);

break;

case 2:
printf(" Enter length of a Square\n");
scanf("%f", &length);

area = length * length;

printf(" Area of Square:\t%f\n", area);

break;

case 3:
printf(" Enter the radius of a Circle\n");
scanf("%f", &radius);

area = PI * radius * radius;


printf(" Area of Circle:\t%f\n", area);

break;

case 4:
printf(" Enter the length and breadth of a Rectangle\n");
scanf("%f %f", &length, &breadth);

area = length * breadth;

printf(" Area of Rectangle:\t%f\n", area);

break;

default:
printf(" Invalid Choice\n");
}
getch();
}

Output
4) Write a menu driven program that perform arithmetic operations (+, –, *, /) according to
user’s choice.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
clrscr();
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
}
Output
5) Write a program to check whether the number is prime or not.

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

void main()
{

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n == 0 || n == 1)
flag = 1;

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


{

if (n % i == 0)
{
flag = 1;
break;
}
}

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

getch();
}

Output
6) Write a program to input number through keyboard and check whether the number is
Armstrong number or not

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

void main() {
int num, originalNum, remainder, result = 0;
clrscr();

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);
getch();
}

Output
7) Write a program to display Fibonacci series up to 8 steps.
#include <stdio.h>
#include <conio.h>

void main()
{

int i, n;
int t1 = 0, t2 = 1;
clrscr();

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

getch();
}

Output
8) Write a c program to print an array of 10 elements in ascending order
#include<stdio.h>
#include<conio.h>

void main()
{
int i, arr{1,2,3,4,5,6,7,8,9,10};
clrscr();
for(i=0;i<10;i++)
{
printf("%d",arr[i])
}
getch();
}

Output
9) Write a program to print Following pattern: N=4
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, k, t = 0;
clrscr();
for (i = 1; i <= 4; i++)
{
for (k = t; k < 4; k++)
{
printf(" ");
}
for (j = 0; j < i; j++)
{
printf(" * ");
t = t + 1;
}
printf("\n");
}
getch();
}

Output
10) Write a program to count characters in the string. E. g. Input String:
BCA College Total Characters: 11

#include <stdio.h>
#include <string.h>

void main()
{
char string[] = "C is Fun";
int i, count = 1;
clrscr();

// Counts each character except space


for (i = 0; i < strlen(string); i++)
{
if (string[i] != ' ')
count++;
}

// Displays the total number of characters present in the given string


printf("Total number of characters in a string: %d", count);

getch();
}

Output
11) Write a program to calculate sum of each digit of a given number.

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

void main()
{
int num, sum = 0;

/* Input a number from user */


printf("Enter any number to find sum of its digit: ");
scanf("%d", &num);

while (num != 0)
{
sum += num % 10;
num = num / 10;
}

printf("Sum of digits = %d", sum);

getch();
}

Output
NAME : GOPAL PATIL

ROLL NO. : 116

SUBJECT : CPPM

FY.BCA

You might also like