0% found this document useful (0 votes)
37 views6 pages

Amstrong: 8.sum of Non-Neg Nos

The document contains code snippets for several C programming problems including: 1. Calculating the sum of non-negative numbers entered by the user 2. Checking if a number is a palindrome 3. Generating the Fibonacci series up to a given limit 4. Finding the largest and smallest number in an array 5. Counting the total number of words in a paragraph 6. Capitalizing the first word of a paragraph 7. Calculating the area of a circle using call by value and call by reference functions

Uploaded by

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

Amstrong: 8.sum of Non-Neg Nos

The document contains code snippets for several C programming problems including: 1. Calculating the sum of non-negative numbers entered by the user 2. Checking if a number is a palindrome 3. Generating the Fibonacci series up to a given limit 4. Finding the largest and smallest number in an array 5. Counting the total number of words in a paragraph 6. Capitalizing the first word of a paragraph 7. Calculating the area of a circle using call by value and call by reference functions

Uploaded by

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

printf("The Fibonacci series is : ");

8.SUM OF NON-NEG NOS printf("%d %d",a,b);


#include<stdio.h> c=a+b;
int main() for(i=0;i<n;i++)
{ {
int i,n,input,sum=0; printf(" %d",c);
scanf("%d",&n); a=b;
printf("Enter %d Numbers: ",n); b=c;
for(i=1;i<=n;i++) c=a+b;
{ if(c>n)
scanf("%d",&input); break;
if(input>=0) }
{ }
sum=sum+input;
}
11. AMSTRONG
} #include<stdio.h>
printf("Sum = %d\n",sum); int main()
} {
9.PALINDROME int input,remainder,sum=0,temp,count=0;
printf("Enter any number : ");
#include<stdio.h> scanf("%d",&input);
int main() temp=input;
{ while(input>0)
int n,originalnum,remainder,reversednum; {
printf("Enter an integer : "); input=input/10;
scanf("%d",&n); count++;
originalnum=n; }
while(n>0) input=temp;
{ while(input>0)
remainder=n%10; {
reversednum=reversednum*10+remainder; remainder=input%10;
n/=10; sum=sum+pow(remainder,count);
} input/=10;
printf("The reverse of a given number : %d\n",reversednum); }
if(originalnum==reversednum) if(temp==sum)
printf("%d is a palindrome\n",originalnum); printf("The given number %d is an armstrong number\n",sum);
else else
printf("%d is not a palindrome\n",originalnum); printf("The given number %d is not an armstrong number\n",temp);
} }

10. FIBBONACCI SERIES STORE AND PRINT


#include<stdio.h>
#include<stdio.h> int main()
int main() {
{ int n,i;
int n,a=0,b=1,c,i; printf("How many numbers do you want to enter: ");
printf("Enter the maximum limit to generate the Fibonacci series scanf("%d",&n);
: "); int array[n];
scanf("%d",&n); printf("Enter %d numbers: ",n);
for(i=0;i<n;i++) int ARUN(int arr[],int n,int k)
{ {
scanf("%d",&array[i]); int dp[n][k];
for(int i=0;i<n;i++)
}
{
printf("Entered numbers are: "); for(int j=0;j<k;j++)
for(i=0;i<n;i++) {
{ dp[i][j]=0;
if(i==0) }
printf("%d",array[i]); }
else for(int i=0;i<n;i++)
{
printf(" %d",array[i]); dp[i][0]=1;
} }
printf("\n"); for(int i=1;i<n;i++)
} {
for(int j=1;j<k;j++)
LARGEST & SMALLEST {
for(int m=0;m<i;m++)
#include<stdio.h> {
int main() if(arr[i]>arr[m])
{ dp[i][j]+=dp[m][j-1];
int i=0,n=0,small,large,arr[50]; }
}
printf("Enter no.of elements: "); }
scanf("%d",&n); int result=0;
printf("Enter element 1: "); for(int i=0;i<n;i++)
scanf("%d",&arr[0]); {
large=arr[0]; result +=dp[i][k-1];
small=arr[0]; }
return result;
for(i=1;i<n;i++) }
{ void main()
printf("Enter element %d: ",i+1); {
scanf("%d",&arr[i]); int result,n,k;
} int arr[50];
for(i=0;i<n;i++) printf("Enter the size: ");
scanf("%d",&n);
{
printf("Enter the elements: ");
if(small>arr[i]) for(int i=0;i<n;i++)
{ {
small=arr[i]; scanf("%d",&arr[i]);
} }
printf("value of k: ");
if(large<arr[i]) scanf("%d",&k);
{ result=ARUN(arr,n,k);
printf("Result: %d",result);
large=arr[i];
} }
}
printf("Largest element: %d\n",large);
printf("Smallest element: %d\n",small);
STRING SPLIT
} #include<stdio.h>
INCREASING SUBSEQUENCES #include<string.h>
int main()
#include<stdio.h> {
char inputstring[100]; strcat(temp, newWord);
scanf("%[^\n]",&inputstring); strcat(temp, pos + oldWordLen);
char *token = strtok(inputstring," "); strcpy(paragraph, temp);
index += pos - paragraph + strlen(newWord);
while(token!=NULL) paragraph += index;
{ }
printf("%s\n",token); }
token=strtok(NULL," ");
} int main() {
} char paragraph[1000];
char oldWord[50], newWord[50];
TOTAL NO OF WORDS printf("Enter a paragraph: ");
fgets(paragraph, sizeof(paragraph), stdin);
#include <stdio.h> printf("word to replace: ");
int main() scanf("%s", oldWord);
{ printf("new word: ");
char a[100]; scanf("%s", newWord);
int count = 0, i; printf("Original Paragraph: %s", paragraph);
printf("Enter a paragraph: "); replaceWord(paragraph, oldWord, newWord);
gets(a); printf("After replacing: %s", paragraph);
for(i=0;a[i]!='\0';i++) return 0;
{ }
if(a[i] == ' ')
count++; AREA OF CIRCLE CALL BY VALUE
}
printf("Total words: %d\n",count+1); #include <stdio.h>
#define PI 3.14
} double areaCallByValue(double radius) {
return PI * radius * radius;
FIRTST WORD CAPITAL }
void areaCallByReference(double radius, double *result) {
*result = PI * radius * radius;
#include<stdio.h> }
int main()
{
int main() {
char str[50]; double radius, area;
printf("Enter a paragraph: "); printf("radius: ");
gets(str); scanf("%lf", &radius);
str[0]=str[0]-32;
printf("Capitalizing first word:\n%s\n",str); // Calculate the area using call by value
} double areaByValue = areaCallByValue(radius);
printf("Area(Call by value): %.2f\n", areaByValue);

REPLACE WORDS // Calculate the area using call by reference


areaCallByReference(radius, &area);
printf("Area(Call by reference): %.2f\n", area);
#include <stdio.h>
#include <string.h> return 0;
}
void replaceWord(char *paragraph, const char *oldWord,
const char *newWord) {
char *pos, temp[1000];
FIBBONACCI
int index = 0, oldWordLen = strlen(oldWord);
#include <stdio.h>
while ((pos = strstr(paragraph, oldWord)) != NULL) {
strncpy(temp, paragraph, pos - paragraph); int fib(int n)
temp[pos - paragraph] = '\0';
{ int array1[size];

int a=0, b=1,c; printf("Enter elements: ");

if(n==0) for (int i = 0; i < size; i++) {

return a; scanf("%d", &array1[i]);

else if(n==1) }

return b; printf("Accessing the elements using the pointer: \n");

else for (int i = 0; i < size; i++) {

{ printf("%d\n", *(array1 + i));

for(int i=0; }

i<=n;i++){ return 0;

c=a+b; }

if(i==n-2) {

return c;

}
DYNAMIC MEMORY ALLOCATION
#include<stdio.h>
a=b;

b=c; int main()

} {

} int n;

} scanf("%d",&n);

#include <stdio.h> int a[n];


#include "fibonacci1.c"
void main() { for(int i=0;i<n;i++)
int n, i;
printf("Enter value of n : "); {
scanf("%d", &n);
printf("The fibonacci series of %d terms are : ", n); scanf("%d",&a[i]);
for (i = 0; i < n; i++) {
printf(" %d ", fib(i)); }
}
} for(int i=n-1;i>=0;i--)

printf("%d ",a[i]);

ACCESS ELEMENT USING POINTER }

}
#include <stdio.h>

int main() {

int size;

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

scanf("%d", &size);
printf("1. Add Expense\n");
printf("2. Calculate Total Expense by Category\n");
printf("3. Calculate Total Expense by Date\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

SUM OF DIGITS if (choice == 1) {


if (numExpenses < MAX_EXPENSES) {
printf("Enter date (YYYY-MM-DD): ");
#include <stdio.h> scanf("%s", expenses[numExpenses].date);
union Data { printf("Enter product name: ");
scanf("%s", expenses[numExpenses].product);
int number; printf("Enter price: ");
scanf("%f", &expenses[numExpenses].price);
}; printf("Enter category: ");
scanf("%s", expenses[numExpenses].category);
int sumOfDigits(int num) { numExpenses++;
} else {
int sum = 0;
printf("Expense list is full!\n");
}
while (num != 0) {
} else if (choice == 2) {
printf("Enter category: ");
sum += num % 10;
scanf("%s", category);
num /= 10; float total = totalExpenseByCategory(expenses, numExpenses, category);
printf("Total expense in category '%s' is %.2f\n", category, total);
} } else if (choice == 3) {
printf("Enter date (YYYY-MM-DD): ");
return sum; scanf("%s", date);
float total = totalExpenseByDate(expenses, numExpenses, date);
} printf("Total expense on date '%s' is %.2f\n", date, total);
} else if (choice == 4) {
int main() { break;
} else {
union Data input; printf("Invalid choice\n");
}
int sum; }

scanf("%d", &input.number); return 0;


}
sum = sumOfDigits(input.number);
STUDENT INFORMATION MANAGEMENT SYSTEM
printf("%d\n", sum);
#include <stdlib.h>
return 0; #include <string.h>
} #include <stdio.h>
struct Student {
char name[100];
char department[100];
int yearofstudy;
EXPENSE MANAGER float passcgpa;
};
#include <stdio.h>
int compareName(const void *a, const void *b) {
int main() {
return strcmp(((struct Student *)a)->name, ((struct Student *)b)->name);
struct Expense expenses[MAX_EXPENSES];
}
int numExpenses = 0;
int main() {
int choice;
int numstudents;
char category[50];
if (scanf("%d", &numstudents) != 1) {
char date[11];
printf("Invalid input for the number of students.\n");
return 1;
printf("Expense Manager\n");
}
while (1) {
struct Student *students = (struct Student *)malloc(numstudents * sizeof(struct Student));
printf("Options:\n");
if (students == NULL) {
printf("Memory allocation error.\n");
return 1;
}

for (int i = 0; i < numstudents; ++i) {


if (scanf("%s %s %d %f", students[i].name, students[i].department, &students[i].yearofstudy,
&students[i].passcgpa) != 4) {
printf("Invalid input for student %d.\n", i + 1);
free(students);
return 1;
}
}

qsort(students, numstudents, sizeof(struct Student), compareName);

for (int i = 0; i < numstudents; ++i) {


printf("Name:%s\n",students[i].name);
printf("Department:%s\n",students[i].department);
printf("Year of study:%d\n",students[i].yearofstudy);
printf("CGPA:%.1f\n",students[i].passcgpa);
}

free(students);

return 0;

};

FACTORIAL
#include <stdio.h>
#include "Program901a.c"
void main() {
long int n;
printf("Enter an integer : ");
scanf("%ld", &n);
printf("Factorial of %ld is : %ld\n", n ,factorial(n));
}

#include <stdio.h>

int factorial(num)

if (num==0 || num==1)

return 1;

else

return

num*factorial(num-1);

You might also like