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

Programmes

The document contains code for 3 C programs: 1) A simple "Hello World" program that prints "Hello World!" 2) A program that takes 2 numbers as input, prints the numbers, and prints their sum 3) A program that takes a positive number N as input, then takes N integer inputs and prints their sum
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Programmes

The document contains code for 3 C programs: 1) A simple "Hello World" program that prints "Hello World!" 2) A program that takes 2 numbers as input, prints the numbers, and prints their sum 3) A program that takes a positive number N as input, then takes N integer inputs and prints their sum
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.HELLO PROGRAMME: /* hello.c -- The most famous program of them all .. */ #include <stdio.h> int main(void) { printf("Hello World!

\n"); // return 0; }

2.ADDING 2 NUMBERS
/* add2.c -- Add two numbers and print them out together with their sum AUTHOR: DATE: */ #include <stdio.h> int main(void) { int first, second; printf("Enter two integers > "); scanf("%d %d", &first, &second); printf("The two numbers are: %d %d\n", first, second); printf("Their sum is %d\n", first+second);

3.ADDING N POSITIVE NUMBERS


/* addn.c -- Read a positive number N. Then read N integers and * print them out together with their sum. */ #include <stdio.h> int main(void) int n; int sum; int current; int lcv; { /* /* /* /* The number of numbers to be read The sum of numbers already read The number just read Loop control variable, it counts of numbers already read */ */ */ */ the number

printf("Enter a positive number n > "); scanf("%d",&n); /* We should check that n is really positive*/ sum = 0; for (lcv=0; lcv < n; lcv++) { printf("\nEnter an integer > "); scanf("%d",&current); /* printf("\nThe number was %d\n", current); */ sum = sum + current; } printf("The sum is %d\n", sum); return 0;

You might also like